Created
November 5, 2015 00:04
-
-
Save karellism/934e6a28b12650c65705 to your computer and use it in GitHub Desktop.
Input of a mix of types. From http://www.cs.utexas.edu/users/ndale/Scanner.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.Scanner; | |
public class MixedTypeInput { | |
public static void main(String[] args) { | |
double number; | |
Scanner in = new Scanner(System.in); | |
System.out.println("Enter your gross income: "); | |
if (in.hasNextInt()) { | |
number = (double)in.nextInt(); | |
System.out.println("You entered " + number); | |
} | |
else if (in.hasNextFloat()) { | |
number = (double)in.nextFloat(); | |
System.out.println("You entered " + number); | |
} | |
else if (in.hasNextDouble()) { | |
number = in.nextDouble(); | |
System.out.println("You entered " + number); | |
} | |
else | |
System.out.println("Token not an integer or a real value."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment