incomparable types: double and java.lang.String

awolarczuk
awolarczuk used Ask the Experts™
on
Hi all i cant understand why i am getting this error below i have used ti code so many times with no issues


C:\Users\awolarczuk\Desktop\UNE\Computer Science\assignments\Computer Science\Assignment P6>javac StringManip.java
StringManip.java:24: incomparable types: double and java.lang.String
               }  while (i != "" );// waiting for the user to exit the program with a -t
import java.util.Scanner; // Needed for the scanner class 
import java.text.DecimalFormat;




public class StringManip
{
	public static void main(String[] args)
    { 
		Double i;  // to get the keyboard input
		
		 
		
		//Create a Scabber object to read input
	       Scanner keyboard = new Scanner (System.in);
	       do
	       {
	    	   System.out.print ("\nPlease enter a line of text to search (enter to exit): ");  //get the input from the user from the keyboard
	    	   i = keyboard.nextDouble();
	       }  while (i != "" );// waiting for the user to exit the program enter
	       
    }
    



}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
You need to test for a String - you're calling nextDouble. You should be calling next()

Commented:
i is a double and can never be compared to an empty string.

try this:

} while (i != null);
try

try{
do
             {
                   System.out.print ("\nPlease enter a line of text to search (enter to exit): ");  //get the input from the user from the keyboard
                   i = keyboard.nextDouble();
             }  while ( true );// waiting for the user to exit the program enter
}catch( InputMismatchException ime ){
 System.out.println( "Exiting!" ) ;
}
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

keyboard.nextDouble();
will throw exception if the input can't be parsed to double, so in your case, enter without valid numeric would cause the exception be thrown!
Top Expert 2016

Commented:
You need something more like

        do {
            System.out.print(
                "\nPlease enter a line of text to search (enter to exit): "); //get the input from the user from the keyboard
            input = keyboard.nextLine();
        } while (!"".equals(input.trim())); // waiting for the user to exit the program enter

Open in new window

Top Expert 2016

Commented:
It doesn't look like you want a double input anyway, but if you did, you could do
i = Double.valueOf(input);

Open in new window

Commented:
Try using string input instead of a double. Like this:
public class StringManip
{
	public static void main(String[] args)
    { 
		String input;  // to get the keyboard input
		
		 
		
		//Create a Scabber object to read input
	       Scanner keyboard = new Scanner (System.in);
	       do
	       {
	    	   System.out.print ("\nPlease enter a line of text to search (enter to exit): ");  //get the input from the user from the keyboard
	    	   input = keyboard.nextLine();
	       }  while (!input.trim().equals("") );// waiting for the user to exit the program enter
	       
    }
}

Open in new window

Top Expert 2016

Commented:
>>Try using string input instead of a double.

Haven't i already suggested that..?

Commented:
>>Try using string input instead of a double.

>Haven't i already suggested that..?

Sorry, i didn't refresh the page before I posted...
Top Expert 2016

Commented:
awolarczuk, can you explain why you accepted a comment that simply repeated one i'd made earlier?

Author

Commented:
the comment was different from what i could ell and it was eaiser for me to understand
Top Expert 2016

Commented:
It's not different in any important way

Author

Commented:
Sorry mate i just wrked with the other one and it worked for me so i ddint go with yours i am sure they both work
Top Expert 2016

Commented:
OK. In future, you must understand that when you accept an answer, you must check it doesn't repeat an earlier answer - see site rules

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial