File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes [HOMEWORK] Please need help with java project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[HOMEWORK] Please need help with java project" Watch "[HOMEWORK] Please need help with java project" New topic
Author

[HOMEWORK] Please need help with java project

Alex Reynders
Greenhorn

Joined: Nov 16, 2008
Posts: 13
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
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

Please fix your code -- the topic, as posted, is not readable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

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

Joined: Oct 13, 2005
Posts: 40046
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [HOMEWORK] Please need help with java project