Results 1 to 17 of 17
Thread: Java Project
- 11-13-2008, 06:13 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
Java Project
Need help with a java project I need to do. I've already posted what the project specifications are in a previous thread, but here it is again.
"""Create a console calculator applicaion that:
* Takes one command line argument: your name and surname. When the
program starts, display the date and time with a welcome message for the
user.
* Display all the available options to the user. Your calculator must include
the arithmetic operations as well as at least five scientific operations of the
Math class.
-Your program must also have the ability to round a number and
truncate it.
-When you multiply by 2, you should not use the '*' operator to perform the
operation.
-Your program must also be able to reverse the sign of a number.
* Include sufficient error checking in your program to ensure that the user
only enters valid input. Make use of the String; Character, and other
wrapper classes to help you.
* Your program must be able to do conversions between decimal, octal and
hex numbers.
* Make use of a menu. You should give the user the option to end the
program when entering a certain option.
* When the program exits, display a message for the user, stating the
current time, and calculate and display how long the user used your
program.
* Make use of helper classes where possible.
* Use the SDK to run your program."""
I have done alot more coding since my last thread, thanks to the help from xcallmejudasx and others. (Thank you!)
I have a few more questions that I need your help with because I'm stuck at the moment.
1. When the program starts it asks the user for his/her name and surname,
I have inserted an error check in case the user does not input anything
and it works, but now I'm struggling with the error check for when the
user inputs a number for his/her name and surname. Don't know how to do
this...
2. I have created methods for the calculator options in the "menu", nrs. '1-7'
works fine but nrs. 8; 9 and 0 from the "menu" doesn't want to work.
Let me explain, when the user enters '8' which he/she has chosen from,
(which is "Octal and Hexadecimal equivalent of numbers") in this case, it
does not give me the desired output, the output/answer it gives me
everytime is ' 0 ', which is totally not right.
The same is happening for option '9' which is ("Round numbers") from
the 'menu'.
The other option which is not working is '0' which is "Exit program", what
happens here is that when the user chooses this option (this option must
exit/stop the whole program immediately) but what happens is that the
program first asks the user for the second numbers input before stopping...
I think this is enough for now (there are a few more q's but I will come back to them later).
Here is my code that I have done so far, please have a look through my code and assist me of where I went wrong.
Your help would be much appreciated.Java Code:import java.io.*; import java.util.*; import java.text.*; public class Project { private static String nameSurname = ""; private static String num1 = null; private static String num2 = null; private static String choice1 = null; private static int answer = 0; private static String more = null; public int Add() { answer = Integer.parseInt(num1) + Integer.parseInt(num2); return answer; } public int Subtract() { answer = Integer.parseInt(num1) - Integer.parseInt(num2); return answer; } public int Multiply() { answer = Integer.parseInt(num1) * Integer.parseInt(num2); return answer; } public int Divide() { answer = Integer.parseInt(num1) / Integer.parseInt(num2); return answer; } public int Modulus() { answer = Integer.parseInt(num1) % Integer.parseInt(num2); return answer; } public int maximumValue() { answer = (Math.max(Integer.parseInt(num1), Integer.parseInt(num2))); return answer; } public int minimumValue() { answer = (Math.min(Integer.parseInt(num1), Integer.parseInt(num2))); return answer; } public int absoluteNumber1() { answer = (Math.abs(Integer.parseInt(num1))); return answer; } public int absoluteNumber2() { answer = (Math.abs(Integer.parseInt(num2))); return answer; } public double Squareroot1() { answer = (int)(Math.sqrt(Integer.parseInt(num1))); return answer; } public double Squareroot2() { answer = (int)(Math.sqrt(Integer.parseInt(num2))); return answer; } public int octalEquivalent() { answer = Integer.parseInt(num1 + num2); answer = Integer.parseInt(Integer.toOctalString(answer)); return answer; } public int hexadecimalEquivalent() { answer = Integer.parseInt(num1 + num2); answer = Integer.parseInt(Integer.toHexString(answer)); return answer; } public double Round1() { answer = Integer.parseInt(null, Math.round(answer)); return answer; } public double Round2() { answer = Integer.parseInt(null, Math.round(answer)); return answer; } SimpleDateFormat format1 = new SimpleDateFormat("EEEE, dd MMMM yyyy"); Date now = new Date(); SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a"); Date now2 = new Date(); public String insertNameAndSurname() throws IOException{ boolean inputCorrect = false; while (inputCorrect == false) { while (nameSurname == null || nameSurname.length() == 0) { inputCorrect = false; try { BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please enter your name and surname :"); nameSurname = inStream.readLine(); inputCorrect = true; }catch (IOException ex) { System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :"); inputCorrect = false; } } System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now)); System.out.println("and the time is now exactly " + format2.format(now2) + "."); } return nameSurname; } public String inputNumber1() throws IOException { boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); System.out.print("\nPlease enter a number you want to do a calculation with and hit <ENTER>: "); num1 = br.readLine(); int number1 = Integer.parseInt(num1); System.out.println("\nThe number you have entered is: " + number1); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("\nYou did not enter a valid number: " + "\""+ num1 + "\" is not a number!!"); inputCorrect = false; } } return num1; } public String calculatorChoice() throws IOException { System.out.println("Please select an option of what you would like to do with this number from the menu below and hit <ENTER>: "); System.out.println("\n*********************************************"); System.out.println("---------------------------------------------"); System.out.println("Please select an option from the list below: "); System.out.println("---------------------------------------------"); System.out.println("1 - Add"); System.out.println("2 - Subtract"); System.out.println("3 - Multiply"); System.out.println("4 - Divide (remainder included)"); System.out.println("5 - Maximum and minimum value of two numbers"); System.out.println("6 - Squareroot"); System.out.println("7 - Absolute value of numbers"); System.out.println("8 - Octal and Hexadecimal equivalent of numbers"); System.out.println("9 - Round numbers"); System.out.println("0 - Exit program"); System.out.println("**********************************************"); boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please enter your option and hit <ENTER>:"); choice1 = inStream.readLine(); int c1 = Integer.parseInt(choice1); System.out.println("\nYou have entered choice number: " + c1); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("You did not enter a valid choice number: " + "\""+ choice1 + "\" is not in the list!!"); inputCorrect = false; } } return choice1; } public String inputNumber2() throws IOException { boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in)); System.out.print("\nPlease enter another number you want to do the calculation with and hit <ENTER>: "); num2 = br2.readLine(); int n2 = Integer.parseInt(num2); System.out.println("\nThe second number you have entered is: " + n2); System.out.println("\nYour numbers are: " + num1 + " and " + num2); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("You did not enter a valid number: " + "\""+ num2 + "\" is not a number!!"); } } return num2; } public int Calculator() { int choice2 = Integer.parseInt(choice1); switch (choice2) { case 1 : Add(); System.out.println("The answer of " + num1 + " + " + num2 + " is: " + answer); break; case 2 : Subtract(); System.out.println("The answer of " + num1 + " - " + num2 + " is: " + answer); break; case 3 : Multiply(); System.out.println("The answer of " + num1 + " * " + num2 + " is: " + answer); break; case 4 : Divide(); System.out.print("The answer of " + num1 + " / " + num2 + " is: " + answer); Modulus(); System.out.println(" and the remainder is " + answer); break; case 5 : maximumValue(); System.out.println("The maximum number between the numbers " + num1 + " and " + num2 + " is: " + answer); minimumValue(); System.out.println("The minimum number between the numbers " + num1 + " and " + num2 + " is: " + answer); break; case 6 : Squareroot1(); System.out.println("The squareroot of value " + num1 + " is: " + answer); Squareroot2(); System.out.println("The squareroot of value " + num2 + " is: " + answer); break; case 7 : absoluteNumber1(); System.out.println("The absolute number of " + num1 + " is: " + answer); absoluteNumber2(); System.out.println("The absolute number of " + num2 + " is: " + answer); break; case 8 : octalEquivalent(); System.out.println("The octal equivalent of " + num1 + " is: " + answer); hexadecimalEquivalent(); System.out.println("The hexadecimal equivalent of " + num1 + " is: " + answer); octalEquivalent(); System.out.println("\nThe octal equivalent of " + num2 + " is: " + answer); hexadecimalEquivalent(); System.out.println("The hexadecimal equivalent of " + num2 + " is: " + answer); case 9 : Round1(); System.out.println("The rounded number of " + num1 + " is: " + answer); Round2(); System.out.println("The rounded number of " + num2 + " is: " + answer); case 0 : if (choice2 == 0) { System.exit(1); } } return choice2; } public String anotherCalculation() throws IOException { BufferedReader br3 = new BufferedReader (new InputStreamReader(System.in)); System.out.print("\nWould you like to do another calculation? Y/N"); more = br3.readLine(); if ((more == "y") | (more == "Y")) { inputNumber1(); System.out.println(""); calculatorChoice(); System.out.println(""); inputNumber2(); System.out.println(""); Calculator(); System.out.println(""); } else { System.exit(0); } return more; } public static void main(String[] args) throws IOException { Project p1 = new Project(); p1.insertNameAndSurname(); System.out.println(""); p1.inputNumber1(); System.out.println(""); p1.calculatorChoice(); System.out.println(""); p1.inputNumber2(); System.out.println(""); p1.Calculator(); System.out.println(""); p1.anotherCalculation(); System.out.println(""); } }
Thank you!
- 11-13-2008, 06:19 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you run your application? If so what happen, you didn't get expecting output? You have to test your application first. If you comes with any issue ask it here.
- 11-13-2008, 06:20 PM #3
This is a simple solution. All character values(capital and lowercase) fall in between a-Z, or A-z( I don't remember which is first); You can create a loop and use the charAt() method within string to check if each character is betwen a and Z. If one character is invalid then the entire name is invalid.1. When the program starts it asks the user for his/her name and surname,
I have inserted an error check in case the user does not input anything
and it works, but now I'm struggling with the error check for when the
user inputs a number for his/her name and surname. Don't know how to do
this...
Let me compile and mess around with your code for the second question and I'll get back to you.
- 11-13-2008, 06:32 PM #4
Your round methods are messing up because your trying to round answer instead of num1 and num2. I answer to
and that works. You have a logic flaw with your round methods also. I tried to input a decimal and it tells me not a number. It might make everything work more smoothly if you change some of your parses to parseDouble instead of parseIntJava Code:answer = Math.round(Integer.parseInt(num1); answer = Math.round(Integer.parseInt(num2);
- 11-13-2008, 08:47 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
Java Project
I got the round methods to work perfectly now, like youv'e said xcallmejudasx.
Tnx.
But I still can't the input name surname part right, I tried what youv'e said about using the charAt() method but I think I'm missing something, please look at my code and help me.
Here is the code:
ThanxJava Code:public String insertNameAndSurname() throws IOException{ boolean inputCorrect = false; while (inputCorrect == false) { while (nameSurname == null || nameSurname.length() == 0) { for (int i = 0; i < nameSurname.length(); i++) { if (String(nameSurname.charAt(i))){ inputCorrect = false; } } inputCorrect = false; try { BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please enter your name and surname :"); nameSurname = inStream.readLine(); inputCorrect = true; }catch (IOException ex) { System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :"); inputCorrect = false; } } System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now)); System.out.println("and the time is now exactly " + format2.format(now2) + "."); } return nameSurname; }
- 11-13-2008, 10:53 PM #6
your not comparing anything in your if statement.
This compares each character in your string and checks if it's a letter. If it is it goes to the next character and checks. If it finds a character that isn't a letter it returns false and breaks the loop. I'm not sure where a space character fits in the alphabet your going to need to look that up and maybe throw another conditional into the if to deal with it.Java Code:for (int i = 0; i < nameSurname.length(); i++) { if ((nameSurname.charAt(i) > "a") && (nameSurname.charAt(i) < "Z")){ inputCorrect = true; } else{ inputCorrect = false; break; } }Last edited by xcallmejudasx; 11-13-2008 at 10:56 PM.
- 11-13-2008, 10:58 PM #7
also remove the inputCorrect = false outside your loop. That just removes all work the loop did and makes every input false.
- 11-14-2008, 07:56 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
Java Project
I tried the code what youv'e said but it doesn't want to work, I get an error message that states - "The operator > is undefined for the argument type(s) char, String", at this part of the code :
I know the problm lies in the error message but not sure how to fix this??Java Code:if (((nameSurname.charAt(i) > "a")) && (nameSurname.charAt(i) < "Z")){...
- 11-14-2008, 07:59 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You cannot use > sign for character comparing. Just think that there is a meaning comparing characters as less than or greater than...
Check that equal or not.
- 11-14-2008, 03:42 PM #10
Are you sure? I am 90% sure I've compared characters like this before. Maybe I had a valueOf or something in front of it. I know in theory each character has an ASCII value and you can compare that value to determine if a letter is in fact a letter(since all characters from a-Z are letters). Is there something like charToIntValue() that would do that?
- 11-14-2008, 03:43 PM #11
omg woops didn't notice this before. Change the double quotes around "a" and "Z" to single quotes. Double means strings, single means char.
- 11-14-2008, 05:19 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-16-2008, 06:20 PM #13
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
Java Project
Hi all, I please need further help on my java project I need to do.
I'm stuck at the moment and isn't sure how to rectify these few problems:
1. The 'octalEquivalent();' and 'hexadecimalEquivalent(); methods that I
have created doesn't want to work, the error messages I'm getting is
"The method toOctalString(String) is undefined for the type Double" &
"The method toHexString(double) in the type Double is not applicable for
the arguments (String) ",
how can I correct this??
2. I have created a method ( anotherCalculation(); ), so that if the user
comes to the end of his/her first calculation, he/she has a choice if they
want to do another calculation, (here is the method: )
the problem is that when the user inputs antything or just press enter the program continues and never stops, what I want the program to do, is when the user comes to the question where the program asks ' Would you like to do another calculation? Y/N ', the user must input " 'y/Y' for yes and 'n/N' for no. If the user enters 'y/Y', the program has to continue and start where the program asks for a number... When the user enters ' n/N ' the program must exit, and display a message for the user, stating the current time and calculate and display how long the user used the program..Java Code:public char anotherCalculation() throws IOException { try { System.out.print("\nWould you like to do another calculation? Y/N"); more = (char) System.in.read(); do { inputNumber1(); System.out.println(""); calculatorChoice(); System.out.println(""); inputNumber2(); System.out.println(""); Calculator(); System.out.println(""); anotherCalculation(); System.out.println(""); } while((more == 'y') | (more == 'Y')); System.exit(0); } catch (IOException ex){ System.exit(0); } return more; }
Here is the code for the whole program that I coded so far...
Please help.Java Code:import java.io.*; import java.util.*; import java.text.*; public class Project { private static String nameSurname = ""; private static String num1 = null; private static String num2 = null; private static String choice1 = null; private static double answer = 0; private static char more; public double Add() { answer = (Double.parseDouble(num1) + Double.parseDouble(num2)); return answer; } public double Subtract() { answer = (Double.parseDouble(num1) - Double.parseDouble(num2)); return answer; } public double Multiply() { answer = (Double.parseDouble(num1) * Double.parseDouble(num2)); return answer; } public double Divide() { answer = (Double.parseDouble(num1) / Double.parseDouble(num2)); return answer; } public double Modulus() { answer = (Double.parseDouble(num1) % Double.parseDouble(num2)); return answer; } public double maximumValue() { answer = (Math.max(Double.parseDouble(num1), Double.parseDouble(num2))); return answer; } public double minimumValue() { answer = (Math.min(Double.parseDouble(num1), Double.parseDouble(num2))); return answer; } public double absoluteNumber1() { answer = (Math.abs(Double.parseDouble(num1))); return answer; } public double absoluteNumber2() { answer = (Math.abs(Double.parseDouble(num2))); return answer; } public double Squareroot1() { answer = (Math.sqrt(Double.parseDouble(num1))); return answer; } public double Squareroot2() { answer = (Math.sqrt(Double.parseDouble(num2))); return answer; } public double octalEquivalent1() { answer = Double.parseDouble(Double.toOctalString(num1)); return answer; } public double octalEquivalent2() { answer = Double.parseDouble(Double.toOctalString(num2)); return answer; } public double hexadecimalEquivalent1() { answer = Double.parseDouble(Double.toHexString(num1)); return answer; } public double hexadecimalEquivalent2() { answer = Double.parseDouble(Double.toHexString(num2)); return answer; } public double Round1() { answer = Math.round(Double.parseDouble(num1)); return answer; } public double Round2() { answer = Math.round(Double.parseDouble(num2)); return answer; } SimpleDateFormat format1 = new SimpleDateFormat("EEEE, dd MMMM yyyy"); Date now = new Date(); SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a"); Date now2 = new Date(); DecimalFormat decimals = new DecimalFormat("#0.00"); public String insertNameAndSurname() throws IOException{ boolean inputCorrect = false; while (inputCorrect == false) { while (nameSurname == null || nameSurname.length() == 0) { for (int i = 0; i < nameSurname.length(); i++) { if ((nameSurname.charAt(i) > 'a') && (nameSurname.charAt(i) < 'Z')){ inputCorrect = true; } else{ inputCorrect = false; break; } } try { BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Please enter your name and surname: "); nameSurname = inStream.readLine(); inputCorrect = true; }catch (IOException ex) { System.out.println("You did not enter your name and surname, " + nameSurname + " is not a name, please enter your name and surname :"); inputCorrect = false; } } System.out.println("\nA warm welcome " + nameSurname + " ,todays date is: " + format1.format(now)); System.out.println("and the time is now exactly " + format2.format(now2) + "."); } return nameSurname; } public String inputNumber1() throws IOException { boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); System.out.print("\nPlease enter a number you want to do a calculation with and hit <ENTER>: "); num1 = br.readLine(); double number1 = Double.parseDouble(num1); System.out.println("\nThe number you have entered is: " + number1); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("\nYou did not enter a valid number: " + "\""+ num1 + "\" is not a number!!"); inputCorrect = false; } } return num1; } public String calculatorChoice() throws IOException { System.out.println("Please select an option of what you would like to do with this number from the menu below and hit <ENTER>: "); System.out.println("\n*********************************************"); System.out.println("---------------------------------------------"); System.out.println("Please select an option from the list below: "); System.out.println("---------------------------------------------"); System.out.println("1 - Add"); System.out.println("2 - Subtract"); System.out.println("3 - Multiply"); System.out.println("4 - Divide (remainder included)"); System.out.println("5 - Maximum and minimum value of two numbers"); System.out.println("6 - Squareroot"); System.out.println("7 - Absolute value of numbers"); System.out.println("8 - Octal and Hexadecimal equivalent of numbers"); System.out.println("9 - Round numbers"); System.out.println("0 - Exit program"); System.out.println("**********************************************"); boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Please enter your option and hit <ENTER>: "); choice1 = inStream.readLine(); int c1 = Integer.parseInt(choice1); System.out.println("\nYou have entered choice number: " + c1); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("You did not enter a valid choice number: " + "\""+ choice1 + "\" is not in the list!!"); inputCorrect = false; } } return choice1; } public String inputNumber2() throws IOException { boolean inputCorrect = false; while (inputCorrect == false) { try { BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in)); System.out.print("\nPlease enter another number you want to do the calculation with and hit <ENTER>: "); num2 = br2.readLine(); double n2 = Double.parseDouble(num2); System.out.println("\nThe second number you have entered is: " + n2); System.out.println("\nYour numbers are: " + num1 + " and " + num2); inputCorrect = true; } catch (NumberFormatException nfe) { System.out.println("You did not enter a valid number: " + "\""+ num2 + "\" is not a number!!"); inputCorrect = false; } } return num2; } public int Calculator() { int choice2 = (int) Double.parseDouble(choice1); switch (choice2) { case 1 : Add(); System.out.print("The answer of " + num1 + " + " + num2 + " is: " + decimals.format(answer)); break; case 2 : Subtract(); System.out.print("The answer of " + num1 + " - " + num2 + " is: " + decimals.format(answer)); break; case 3 : Multiply(); System.out.print("The answer of " + num1 + " * " + num2 + " is: " + decimals.format(answer)); break; case 4 : Divide(); System.out.print("The answer of " + num1 + " / " + num2 + " is: " + decimals.format(answer)); Modulus(); System.out.print(" and the remainder is " + decimals.format(answer)); break; case 5 : maximumValue(); System.out.println("The maximum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer)); minimumValue(); System.out.println("The minimum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer)); break; case 6 : Squareroot1(); System.out.println("The squareroot of value " + num1 + " is: " + decimals.format(answer)); Squareroot2(); System.out.println("The squareroot of value " + num2 + " is: " + decimals.format(answer)); break; case 7 : absoluteNumber1(); System.out.println("The absolute number of " + num1 + " is: " + decimals.format(answer)); absoluteNumber2(); System.out.println("The absolute number of " + num2 + " is: " + decimals.format(answer)); break; case 8 : octalEquivalent1(); System.out.println("The octal equivalent of " + num1 + " is: " + decimals.format(answer)); hexadecimalEquivalent1(); System.out.println("The hexadecimal equivalent of " + num1 + " is: " + decimals.format(answer)); octalEquivalent2(); System.out.println("\nThe octal equivalent of " + num2 + " is: " + decimals.format(answer)); hexadecimalEquivalent2(); System.out.println("The hexadecimal equivalent of " + num2 + " is: " + decimals.format(answer)); break; case 9 : Round1(); System.out.println("The rounded number of " + num1 + " is: " + decimals.format(answer)); Round2(); System.out.println("The rounded number of " + num2 + " is: " + decimals.format(answer)); break; case 0 : if (choice2 == 0) { System.exit(1); } break; } return choice2; } public char anotherCalculation() throws IOException { try { System.out.print("\nWould you like to do another calculation? Y/N"); more = (char) System.in.read(); do { inputNumber1(); System.out.println(""); calculatorChoice(); System.out.println(""); inputNumber2(); System.out.println(""); Calculator(); System.out.println(""); anotherCalculation(); System.out.println(""); } while((more == 'y') | (more == 'Y')); System.exit(0); } catch (IOException ex){ System.exit(0); } return more; } public static void main(String[] args) throws IOException { Project p1 = new Project(); p1.insertNameAndSurname(); System.out.println(""); p1.inputNumber1(); System.out.println(""); p1.calculatorChoice(); System.out.println(""); p1.inputNumber2(); System.out.println(""); p1.Calculator(); System.out.println(""); p1.anotherCalculation(); System.out.println(""); } }
I would very much apreciate it.
Thanks
- 11-16-2008, 08:57 PM #14
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
Java Project
I tried to correct my code according to the errors I got while waiting for help, and my ' octalEquivalent(); ' method now works, I also changed my ' hexadecimalEquivalent(); ' method, but I get an error message that states
"Exception in thread "main" java.lang.NumberFormatException: For input string: "b"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at org.Java.Instructor.Project.hexadecimalEquivalent1 (Project.java:86)
at org.Java.Instructor.Project.Calculator(Project.jav a:271)
at org.Java.Instructor.Project.main(Project.java:329) "
By the looks of the error message I cannot have letters in the answer.
How can I correct this??
Please Help
Java Code:public double hexadecimalEquivalent1() { long n1 = Long.parseLong(num1); answer = Long.parseLong(Long.toHexString(n1)); return answer; }
- 11-17-2008, 04:00 PM #15
Look up a conversion algorithm. It's not as simple as just parsing it. You have to read the character, determine if its a letter or number, if its a number convert it to its decimal equivalent. Hex is base 16 which is 0-9 A B C D E F correct?
- 11-17-2008, 06:01 PM #16
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
[HOMEWORK] Please need help with java calculator project.
I've sorted out the problem about the octal and hex thanx, but now I'm stuck with another problem.
The other thing that I don't know how to correct is, when the user comes to the end of the program, the program asks the user if he/she wants to do another calculation.
I have created a method for this which is called " antoherCalculation(); " , what I want the method to do is, if the user answers ' y/Y ', it must ask the user again for a number and continue with the process as in the beginning (except for asking his/her name again!).
At the moment my program allows any input even only enter and continues on and on and on without stopping...
I want it to accept only answers ' y/Y ' or 'n/N' , when the user chooses e.g. 'y' the program must continue and when the user chooses 'n', the program must stop/exit, and display a message for the user, stating the current time, and calculate and display how long the user used the program.
Here is the code for the method ' anotherCalculation(); ' :
Please help me with this.Java Code:public char anotherCalculation() throws IOException { try { System.out.print("\nWould you like to do another calculation? Y/N"); more = (char) System.in.read(); do { inputNumber1(); System.out.println(""); calculatorChoice(); System.out.println(""); inputNumber2(); System.out.println(""); Calculator(); System.out.println(""); anotherCalculation(); System.out.println(""); } while((more == 'y') | (more == 'Y')); System.exit(0); } catch (IOException ex){ System.exit(0); } return more; }
Thanks
- 11-17-2008, 08:37 PM #17
Member
- Join Date
- Oct 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Java Project
By Smirre in forum New To JavaReplies: 7Last Post: 11-06-2008, 06:13 PM -
java project
By gvdn in forum Advanced JavaReplies: 4Last Post: 07-13-2008, 06:45 AM -
UML to Java project
By banie in forum NetBeansReplies: 3Last Post: 01-28-2008, 10:16 AM -
Help Java project.
By mandrake446 in forum New To JavaReplies: 1Last Post: 11-26-2007, 11:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks