Since you're using nextFloat() you must be sure that you enter a floating number, otherwise clear the scanner with next()
public static void main(String[] args) throws Exception {
while (true) {
System.out.print("Enter a float: ");
try {
float myFloat = input.nextFloat();
if (myFloat % 1 == 0) {
throw new Exception("Wrong type");
}
System.out.println(myFloat);
} catch (InputMismatchException ime) {
System.out.println(ime.toString());
input.next(); // Flush the buffer from all data
}
}
}
Results:

UPDATE
You still have to handle the InputMismatchException, just throw your own exception in the catch block.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// while (true) just for testing
while (true) {
try {
System.out.print("Enter a float: ");
System.out.println(CheckFloat(input));
} catch (MyException me) {
System.out.println(me.toString());
}
}
}
private static float CheckFloat(Scanner sc) throws MyException {
try {
float input = sc.nextFloat();
if (input % 1 == 0) {
throw new MyException("Wrong type");
} else {
return input;
}
} catch (InputMismatchException ime) {
sc.next(); // Flush the scanner
// Rethrow your own exception
throw new MyException("Wrong type");
}
}
private static class MyException extends Exception {
// You exception details
public MyException(String message) {
super(message);
}
}
Results:

Here is what's going on.
When the you use the hasNext(Pattern) method, the scanner looks at its complete next token and decides whether that complete token matches the pattern or not.
When the delimiter is empty, it means that the complete next token is a single character. You can see that if you try to use String.split() with an empty pattern.
So, when you enter 123.4.5, what hasNext() actually sees is just the 1. Luckily, that matches your pattern, so you get into the body of the if.
At this point, you are using findInLine(pattern). This method disregards delimiters and tokens, and instead simply looks for a matching pattern. So it sees the whole 123.4 and gives that to you.
Now that you are left with .5, the next complete token is just the .! This doesn't match the pattern (your pattern says that if there is a . it has to be followed by at least one digit. A single dot doesn't match). Therefore, the hasNext(integerPattern) fails, and you get to the else part.
Here is a possible solution: have different patterns for the hasNext and for the findInLine:
Pattern findPattern = Pattern.compile("\\d*(\\.\\d+)?");
Pattern tokenPattern = Pattern.compile("\\d|\\.(?=\\d)");
The tokenPattern has a positive look-ahead which means that it will accept a single-character token that is either:
- A digit
- A dot - provided that there are digits after it, though they are not in the match.
If you have a single char that matches these criteria you know you'll be able to match the full pattern. So your program changes to:
while (interpreter.hasNext()) {
// Do we have the beginning of a number?
if (interpreter.hasNext(tokenPattern)) {
// Extract the full number
String strVal = interpreter.findInLine(findPattern);
float value = Float.parseFloat(strVal);
tokenList.add(new Token(11, value));
}
else{
// Handle single char token
}
}
This will scan in the next float.
Scanner interpreter = new Scanner(input);
while(interpreter.hasNextFloat()){
tokenList.add(newToken(11, scanner.nextFloat()));
}
You need to import java.util.Scanner class. See below code
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number");
float number = scan.nextFloat();
}
}
This will compile,just check your imports.
import java.util.Scanner;