Inputting numbers in java?

Caporegime
Joined
12 Mar 2004
Posts
29,952
Location
England
How do I input integers into a program?

This code will take the number I give it, but then it outputs the sum completley wrong.

public class gallonstolitres
{
public static void main(String[] args) throws Exception
{
char gallons;
System.out.println("how many gallons do you want to be converted?");
gallons = (char)System.in.read();
System.in.read();
System.out.println(gallons+" gallons is "+gallons*4.5+" litres");
}
}

I assume I need to change the char gallons to int gallons, otherwise it uses the int value for the keyboard char I pressed and not the actual number I typed in? How do I do this?
 
Last edited:
If you're reading it in as a char, surely there can only be 1 digit inputs?

Makes more sense to read in as a string, and the use Integer.parseint to convert.
 
Everything seems more complicated in java, why didn't they just use c++'s method of typing in cin>> for any type of variable?
 
You need to use Integer.parseInt() to convert the character to an integer and store it in an int variable. :)

P.S. put your code in
Code:
 tags ;)
 
Inquisitor said:
You need to use Integer.parseInt() to convert the character to an integer and store it in an int variable. :)

P.S. put your code in
Code:
 tags ;)[/QUOTE]

Where do I put this line of code in my program?
 
Energize said:
Everything seems more complicated in java, why didn't they just use c++'s method of typing in cin>> for any type of variable?
Probably because it's illogical to have syntax solely for getting input from/sending output to streams. Java is an object oriented language through and through, so it's all done with objects instead.

You'd probably say the same if you were going to C++ from Java ;)
 
It's just that it seems to be much more typing, wouldn't it make sense to only use objects where nessecary to reduce the amount of code?
 
Energize said:
Where do I put this line of code in my program?
You need to assign it to a variable, and then use that variable in the string.

Something like this:
Code:
int number = Integer.parseInt(gallons);
System.out.println(gallons+" gallons is " + number * 4.5 + " litres");
 
Energize said:
It's just that it seems to be much more typing, wouldn't it make sense to only use objects where nessecary to reduce the amount of code?
That's a good point, and one that is often made against Java, in fact :)
 
Last edited:
Ok this is my code now:

Code:
public class integer
{
public static void main(String[] args) throws Exception
{
int gallons;
System.out.println("Please enter the amount of gallons you want to be converted");
gallons = System.in.read();
          System.in.read();
			 int number = Integer.parseInt(gallons);
			 System.out.println(gallons+" gallons is "+number*4.5+" litres");
			 }}

But I get this error
Code:
 ----jGRASP exec: javac -g C:\java files\integer.java

integer.java:9: cannot find symbol
symbol  : method parseInt(int)
location: class java.lang.Integer
			 int number = Integer.parseInt(gallons);
			                     ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
 
Whoops, Integer.parseInt takes a string as an argument, not a character, it would seem (I don't use Java myself).

Try this:
Code:
int number = Integer.parseInt(String.valueOf(gallons));
Basically this just takes gallons, converts it to a string, and passes the result straight into parseInt.
 
Last edited:
InputStream.read() returns int, no need to convert.

Code:
int gallons = System.in.read();
(as you already have!)

Then for printing/concatenating:
Code:
System.out.println(String.valueOf(gallons) + "blah blah" + String.valueOf(gallons*4.5));
 
Last edited:
Ok, now I have this;

Code:
public class integer
{
public static void main(String[] args) throws Exception
{
int gallons;
System.out.println("Please enter the amount of gallons you want to be converted");
gallons = System.in.read();
          System.in.read();
			 System.out.println(String.valueOf(gallons)+" gallons is "+ String.valueOf(gallons*4.5)+" litres");
			 }}

But I still get 5 = 53 = 238.5
 
Last edited:
Aye, see my edited version. I was a little too hasty - and have been using SmallTalk a lot more than Java lately :p (No primitives in smalltalk - absolutely everything is an object - even the class definitions are objects, as well as each and every method being an instance of the method object. Dizziness just thinking about it all!)
 
I'm doing java because its the only programming course my school do, but I have to ask, why would you want to use java instead of c++ or c# when making a program for a windows pc? Java programs do seem to run slow.
 
Read it in as a String...

Code:
Scanner input = new Scanner(System.in);
String line = input.nextLine();
int a = Integer.parseInt(line);
 
You probably wouldn't use it for making a windows program. It's good because it can work on just about any platform. It's also good because it teaches proper OO. And it's free, and there's shed loads of tutorials/help out there.
 
Well, it's more C++ vs. C# and Java than Java vs. C++ and C#. C# and Java are very similar, the main differences being that a) C# has on the whole more features than Java and b) Java is portable to practically all major platforms, whereas C# is targeted solely at Windows platforms.
 
Back
Top Bottom