Scanner sc = new Scanner(System.in); while (!sc.hasNext("exit")) { System.out.println( sc.hasNextInt() ? "(int) " + sc.nextInt() : sc.hasNextLong() ? "(long) " + sc.nextLong() : sc.hasNextDouble() ? "(double) " + sc.nextDouble() : sc.hasNextBoolean() ? "(boolean) " + sc.nextBoolean() : "(String) " + sc.next() ); }
while(true) { Scanner keyboard = new Scanner(System.in); System.out.println("Would you like to try again? (true/false)"); if(keyboard.hasNextBoolean()){ continueGame = keyboard.nextBoolean(); System.out.println(); break; } }
@Override public boolean hasBoolean() { return getHandle().hasNextBoolean(); }
@Override public boolean hasBoolean() { return getHandle().hasNextBoolean(); }
public static boolean readNextBoolean(Scanner scanner, boolean defaultValue) { try { while (!scanner.hasNextBoolean()) scanner.next(); return scanner.nextBoolean(); } catch (NoSuchElementException e) { return defaultValue; } } }
/** * @param str string to check * @return whether the string is a booelan type */ public static boolean isBoolean(String str) { if (str == null) { return false; } Scanner s = new Scanner(str.trim()); if (!s.hasNextBoolean()) { return false; } s.nextBoolean(); return !s.hasNext(); }
/** * Parse a boolean with a default value. * * @param str string to parse * @param def default boolean value * @return parsed boolean */ public static Boolean parseBoolean(String str, Boolean def) { if (str == null) { return def; } Scanner scnr = new Scanner(str); if (!scnr.hasNextBoolean()) { return def; } return scnr.nextBoolean(); }
String input = " 1 true foo 2 false bar 3 "; Scanner sc = new Scanner(input); while (sc.hasNext()) { if (sc.hasNextInt()) { System.out.println("(int) " + sc.nextInt()); } else if (sc.hasNextBoolean()) { System.out.println("(boolean) " + sc.nextBoolean()); } else { System.out.println(sc.next()); } }
String[] testValues = { "123", "123.4", "FALSE", "false", "some strings" }; for (String test : testValues) { Scanner scanner = new Scanner(test); scanner.useDelimiter("^"); if (scanner.hasNextBoolean()) { boolean value = scanner.nextBoolean(); System.out.println("boolean " + value); } else if (scanner.hasNextDouble()) { double value = scanner.nextDouble(); System.out.println("number " + value); } else if (scanner.hasNext(".*")) { String value = scanner.next(); System.out.println("string " + value); } }
import java.util.Scanner; public class DataType { public static void main(String[] args) { Scanner in = new Scanner(System.in); if(in.hasNextByte(2)) System.out.println("Byte"); else if(in.hasNextInt()) System.out.println("Integer"); else if(in.hasNextFloat()) System.out.println("Float"); else if(in.hasNextBoolean()) System.out.println("Boolean"); else if(in.hasNext()) System.out.println("String"); } }
public static void main(String [] args){ ArrayList<String> randomTextlist = new ArrayList<String>(); ArrayList<Double> doublelist = new ArrayList<Double>(); ArrayList<Integer> intlist = new ArrayList<Integer>(); boolean input = true; Scanner scan = new Scanner(System.in); while (input){ System.out.print("Input string, int, or double."); //exit loop case: input false, or how would you exit the loop? if (scan.hasNextBoolean()){ break; } if (scan.hasNextDouble()){ doublelist.add(scan.nextDouble()); } else if (scan.hasNextInt()){ intlist.add(scan.nextInt()); } else{ randomTextlist.add(scan.next()); } } Collections.sort(doublelist); System.out.println(doublelist); Collections.sort(intlist); System.out.println(intlist); Collections.sort(randomTextlist); System.out.println(randomTextlist); }
if ( input.hasNextBoolean() ) { isQuit = input.nextBoolean(); } else {
int i; double d; boolean b; String str; Scanner s = new Scanner(System.in); while (s.hasNext()) { if (s.hasNextInt()) { i = s.nextInt(); System.out.println("int: " + i); } else if (s.hasNextDouble()) { d = s.nextDouble(); System.out.println("double: " + d); } else if (s.hasNextBoolean()) { b = s.nextBoolean(); System.out.println("boolean: " + b); } else { str = s.next(); System.out.println("String: " + str); } }
else if(keyboard.hasNextInt()) return new TLValue(Integer.valueOf(keyboard.nextInt())); else if(keyboard.hasNextBoolean()) return new TLValue(Boolean.valueOf(keyboard.nextBoolean())); else
output("You entered a BigInteger = %s", next); else if (sis.hasNextBoolean())
public static IntersectionEstimationParameters parse(String parametersAsString) { parametersAsString = parametersAsString.replace(",", ""); Scanner scanner = new Scanner(parametersAsString); while (!scanner.hasNextDouble()) scanner.next(); double maxDistanceToRegion = scanner.nextDouble(); while (!scanner.hasNextInt()) scanner.next(); int minRegionSize = scanner.nextInt(); while (!scanner.hasNextDouble()) scanner.next(); double minIntersectionLength = scanner.nextDouble(); while (!scanner.hasNextDouble()) scanner.next(); double minRegionAngleDifference = scanner.nextDouble(); while (!scanner.hasNextBoolean()) scanner.next(); boolean addIntersectionsToRegions = scanner.nextBoolean(); scanner.close(); IntersectionEstimationParameters parameters = new IntersectionEstimationParameters(); parameters.setMaxDistanceToRegion(maxDistanceToRegion); parameters.setMinRegionSize(minRegionSize); parameters.setMinIntersectionLength(minIntersectionLength); parameters.setMinRegionAngleDifference(minRegionAngleDifference); parameters.setAddIntersectionsToRegions(addIntersectionsToRegions); return parameters; } }