Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[HOMEWORK] Please need help with java project

 
Alex Reynders
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need help with a java project I need to do. Here is the project specifications :

"""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 am stuck at the moment and don't know how to correct this problem.

I have created a method ' hexadecimalEquivalent(); ' (among others...) which if the user chooses, it converts the numbers he/she provided as input to the hexadecimal equivalent, my ' octalEquivalent(); ' method works but I get an error message that states " Exception in thread "main" java.lang.NumberFormatException: For input string: "b"
at java.lang.NumberFormatException.forInputString(Unknown 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.java:271)
at org.Java.Instructor.Project.main(Project.java:329)
"

Can anyone please help me.

Here is the code that I have done so far for my Project.class:



Your help will be much appreciated.
Thanks


Edit by mw: Formatted code (which was originally posted as a single line. ).
[ November 16, 2008: Message edited by: marc weber ]
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please fix your code -- the topic, as posted, is not readable.

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
Please fix your code -- the topic, as posted, is not readable...

Because this is a first-time poster, and I'm feeling especially helpful today (and putting off making a phone call that I really don't want to make), I fixed this post.

 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is here...

Exception in thread "main" java.lang.NumberFormatException: For input string: "b"
at java.lang.NumberFormatException.forInputString(Unknown 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.java:271)
at org.Java.Instructor.Project.main(Project.java:329)


On line 86 of your Project.java program, in you hexadecimalEquivalent1() method, you make a call to parseLong() method (which parses a decimal string to long). This method passes a string with a "b", which is not a valid character for a decimal number.

Following the code, it is likely here....



You are taking a long, converting it to a hex string, and then trying to use that hex string as a decimal string to convert it back to the long. Besides the fact that the acrobatics being done don't make sense, the toHexString() method will create string, that won't be parsable as a decimal string.

Henry
[ November 16, 2008: Message edited by: Henry Wong ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Thank you, Marc, for adding the code tags.

The NumberFormatException means that whatever is entered cannot be converted to a number. Now you can easily convert "b" to a hexadecimal number, but you can't convert it to decimal or octal or binary. So you have to avoid any such input when you aren't working in hexadecimal (more precisely duodecimal or higher for "b").

You are using some very old-fashioned code. You will probably find it easier to use the %f tags rather than DecimalFormat, and to use java.util.Scanner instead of BufferedReader. What is more, if you use Scanner, you don't need to bother declaring any Exceptions because Scanner only throws an (unchecked) InputMismatchException (if I remember correctly). For details of the % tags look here and here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic