Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default 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
    *NOTE: right where my code says double newDouble = Double.valueOf(newResponse); is where the error is occurring.

    *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!!!!!!!

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default 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

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Trouble with Converting String Input to Double

    Quote Originally Posted by NoviceJavaNoob View Post
    The program should get strings of data from the command line (that is, look for the data in the “args” array of strings).
    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).

    Quote Originally Posted by NoviceJavaNoob View Post
    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'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)

    Quote Originally Posted by NoviceJavaNoob View Post
    The input data should be a list of numbers on the line that runs the program.
    Again, this emphasises that the numbers are to be provided as command line arguments.

  4. #4
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Re: Trouble with Converting String Input to Double

    Your explanation really helped!

    Thank you jashburn!

  5. #5
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default 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

  1. Converting Double to Int
    By Rahim2312 in forum New To Java
    Replies: 15
    Last Post: 05-02-2012, 05:58 PM
  2. Trouble! - Converting an User Input value to Array.
    By derekdelgrosso in forum New To Java
    Replies: 1
    Last Post: 02-07-2012, 04:58 PM
  3. Replies: 2
    Last Post: 01-10-2012, 01:10 PM
  4. NullPointerException converting String to double
    By infaddict in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 06:01 PM
  5. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •