Results 1 to 5 of 5

Thread: Hello guys

  1. #1
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default Hello guys

    its really taking me some time to finish this program,i dont know why i kept getting errors when i compile it but it doesnt show any error on the compiler
    ------------------------------------


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author INTEL
    */import java.io.*;
    public class ass {
    public static void main(String [] args)throws Exception{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    char selection;
    int number1,number2;
    String strnum1,strnum2;

    System.out.println("PRESS [A] FOR ADDITION");
    System.out.println("PRESS [S] FOR SUBSTRACTION");
    System.out.println("PRESS [M] FOR MULTIPLICATION");
    System.out.println("PRESS [D] FOR DIVISION");
    System.out.println("Enter your choice: ");

    selection=Character.toUpperCase((char)System.in.re ad());

    if (selection == 'A')
    {
    System.out.println("ADD TWO NUMBERS: ");
    System.out.println("Enter First Number: ");
    strnum1=in.readLine();
    number1=Integer.parseInt(strnum1);
    System.out.println("Enter Second Number: ");
    strnum2=in.readLine();
    number2=Integer.parseInt(strnum2);
    System.out.println("The Sum of Two numbers is: " + (number1 + number2));
    }
    else if (selection =='S')
    {
    System.out.println("SUBSTRACT TWO NUMBERS: ");
    System.out.println("Enter First Number: ");
    strnum1=in.readLine();
    number1=Integer.parseInt(strnum1);
    System.out.println("Enter Second Number: ");
    strnum2=in.readLine();
    number2=Integer.parseInt(strnum2);
    System.out.println("The Difference of Two numbers is: " + (number1 - number2));
    }
    else if (selection =='M')
    {
    System.out.println("MULTIPLY TWO NUMBERS: ");
    System.out.println("Enter First Number: ");
    strnum1=in.readLine();
    number1=Integer.parseInt(strnum1);
    System.out.println("Enter Second Number: ");
    strnum2=in.readLine();
    number2=Integer.parseInt(strnum2);
    System.out.println("The Product of Two numbers is: " + (number1 * number2));
    }
    else if (selection =='D')
    {
    System.out.println("DIVIDE TWO NUMBERS: ");
    System.out.println("Enter First Number: ");
    strnum1=in.readLine();
    number1=Integer.parseInt(strnum1);
    System.out.println("Enter Second Number: ");
    strnum2=in.readLine();
    number2=Integer.parseInt(strnum2);
    System.out.println("The Quotient of Two numbers is: " + (number1/number2));
    }

    else
    {
    System.out.println("INVALID CHOICE");
    }



    }

    }
    ------------------------------------------------------------------------------------


    init:
    deps-jar:
    compile-single:
    run-single:
    PRESS [A] FOR ADDITION
    PRESS [S] FOR SUBSTRACTION
    PRESS [M] FOR MULTIPLICATION
    PRESS [D] FOR DIVISION
    Enter your choice:
    A
    ADD TWO NUMBERS:
    Enter First Number:
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:468)
    at java.lang.Integer.parseInt(Integer.java:497)
    at ass.main(ass.java:31)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)

    that is what it displays after i enter the character on this statement :\ i would really appreciate some help cause ive been working on this for hours

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is with your reading in a character this way:

    Java Code:
     selection = Character.toUpperCase((char) System.in.read());
    This doesn't handle the end of line character and I think that this is what is tripping you up. Instead, use your BufferedReader to read in a line and only take the first character from the String read in via String's charAt method:

    Java Code:
    selection = Character.toUpperCase(in.readLine().charAt(0));

    Oh, and while you're new here, you've been here long enough so that you should start using code tags when posting code as this will allow your code to retain its formatting and thus be readable. For e.g.,

    Java Code:
    import java.io.*;
    
    public class Assignment {
       public static void main(String[] args) throws Exception {
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
          char selection;
          int number1, number2;
          String strnum1, strnum2;
    
          System.out.println("PRESS [A] FOR ADDITION");
          System.out.println("PRESS [S] FOR SUBSTRACTION");
          System.out.println("PRESS [M] FOR MULTIPLICATION");
          System.out.println("PRESS [D] FOR DIVISION");
          System.out.println("Enter your choice: ");
    
          // selection = Character.toUpperCase((char) System.in.read());
          selection = Character.toUpperCase(in.readLine().charAt(0));
    
          if (selection == 'A') {
             System.out.println("ADD TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out
                   .println("The Sum of Two numbers is: " + (number1 + number2));
          } else if (selection == 'S') {
             System.out.println("SUBSTRACT TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out.println("The Difference of Two numbers is: "
                   + (number1 - number2));
          } else if (selection == 'M') {
             System.out.println("MULTIPLY TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out.println("The Product of Two numbers is: "
                   + (number1 * number2));
          } else if (selection == 'D') {
             System.out.println("DIVIDE TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out.println("The Quotient of Two numbers is: "
                   + (number1 / number2));
          }
    
          else {
             System.out.println("INVALID CHOICE");
          }
    
       }
    
    }
    The link in my signature will show you how to do this.


    Also one more nitpick: please use an informative thread title. "Hello guys" tells us nothing about your problem. Instead use something useful such as "problem reading in user input" or some-such.
    Last edited by Fubarable; 07-23-2011 at 05:41 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:468)
    at java.lang.Integer.parseInt(Integer.java:497)
    at ass.main(ass.java:31)
    On line 33 of your program you are calling parseInt with an empty String: ""
    Check the logic at that point to see why the String does not have valid data in it?

    You should test if the user has entered bad data or no data before trying to convert what was entered to an int.

    Naming conventions say Your class name should start with a Capital letter.

  4. #4
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your problem is with your reading in a character this way:

    Java Code:
     selection = Character.toUpperCase((char) System.in.read());
    This doesn't handle the end of line character and I think that this is what is tripping you up. Instead, use your BufferedReader to read in a line and only take the first character from the String read in via String's charAt method:

    Java Code:
    selection = Character.toUpperCase(in.readLine().charAt(0));

    Oh, and while you're new here, you've been here long enough so that you should start using code tags when posting code as this will allow your code to retain its formatting and thus be readable. For e.g.,

    Java Code:
    import java.io.*;
    
    public class Assignment {
       public static void main(String[] args) throws Exception {
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
          char selection;
          int number1, number2;
          String strnum1, strnum2;
    
          System.out.println("PRESS [A] FOR ADDITION");
          System.out.println("PRESS [S] FOR SUBSTRACTION");
          System.out.println("PRESS [M] FOR MULTIPLICATION");
          System.out.println("PRESS [D] FOR DIVISION");
          System.out.println("Enter your choice: ");
    
          // selection = Character.toUpperCase((char) System.in.read());
          selection = Character.toUpperCase(in.readLine().charAt(0));
    
          if (selection == 'A') {
             System.out.println("ADD TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out
                   .println("The Sum of Two numbers is: " + (number1 + number2));
          } else if (selection == 'S') {
             System.out.println("SUBSTRACT TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out.println("The Difference of Two numbers is: "
                   + (number1 - number2));
          } else if (selection == 'M') {
             System.out.println("MULTIPLY TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out.println("The Product of Two numbers is: "
                   + (number1 * number2));
          } else if (selection == 'D') {
             System.out.println("DIVIDE TWO NUMBERS: ");
             System.out.println("Enter First Number: ");
             strnum1 = in.readLine();
             number1 = Integer.parseInt(strnum1);
             System.out.println("Enter Second Number: ");
             strnum2 = in.readLine();
             number2 = Integer.parseInt(strnum2);
             System.out.println("The Quotient of Two numbers is: "
                   + (number1 / number2));
          }
    
          else {
             System.out.println("INVALID CHOICE");
          }
    
       }
    
    }
    The link in my signature will show you how to do this.


    Also one more nitpick: please use an informative thread title. "Hello guys" tells us nothing about your problem. Instead use something useful such as "problem reading in user input" or some-such.
    thank you so much,this is a life saver...

    Also one more nitpick: please use an informative thread title. "Hello guys" tells us nothing about your problem. Instead use something useful such as "problem reading in user input" or some-such.
    and yea ill keep this in mind,thank you very much :)

    and thanks for the info about the code tag,i didnt notice that til now...im new to forums btw :)

  5. #5
    amzers is offline Member
    Join Date
    Dec 2010
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    On line 33 of your program you are calling parseInt with an empty String: ""
    Check the logic at that point to see why the String does not have valid data in it?

    You should test if the user has entered bad data or no data before trying to convert what was entered to an int.

    Naming conventions say Your class name should start with a Capital letter.
    yea,ive input the correct data,but ive managed to make the program work with the help of the guy above you :)

    thank though :D

Similar Threads

  1. Guys please i need help !!! :d
    By omar_6630 in forum New To Java
    Replies: 7
    Last Post: 11-23-2010, 12:55 AM
  2. Hi guys,
    By pereira.lawrence.java in forum Introductions
    Replies: 0
    Last Post: 03-17-2009, 03:55 AM
  3. What's up guys
    By zuriick in forum New To Java
    Replies: 8
    Last Post: 02-06-2009, 04:39 AM
  4. Hello Guys...
    By balaram.bocha in forum Introductions
    Replies: 0
    Last Post: 02-03-2009, 06:11 PM
  5. Hello Guys!
    By jeraldjamescapao in forum Advanced Java
    Replies: 1
    Last Post: 11-22-2008, 07:42 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
  •