Results 1 to 5 of 5
- 05-02-2014, 07:43 AM #1
Member
- Join Date
- Feb 2014
- Posts
- 19
- Rep Power
- 0
Trouble with Converting String Input to Double
Hello World!
Hopefully my question is easy, here goes:
--------------------------------------------------------------------------------------------------------------------------------------------------------------
I am supposed to write a program on PuTTY using UNIX (I have Windows 8), but I am not comfortable with it yet, so I am using Java through NetBeans (IDE 7.4).
The program has to follow these instructions (ignore the Linux part of the instructions, the rest is in bold):
Write a program on the Linux system using the putty utility. The program should get strings of data from the command line (that is, look for the data in the “args” array of strings). Use a loop to convert each of the strings in the array into a double and add the number to a total. Print the total after all of the strings have been processed.
The program will use try-catch to catch any error that occurs. If an error occurs, the program will print a message saying that the error occurred. The program can end at that point.
You should create the Java program using the nano editor. The input data should be a list of numbers on the line that runs the program.
--------------------------------------------------------------------------------------------------------------------------------------------------------------
The problem so far is that I keep getting an error when converting a String value into a new double value.
Here is my code to this point,
*Note: I have yet to code the try-catch method in my program
Java Code:import java.util.*; public class Program13Strings { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("How many lines of data do you wish to enter?"); int size = scan.nextInt(); int i = 0; String [] array = new String [size]; double [] newArray = new double [size]; double total = 0; do { Scanner newScan = new Scanner (System.in); System.out.println("Enter a line of data, then press enter."); String newResponse = newScan.nextLine(); array [i] = newResponse; double newDouble = Double.valueOf(newResponse); newArray [i] = newDouble; total += newArray [i]; i++; } while (i < array.length); System.out.println("The total is " + total + "."); } // end main } // end class
*ERROR: Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241)
at java.lang.Double.valueOf(Double.java:504)
at program13strings.Program13Strings.main(Program13St rings.java:21)
Java Result: 1
How can this be fixed so I don't get this error?
Thanks in advance!!!!!!!
- 05-02-2014, 08:08 AM #2
Senior Member
- Join Date
- Feb 2014
- Posts
- 447
- Rep Power
- 7
Re: Trouble with Converting String Input to Double
Hi,
I am just wondering what double you expect from a string "abc".
When you input something that cannot be converted to a double, then you get such an exception. That is a normal behavior and that such errors can happen is also more or less given in the instructions.
Konrad
- 05-02-2014, 04:10 PM #3
Senior Member
- Join Date
- Feb 2014
- Posts
- 219
- Rep Power
- 7
Re: Trouble with Converting String Input to Double
I hope you're aware that your current implementation (the exception aside) is off the mark based on the instruction above. You're supposed to use data provided via command line arguments that are placed into the main method's args String array parameter, i.e., the "args" in main(String[] args).
You're expected to use try-catch. Therefore the exception that is thrown during the attempt to convert a String into a double is to be caught by the catch block. You are expected to print out a message about the error (exception), and your program can then exit. The error message can be the exception's built-in message (see getMessage(), or you can print a more user-friendly message yourself.
Suggested reading: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Again, this emphasises that the numbers are to be provided as command line arguments.
- 05-03-2014, 09:37 AM #4
Member
- Join Date
- Feb 2014
- Posts
- 19
- Rep Power
- 0
Re: Trouble with Converting String Input to Double
Your explanation really helped!
Thank you jashburn!
- 05-03-2014, 09:40 AM #5
Member
- Join Date
- Feb 2014
- Posts
- 19
- Rep Power
- 0
Re: Trouble with Converting String Input to Double
Konrad,
My brain stopped working when I input that data (caffeine shortage?). I completely forgot that a string can consist of numerical values if desired.
When I typed that I was thinking that I must input letters only and not numerical values.
Silly mistake!
Similar Threads
-
Converting Double to Int
By Rahim2312 in forum New To JavaReplies: 15Last Post: 05-02-2012, 06:58 PM -
Trouble! - Converting an User Input value to Array.
By derekdelgrosso in forum New To JavaReplies: 1Last Post: 02-07-2012, 05:58 PM -
Scanner.Next(Long/String/Double) - How do I skip the input?
By Illanair in forum New To JavaReplies: 2Last Post: 01-10-2012, 02:10 PM -
NullPointerException converting String to double
By infaddict in forum New To JavaReplies: 3Last Post: 07-19-2008, 07:01 PM -
Converting String to Double
By srini in forum New To JavaReplies: 1Last Post: 12-24-2007, 09:03 PM
Bookmarks