Results 1 to 5 of 5
Thread: Hello guys
- 07-23-2011, 05:31 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 31
- Rep Power
- 0
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
-
Your problem is with your reading in a character this way:
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((char) System.in.read());
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.,
The link in my signature will show you how to do this.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"); } } }
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.
- 07-23-2011, 05:42 PM #3
On line 33 of your program you are calling parseInt with an empty String: ""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)
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.
- 07-23-2011, 06:42 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 31
- Rep Power
- 0
thank you so much,this is a life saver...
and yea ill keep this in mind,thank you very much :)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 thanks for the info about the code tag,i didnt notice that til now...im new to forums btw :)
- 07-23-2011, 06:44 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Guys please i need help !!! :d
By omar_6630 in forum New To JavaReplies: 7Last Post: 11-23-2010, 12:55 AM -
Hi guys,
By pereira.lawrence.java in forum IntroductionsReplies: 0Last Post: 03-17-2009, 03:55 AM -
What's up guys
By zuriick in forum New To JavaReplies: 8Last Post: 02-06-2009, 04:39 AM -
Hello Guys...
By balaram.bocha in forum IntroductionsReplies: 0Last Post: 02-03-2009, 06:11 PM -
Hello Guys!
By jeraldjamescapao in forum Advanced JavaReplies: 1Last Post: 11-22-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks