Link to home
Start Free TrialLog in
Avatar of awolarczuk
awolarczukFlag for Australia

asked on

incomparable types: double and java.lang.String

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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!" ) ;
}
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!
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

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

ASKER CERTIFIED SOLUTION
Avatar of jstakk
jstakk
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Try using string input instead of a double.

Haven't i already suggested that..?
>>Try using string input instead of a double.

>Haven't i already suggested that..?

Sorry, i didn't refresh the page before I posted...
awolarczuk, can you explain why you accepted a comment that simply repeated one i'd made earlier?
Avatar of awolarczuk

ASKER

the comment was different from what i could ell and it was eaiser for me to understand
It's not different in any important way
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
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