Inputting numbers in java?

Caporegime
Joined
12 Mar 2004
Posts
29,962
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:
Everything seems more complicated in java, why didn't they just use c++'s method of typing in cin>> for any type of variable?
 
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?
 
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?
 
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.
 
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:
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.
 
Ok this works,

Code:
import javax.swing.*;
 
public class galtolit
{
   public static void main(String[] args)
   {
      String i;
      int gallons;
      i = JOptionPane.showInputDialog("Enter the number of gallons you want to be converted");
      gallons = Integer.parseInt(i);
      System.out.println(gallons+" gallons is "+gallons*4.6+" litres");
      System.exit(0);
   }
}

but when I try to get rid of the graphics, it doesn't.

Code:
public class galtolit
{
   public static void main(String[] args) throws Exception
   {
      String i;
      int gallons;
      i = (String) System.in.read("Enter the number of gallons you want to be converted");
		System.in.read();
      gallons = Integer.parseInt(i);
      System.out.println(gallons+" gallons is "+gallons*4.6+" litres");
      System.exit(0);
   }
}

Instead I get "ConvertString.java:8: cannot find symbol
symbol : method read(java.lang.String)
location: class java.io.InputStream
i = (String) System.in.read("Enter the number of gallons you want to be converted");
........................... ^"

I suppose I can live with using the graphics box, but it's more stuff to remember.
 
Last edited:
Chrisss said:
Presumably because System.in.read() doesn't take in any arguments - you're giving it a String.

And why are you calling in.read twice?

Guide, says I need to put it in again to catch the user pressing enter.
 
Back
Top Bottom