-1
package myfirstclass;

import java.io.IOException;

public class MyFirstClass {

    public static void main(String[] args) throws IOException {


        Car RedCar;
        RedCar = new Car();

        RedCar.carColor = "red";
        RedCar.milesPerGallon = 25;
        RedCar.numDoors = 2;

        Car BlueCar = new Car();
        BlueCar.carColor = "blue";
        BlueCar.milesPerGallon = 50;
        BlueCar.numDoors = 4;

        System.out.println("Choose a car...");
        int read = System.in.read();


        if(read == 1){
            System.out.println("Hello, and your car is...");

            System.out.println("Red!");

        }



    }
}

After I input a number, such as 1, it just says "Build Successful!", why is this? And how can I fix it to make sure it reads my input and follows the "if" statement correctly.

Thanks!

Nan N
  • 65
  • 1
  • 4
  • 3
    I suggest printing out the value of `read` to see what you get. Hint: it's not 1. – Greg Hewgill Jan 13 '15 at 23:21
  • 1
    Related: http://stackoverflow.com/questions/15273449/what-does-system-in-read-actually-return – Shawn Bush Jan 13 '15 at 23:24
  • 1
    You've asked almost exactly the same question before here - http://stackoverflow.com/questions/27298551/why-does-this-do-while-loop-not-produce-the-right-output. What is it about the answers there that you are still unclear on? If you can explain that - maybe we can help you. – J Richard Snape Jan 13 '15 at 23:27
  • All 3 of your questions on this site relate to System.in.read() - I think that you have a misunderstanding of what it does. The question now has two answers that give you an idea of what to look at. My personal opinion is that the suggestion to use a Scanner object for console based input is the best idea. – J Richard Snape Jan 13 '15 at 23:33
  • Yes, I would agree with you, J Richard. I figured it out, and will be using Scanner object from now on, thank you for your advice. – Nan N Jan 13 '15 at 23:36

2 Answers2

2

System.in.read() reads exactly one byte. In your example, the variable read will hold the value 49, not 1.

Use a Scanner instead:

Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();

Useful links:

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

JamesB
  • 7,774
  • 2
  • 22
  • 21
1

System.in.read() does not do what you think. It reads one byte from input and returns the integer value of it. If you type "1", System.in.read() returns 0x31, or 49. Not 1.

Unfortunately, what you want is way too complicated in Java.

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
if (Integer.parseInt(in.readLine()) == 1) {
    // do something
}

The first line creates an inane object that Java requires to read lines. The second line reads one line from the input using in.readLine(), converts it to an integer with Integer.parseInt, and then compares it to 1.

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • `Unfortunately, what you want is way too complicated in Java` You've just solved in a "complicated" way. JamesB for example has a much easier way (also easier to read for new ones). But still a possible way. – Tom Jan 13 '15 at 23:35
  • Upvote for suggesting an alternative to `Scanner`. – Greg Hewgill Jan 14 '15 at 01:06