Results 1 to 20 of 20
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Problem with int method ..!!

    Hi

    How is everyone ?

    I have problem with the follow code.

    When I run the programe and enter the first number, it gives me a random result and doesn't allow me to enter the second number

    Java Code:
    import java.io.*;
    public class Fire {
    public static void main(String[] args)throws IOException{
    	BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
    	
    	System.out.println("Enter the first number: ");
    	int first_num = in.read();
    	
    	System.out.println("Enter the second number: ");
    	int second_num = in.read();
    	
    	int sum = first_num + second_num;
    	
    	System.out.println("The summation = "+ sum);
    }
    }
    I think the problem with the read() method !!

    I hope I find the solution

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    That's not how to read numbers; the read() method reads a single char value. Also the <enter> key leaves a character in the input buffer which will be read by that method as well. It seems that you can't type another number; there are still characters in the input buffer that are read by that method.

    kind regards,

    Jos

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Thank you Jos for the great explanation

    so, what can I do to fix this problem and allow the user to enter two numbers ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    Thank you Jos for the great explanation

    so, what can I do to fix this problem and allow the user to enter two numbers ?
    Read the API documentation for the Integer.parseInt() method; also read up about the BufferedReader.readLine() method; alternatively you may use the Scanner class.

    kind regards,

    Jos

  5. #5
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    u can use readline

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter 1st number: ");
    String x = input.readLine();
    Integer n1 = Integer.parseInt(x);

    System.out.println("Enter 2nd number: ");
    String y = input.readLine();
    Integer n2= Integer.parseInt(y);

    int sum = n1+n2;

    System.out.println("The sum is " + sum);

  6. #6
    Syntax's Avatar
    Syntax is offline Member
    Join Date
    Jan 2010
    Location
    Georgia
    Posts
    20
    Rep Power
    0

    Default

    I would recommend using num1 = one.nextInt(); rather than reading, converting to int, then computing.
    11th Grade | Beginner Programmer | Looking into College

  7. #7
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    I also having trouble with those things.
    is this right:
    Scanner is for strings
    and InputStreamReader is for in character?
    parseInt() is for integers?
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    I also having trouble with those things.
    is this right:
    Scanner is for strings
    and InputStreamReader is for in character?
    parseInt() is for integers?
    No, InputStreams and OutputStreams are for byte oriented I/O. Readers and Writers are for character oriented I/O. An InputStreamReader converts an InputStream to a Reader. Different Readers have different capabilities.

    Scanners can read from several input sources (check its different constructors) and parse the bytes or chars to numbers and more. A Scanner is a higher level class than a simple InputStream or Reader.

    kind regards,

    Jos

  9. #9
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    thanks that cleared out a lot.

    do you work in the programming industries? because you seem to understand evry question :P
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    thanks that cleared out a lot.

    do you work in the programming industries? because you seem to understand evry question :P
    Yep, I've been in the ICT industry for a bit more than 30 years now and I studied mathematics before that. Don't tell my old mother about it ;-)

    kind regards,

    Jos

  11. #11
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    wow, thats some nice shit.

    than you probaly can tell me is there good work in the programming industry.
    I'm only 14 years old, i started programming and i like it and i'm not going to let it down because of my age. But i'm thinking about a possible career in ICT.

    is there a lot of work in this section, since we live in the same country you maybe know that?
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    wow, thats some nice shit.

    than you probaly can tell me is there good work in the programming industry.
    I'm only 14 years old, i started programming and i like it and i'm not going to let it down because of my age. But i'm thinking about a possible career in ICT.

    is there a lot of work in this section, since we live in the same country you maybe know that?
    There is a lot of work to be done but there's also a lot of boring 'sweatshop' work. Take care that you really know something and have something 'unique' to offer (mathematics or physics both do fine) next to 'just' being able to program.

    kind regards,

    Jos

  13. #13
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    thanks, im bad at mathematics :S physics are fine with me. was best of class with last test. Is VWO a high enough education or i need Gymnasium to become programmer?
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    thanks, im bad at mathematics :S physics are fine with me. was best of class with last test. Is VWO a high enough education or i need Gymnasium to become programmer?
    Better go for the whole nine yards: VWO --> University; you won't regret it afterwards. Gymnasium is fun if you like Latin and/or Greek; you won't need that for an interesting programming job (to tell you the truth I don't even know what else you would need it for ;-)

    kind regards,

    Jos

  15. #15
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    haha ;) well i have a long way to go.
    at the moment programming is a 2nd part next to school. But i sure want to go to university, otherways i'd better had done HAVO if i aint going to university tough.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    haha ;) well i have a long way to go.
    at the moment programming is a 2nd part next to school. But i sure want to go to university, otherways i'd better had done HAVO if i aint going to university tough.
    Take it from an old sod like me: studying is so much more fun than a boring full time job; go for it, working is for dummies ;-)

    kind regards,

    Jos

  17. #17
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    we'll all have to work once :P
    i do it now, bringing around newspapers. always the same bullshit thats boring
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    we'll all have to work once :P
    i do it now, bringing around newspapers. always the same bullshit thats boring
    See? Work is 20% inspiration and 80% perspiration at best. If you have finished University you are more likely to find a job that is near that optimum. And it pays better too ;-)

    kind regards,

    Jos

  19. #19
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Thanks 4 everyone

    I have used the Scanner class and it works properly.

    Java Code:
    import java.util.Scanner;
    public class Fire {
    public static void main(String[] args){
    	
    	Scanner sc = new Scanner(System.in);
    	
    	System.out.println("Enter the first number: ");
    	int first_num = sc.nextInt();
    	
    	System.out.println("Enter the second number: ");
    	int second_num = sc.nextInt();
    	
    	int sum = first_num + second_num;
    	
    	System.out.println("The summation = "+ sum);
    }
    }

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    Thanks 4 everyone

    I have used the Scanner class and it works properly.
    Good to see you managed to solved it yourself; almost the same number of lines/statements as your first version but now it is correct and works. Here's a small tip: always keep that API documentation near you, you'll need it (and so does everybody else).

    kind regards,

    Jos

Similar Threads

  1. I have a problem filling out this method
    By kira137 in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 01:43 AM
  2. help to solve tis problem in method
    By alrebatsd in forum New To Java
    Replies: 4
    Last Post: 06-07-2009, 03:35 PM
  3. Method problem
    By Jeff6461 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 06:48 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Problem with sort method
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •