awolarczuk
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\Deskto p\UNE\Comp uter Science\assignments\Comput er 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
C:\Users\awolarczuk\Deskto
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
}
}
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 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!" ) ;
}
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!
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
It doesn't look like you want a double input anyway, but if you did, you could do
i = Double.valueOf(input);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Try using string input instead of a double.
Haven't i already suggested that..?
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...
>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?
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
ASKER
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