Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 java.lang.NumberFormatException: empty String?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
a22asin




PostPosted: Wed Jan 22, 2014 6:35 pm   Post subject: java.lang.NumberFormatException: empty String?

Hey, ok so i keep getting this error for my program when asking the user for the value of x:

Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at question2.PolynomialTest.main(PolynomialTest.java:25)



coming from this part of my code:

String x;
do{
System.out.print ("Enter a value of x for which to evaluate the polynomial: ");
x = key.nextLine();
if (!x.equals("quit")){
double in = Double.parseDouble(x);
double result = p.evaluate(in);
System.out.println ("For x = " + x +", polynomial =" + result);
}
}while(!x.equals("quit"));


The Program will run right till it asks for an input for x, it wont let me input a value, it just gives the error. I tried initializing x as a random string at the start since the error says "empty string", i tried making "quit" a variable and comparing it to x instead. But nothing seems to work and i cant figure out why im getting this. Any ideas? oh and line 25 would be when i parse the double.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Jan 22, 2014 10:26 pm   Post subject: RE:java.lang.NumberFormatException: empty String?

"Empty string" is different from "null string". Empty means "no content", null means "no value".

So after x = key.nextLine(); , x isn't null, it's just empty. Without seeing how 'key' is set up, I can't tell you anything more about why that would be the case (don't forget: the 'nextLine()' method will return an empty string if it encounters an extra line-break in its input).
a22asin




PostPosted: Wed Jan 22, 2014 10:34 pm   Post subject: Re: RE:java.lang.NumberFormatException: empty String?

DemonWasp @ Wed Jan 22, 2014 10:26 pm wrote:
"Empty string" is different from "null string". Empty means "no content", null means "no value".

So after x = key.nextLine(); , x isn't null, it's just empty. Without seeing how 'key' is set up, I can't tell you anything more about why that would be the case (don't forget: the 'nextLine()' method will return an empty string if it encounters an extra line-break in its input).


Well the error occurs before i have a chance to enter a value for x. The key is the scanner object and works ealier in the code, so thats why i dont get whats wrong. Heres my entire code as maybe something may have been wrong in between.

code:
package question2;

import java.util.*;

public class PolynomialTest {
        public static void main(String[] args){

                Scanner key = new Scanner (System.in); 
                       
                System.out.print ("Enter polynomial degree: ");
                int index = key.nextInt();
                Polynomial p = new Polynomial(index);
               
                for (int i = 0; i <= index; i++){
                        System.out.print ("Enter polynomial degree " + i + ": ");
                        p.setCoefficient(i, key.nextDouble()); 
                }
               
                System.out.println(p);
                String x;
                do{
                        System.out.print ("Enter a value of x for which to evaluate the polynomial: ");
                        x = key.nextLine();
                        if (!x.equals("quit")){
                                double in = Double.parseDouble(x);
                                double result = p.evaluate(in);
                                System.out.println ("For x = " + x +", polynomial =" + result);
                        }
                }while(!x.equals("quit"));
        }

}
Tony




PostPosted: Wed Jan 22, 2014 11:21 pm   Post subject: RE:java.lang.NumberFormatException: empty String?

Think about the difference between
code:

int index = key.nextInt();

and
code:

x = key.nextLine();


hint: read the documentation.
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt()
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine()
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: