Java Code Examples for java.util.Scanner#nextBoolean()

The following examples show how to use java.util.Scanner#nextBoolean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Exercise_13_01.java    From Intro-to-Java-Programming with MIT License 6 votes vote down vote up
/** Main method */
public static void main(String[] args) {
	// Create a Scanner
	Scanner input = new Scanner(System.in);

	// Prompt the user to enter three sides of a triangle a color 
	// and a Boolean value to indicate whether the triangle is filled
	System.out.print("Enter three sides of the triangle: ");
	double side1 = input.nextDouble();
	double side2 = input.nextDouble();
	double side3 = input.nextDouble();

	System.out.print("Enter a color: ");
	String color = input.next();

	System.out.print("Is the triangle filled (true / false)? ");
	boolean filled = input.nextBoolean();

	// Create a Triangle
	Triangle triangle = new Triangle(side1, side2, side3, color, filled);

	System.out.println(triangle);
}
 
Example 2
Source File: PersistentData.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
public boolean getBool(String key, boolean def) {
  String str = readKeyFile(key);
  Scanner scan = new Scanner(str);
  if (scan.hasNextBoolean()) {
    return scan.nextBoolean();
  } else {
    return def;
  }
}
 
Example 3
Source File: NumberUtils.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @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();
}
 
Example 4
Source File: NumberUtils.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 5
Source File: DataCubeColumn.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 6
Source File: DataCubeDimension.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 7
Source File: SchemaBuilder.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);

    return sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 8
Source File: Country.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
public Country(Scanner in) {
    if (in.hasNextLine()) {
        this.name = in.next();
        this.population = in.nextInt();
        this.area = in.nextInt();
        this.landlocked = in.nextBoolean();
    }
}
 
Example 9
Source File: Exercise_11_01.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	// Create a Scanner object
	Scanner input = new Scanner(System.in);

	// Prompt the user to enter three sides of the triangle
	System.out.print("Enter three side of the triangle: ");
	double side1 = input.nextDouble();		
	double side2 = input.nextDouble();		
	double side3 = input.nextDouble();

	// Prompt the user to enter a color
	System.out.print("Enter a color: ");
	String color = input.next();

	// Prompt the user to enter whether the triangle is filled
	System.out.print("Is the triangle filled (true / false)? ");
	boolean filled = input.nextBoolean();

	// Create triangle object with user input
	Triangle triangle = new Triangle(side1, side2, side3);
	triangle.setColor(color);
	triangle.setFilled(filled);

	System.out.println(triangle.toString());
	System.out.println("Area: " + triangle.getArea());
	System.out.println("Perimeter: " + triangle.getPerimeter());
	System.out.println("Color: " + triangle.getColor());
	System.out.println("Triangle is" + (triangle.isFilled() ? "" : " not ") 
		+ "filled");
}
 
Example 10
Source File: Variable.java    From JOpenShowVar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
     * Parses a string encoded KRL variable to a JOpenShowVar Variable
     *
     * @param callback the Callback from the robot
     * @return the JOpenShowVar Variable
     * @throws NumberFormatException on parsing error
     */
    public static Variable parseVariable(Callback callback) throws NumberFormatException {
        String variable = callback.getVariableName();
        String value = callback.getStringValue();
        int id = callback.getId();
        long readTime = callback.getReadTime();
//        int option = callback.getOption();

        Scanner sc = new Scanner(value);
        Variable var;
        if (sc.hasNextInt()) {
            var = new Int(id, variable, sc.nextInt(), readTime);
            sc.close();
        } else if (sc.hasNextFloat()) {
            var = new Real(id, variable, (double) sc.nextFloat(), readTime);
            sc.close();
        } else if (sc.hasNextDouble()) {
            var = new Real(id, variable, sc.nextDouble(), readTime);
            sc.close();
        } else if (sc.hasNextBoolean()) {
            var = new Bool(id, variable, sc.nextBoolean(), readTime);
            sc.close();
        }else if (value.contains("#")) {
            var = new Enum(id, variable, sc.nextLine(), readTime);
            sc.close();
        } else if (value.contains("{")) {
            sc.close();
            var = new Struct(id, variable, Struct.parseString(value), readTime);
        } else {
            var = new Real(id, variable, Double.parseDouble(value), readTime);
            sc.close();
        }
        return var;
    }
 
Example 11
Source File: Struct.java    From JOpenShowVar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Parses a String encoded KRL Struct to a JOpenShowVar Struct
 * @param value the KRL String encoded Struct
 * @return a JOpenShowVar Struct 
 */
public static List<StructNode> parseString(String value) {
    if (value.contains(":")) {
        value = value.substring(value.indexOf(":") + 1, value.indexOf("}") + 1);
    } else {
        value = value.substring(value.indexOf("{") + 1, value.indexOf("}") + 1);
        value = " " + value;
    }
    value = value.replaceAll("\\{", "").replaceAll("\\}", "");
    List<StructNode> nodes = new ArrayList<>();
    String[] split1 = value.split(",");
    for (String str1 : split1) {
        String[] split2 = str1.split(" ");
        String name = split2[1];
        Object val;
        Scanner sc = new Scanner(split2[2]);
        if (sc.hasNextInt()) {
            val = sc.nextInt();
        } else if (sc.hasNextDouble()) {
            val = sc.nextDouble();
        } else if (sc.hasNextBoolean()) {
            val = sc.nextBoolean();
        } else {
            val = sc.next();
        }
        StructNode node = new StructNode(name, val);
        nodes.add(node);
    }
    return nodes;
}
 
Example 12
Source File: ConsoleReader.java    From JavaRushTasks with MIT License 4 votes vote down vote up
public static boolean readBoolean() throws Exception {
    //напишите тут ваш код
    Scanner sc = new Scanner(System.in);
    return sc.nextBoolean();

}
 
Example 13
Source File: Exercise_12_05.java    From Intro-to-Java-Programming with MIT License 4 votes vote down vote up
public static void main(String[] args) {
	// Create a Scanner object
	Scanner input = new Scanner(System.in);

	// Prompt the user to enter a color
	System.out.print("Enter a color: ");
	String color = input.next();

	// Prompt the user to enter whether the triangle is filled
	System.out.print("Is the triangle filled (true / false)? ");
	boolean filled = input.nextBoolean();

	boolean repeatSidesInput = true;
	do {
		// Prompt the user to enter three sides of the triangle
		System.out.print("Enter three side of the triangle: ");
		double side1 = input.nextDouble();		
		double side2 = input.nextDouble();		
		double side3 = input.nextDouble();

		try {
			// Create triangle object with user input
			Triangle triangle = new Triangle(side1, side2, side3);
			repeatSidesInput = false;	
			
			triangle.setColor(color);
			triangle.setFilled(filled);
	
			System.out.println(triangle.toString());
			System.out.println("Area: " + triangle.getArea());
			System.out.println("Perimeter: " + triangle.getPerimeter());
			System.out.println("Color: " + triangle.getColor());
			System.out.println("Triangle is" + (triangle.isFilled() ? "" : " not ") 
				+ "filled");
		}
		catch (IllegalTriangleException ex) {
			System.out.println(ex.getMessage());
		}

	} while (repeatSidesInput);
}