Use of integers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Use of integers

  1. #1
    Join Date
    Nov 2003
    Posts
    22

    Use of integers

    Hi I'm really new to java and is trying stuff out as I learn them. I'm trying to make a calculator where you type in two numbers and they get added toghether. For some reason it doesn't work and the program is making up numbers of its' own! I really have no idea why. I'll post the hole code since it isn't big:
    Code:
        public static void main (String [] args)throws Exception {
            
            int sum=0;
            int num1=2;
            int num2=2;
            char choice ='R';
           
            
            
            System.out.println("Type in your first number");
            System.out.flush();
            num1=(int)System.in.read();
            System.out.println("Type in your second number");
            System.out.flush();
            num2=(int)System.in.read();
            System.out.println("You now have the numbers" + num1);
            System.out.println("and" +num2);
            
            choice=(char)System.in.read();
            
            if ( choice == 'A' ) {
                sum=num1+num2; 
                System.out.println(sum);
            }
            
        }
        
    }
    The if statement is there because I want to add the option to choose what to do with the numbers but first I need this to work. What happens when I run the program is that it asks for my first number like it's supposed to and I enter it. The it askes for my second number but at the same time moves on to the other part. I entered a 5 as my first number and it looks like this:
    Type in your first number
    5
    Type in your second number
    You now have the numbers53
    and 10


    Anyone knows what I've done wrong? Thanks..

    [ArchAngel added CODE tags]

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    It's much better to read user input like this:
    Code:
    import java.io.*;
    
    public class Test {
            public static void main(String[] args) throws IOException {
                    // Setup a reader for standard in.
                    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                    // Get input from user.
                    String line = reader.readLine();
                    // Print out the line just entered by the user.
                    System.out.println(line);
            }
    }
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    22
    Will that work when I want to add the numbers later? I thought I needed an integer or a float for that. But I'm really new to Java Thanks BTW.

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    Yes, you just convert the Strings into numbers of the appropriate type using Integer.parseInt(String) and Float.parseFloat(String) etc.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2003
    Posts
    22
    Okey I've changed it to the way you said and added the parsings. But now when I want to add the numbers the IDE claims that they are strings and not ints so I can't do that. Here's the code:
    Code:
    import java.io.*;
    
    public class UserInput {
        
        /** Creates a new instance of UserInput */
    public static void main(String [] args)throws Exception {
    
    int Sum = 0;
    
    
    
    System.out.println("Type in your first number.");
    System.out.flush();
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String num1 = reader.readLine();
    Integer.parseInt(num1);
    
    System.out.println("Type in your second number.");
    System.out.flush();
    BufferedReader reader2 = new BufferedReader(new InputStreamReader(System.in));
    String num2 = reader2.readLine();
    Integer.parseInt(num2);
    //Here I want to add the line Sum=num1+num2
    System.out.println("The sum is" + Sum);
    }
    
    }

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    The line:
    Code:
    Integer.parseInt(num1);
    Does not convert a String variable into an int variable - that would make no sense. This method returns an int representation of the String argument:
    Code:
    int integerRepresentation = Integer.parseInt(stringRepresentation);
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center