/** * Originally designed for Iris plants, could be used for any thing that follows this file format * * <p>double, double, double, double, string * * <p>Phase takes a scanner and create a list of QuickSubject. * * @param scanner * @return */ public static List<QuickSubject> phase(Scanner scanner) { List<QuickSubject> li = new ArrayList<>(); scanner.useDelimiter(" "); mainLoop: while (scanner.hasNext()) { while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; // ends loop } } double sepalLength = scanner.nextDouble(); while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; } } double sepalWidth = scanner.nextDouble(); while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; } } double petalLength = scanner.nextDouble(); while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; } } double petalWidth = scanner.nextDouble(); if (!scanner.hasNextLine()) { break; } String label = scanner.nextLine().substring(1); // System.out.println(sepalLength+" "+sepalWidth+" "+petalLength+" "+petalWidth+" "+label); li.add(new QuickSubject(sepalLength, sepalWidth, petalLength, petalWidth, label)); } scanner.close(); return li; }
/** * Like phase but does not expect a label to be given per object. * * @param scanner * @return */ public static List<QuickSubject> phaseWithOutLabel(Scanner scanner) { List<QuickSubject> li = new ArrayList<>(); scanner.useDelimiter(" "); mainLoop: while (scanner.hasNext()) { while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; } } double sepalLength = scanner.nextDouble(); while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; } } double sepalWidth = scanner.nextDouble(); while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; } } double petalLength = scanner.nextDouble(); while (!scanner.hasNextDouble() && !scanner.hasNextFloat()) { scanner.next(); if (!scanner.hasNext()) { break mainLoop; } } double petalWidth = scanner.nextDouble(); li.add(new QuickSubject(sepalLength, sepalWidth, petalLength, petalWidth)); } scanner.close(); return li; }
/** * parser for consumables, creates a consumable using the fields parsed earlier and parses in the * fields unique to consumables * * @param scan * @param pos * @param height * @param name * @param ID * @return * @throws ParserError */ private static Item parseConsumable(Scanner scan, Position pos, int height, String name, int ID) throws ParserError { int worth = parseInt(scan, "Worth"); if (!gobble(scan, "<BuffPercentage>")) { throw new ParserError("Parsing Consumable: Expecting <BuffPercentage>, got " + scan.next()); } if (!scan.hasNextFloat()) { throw new ParserError("Parsing Consumable: Expecting Float, got Nothing"); } Float buff = scan.nextFloat(); if (!gobble(scan, "</BuffPercentage>")) { throw new ParserError("Parsing Consumable: Expecting </BuffPercentage>, got " + scan.next()); } return new Consumables(pos, height, ID, name, worth, buff); }
public static void main(String[] args) { final int N = 5; int[] ints = new int[N]; float[] floats = new float[N]; Scanner input = new Scanner(System.in); int numInts = 0; int numFloats = 0; boolean cont = true; while (input.hasNext() && cont) { if (input.hasNextInt()) { if (numInts == N) { cont = false; } else { ints[numInts] = input.nextInt(); numInts++; } } else if (input.hasNextFloat()) { if (numFloats == N) { cont = false; } else { floats[numFloats] = input.nextFloat(); numFloats++; } } else { cont = false; } } System.out.println(""); System.out.print("Integers: "); for (int i = 0; i < numInts; i++) { System.out.print(ints[i] + " "); } System.out.println(""); System.out.print("Floats: "); for (int i = 0; i < numFloats; i++) { System.out.print(floats[i] + " "); } System.out.println("\n"); }
private void parseQMatrixElement(String input) { if (!input.startsWith("[")) { return; } Scanner scanner = new Scanner(input); QlearningTuple t; QLearningState s; QAction a = null; scanner.useDelimiter(Pattern.compile("[\\[\\],>(==)(\\s)+]")); int slicectr = 0; int vectorsize = 8; float result = 0; float[] slicevector = new float[8]; String action; Pattern p = Pattern.compile("\\S+"); while (scanner.hasNext()) { if (scanner.hasNext(p)) { if (scanner.hasNextFloat()) { if (slicectr >= vectorsize) result = scanner.nextFloat(); else slicevector[slicectr++] = scanner.nextFloat(); } else { action = scanner.next(); if (action.equals("ACTION_BACK")) a = new QAction(ActionType.ACTION_BACK); else if (action.equals("ACTION_RIGHT")) a = new QAction(ActionType.ACTION_RIGHT); else if (action.equals("ACTION_LEFT")) a = new QAction(ActionType.ACTION_LEFT); else if (action.equals("ACTION_NONE")) a = new QAction(ActionType.ACTION_NONE); else System.out.println("->" + scanner.next()); } } else scanner.next(); } s = new QLearningState(slicevector); t = new QlearningTuple(s, a); qMatrix.put(t, result); }
public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out, true); BigDecimal result; String str; // while there is some input to read while (in.hasNextFloat()) { result = in.nextBigDecimal(); int n = in.nextInt(); result = result.pow(n); str = result.toPlainString(); while (str.charAt(0) == '0') { str = str.substring(1); } int i = str.length() - 1; while (str.charAt(i--) == '0') { str = str.substring(0, str.length() - 1); } out.println(str); } }
public static Object parseValue(Database database, Object val, DataType type) { if (!(val instanceof String)) { return val; } int typeId = Integer.MIN_VALUE; if (type.getDataTypeId() != null) { typeId = type.getDataTypeId(); } String typeName = type.getTypeName(); LiquibaseDataType liquibaseDataType = DataTypeFactory.getInstance().from(type, database); String stringVal = (String) val; if (stringVal.isEmpty()) { if (liquibaseDataType instanceof CharType) { return ""; } else { return null; } } if (database instanceof OracleDatabase && !stringVal.startsWith("'") && !stringVal.endsWith("'")) { // oracle returns functions without quotes Object maybeDate = null; if (liquibaseDataType instanceof DateType || typeId == Types.DATE) { if (stringVal.endsWith("'HH24:MI:SS')")) { maybeDate = DataTypeFactory.getInstance() .fromDescription("time", database) .sqlToObject(stringVal, database); } else { maybeDate = DataTypeFactory.getInstance() .fromDescription("date", database) .sqlToObject(stringVal, database); } } else if (liquibaseDataType instanceof DateTimeType || typeId == Types.TIMESTAMP) { maybeDate = DataTypeFactory.getInstance() .fromDescription("datetime", database) .sqlToObject(stringVal, database); } else { return new DatabaseFunction(stringVal); } if (maybeDate != null) { if (maybeDate instanceof java.util.Date) { return maybeDate; } else { return new DatabaseFunction(stringVal); } } } if (stringVal.startsWith("'") && stringVal.endsWith("'")) { stringVal = stringVal.substring(1, stringVal.length() - 1); } else if (stringVal.startsWith("((") && stringVal.endsWith("))")) { stringVal = stringVal.substring(2, stringVal.length() - 2); } else if (stringVal.startsWith("('") && stringVal.endsWith("')")) { stringVal = stringVal.substring(2, stringVal.length() - 2); } else if (stringVal.startsWith("(") && stringVal.endsWith(")")) { return new DatabaseFunction(stringVal.substring(1, stringVal.length() - 1)); } Scanner scanner = new Scanner(stringVal.trim()); if (typeId == Types.ARRAY) { return new DatabaseFunction(stringVal); } else if ((liquibaseDataType instanceof BigIntType || typeId == Types.BIGINT)) { if (scanner.hasNextBigInteger()) { return scanner.nextBigInteger(); } else { return new DatabaseFunction(stringVal); } } else if (typeId == Types.BINARY) { return new DatabaseFunction(stringVal.trim()); } else if (typeId == Types.BIT) { if (stringVal.startsWith("b'")) { // mysql returns boolean values as b'0' and b'1' stringVal = stringVal.replaceFirst("b'", "").replaceFirst("'$", ""); } stringVal = stringVal.trim(); if (scanner.hasNextBoolean()) { return scanner.nextBoolean(); } else { return new Integer(stringVal); } } else if (liquibaseDataType instanceof BlobType || typeId == Types.BLOB) { return new DatabaseFunction(stringVal); } else if ((liquibaseDataType instanceof BooleanType || typeId == Types.BOOLEAN)) { if (scanner.hasNextBoolean()) { return scanner.nextBoolean(); } else { return new DatabaseFunction(stringVal); } } else if (liquibaseDataType instanceof CharType || typeId == Types.CHAR) { return stringVal; } else if (liquibaseDataType instanceof ClobType || typeId == Types.CLOB) { return stringVal; } else if (typeId == Types.DATALINK) { return new DatabaseFunction(stringVal); } else if (liquibaseDataType instanceof DateType || typeId == Types.DATE) { if (typeName.equalsIgnoreCase("year")) { return stringVal.trim(); } return DataTypeFactory.getInstance() .fromDescription("date", database) .sqlToObject(stringVal, database); } else if ((liquibaseDataType instanceof DecimalType || typeId == Types.DECIMAL)) { if (scanner.hasNextBigDecimal()) { return scanner.nextBigDecimal(); } else { return new DatabaseFunction(stringVal); } } else if (typeId == Types.DISTINCT) { return new DatabaseFunction(stringVal); } else if ((liquibaseDataType instanceof DoubleType || typeId == Types.DOUBLE)) { if (scanner.hasNextDouble()) { return scanner.nextDouble(); } else { return new DatabaseFunction(stringVal); } } else if ((liquibaseDataType instanceof FloatType || typeId == Types.FLOAT)) { if (scanner.hasNextFloat()) { return scanner.nextFloat(); } else { return new DatabaseFunction(stringVal); } } else if ((liquibaseDataType instanceof IntType || typeId == Types.INTEGER)) { if (scanner.hasNextInt()) { return scanner.nextInt(); } else { return new DatabaseFunction(stringVal); } } else if (typeId == Types.JAVA_OBJECT) { return new DatabaseFunction(stringVal); } else if (typeId == Types.LONGNVARCHAR) { return stringVal; } else if (typeId == Types.LONGVARBINARY) { return new DatabaseFunction(stringVal); } else if (typeId == Types.LONGVARCHAR) { return stringVal; } else if (liquibaseDataType instanceof NCharType || typeId == Types.NCHAR) { return stringVal; } else if (typeId == Types.NCLOB) { return stringVal; } else if (typeId == Types.NULL) { return null; } else if ((liquibaseDataType instanceof NumberType || typeId == Types.NUMERIC)) { if (scanner.hasNextBigDecimal()) { return scanner.nextBigDecimal(); } else { return new DatabaseFunction(stringVal); } } else if (liquibaseDataType instanceof NVarcharType || typeId == Types.NVARCHAR) { return stringVal; } else if (typeId == Types.OTHER) { if (database instanceof DB2Database && typeName.equalsIgnoreCase("DECFLOAT")) { return new BigDecimal(stringVal); } return new DatabaseFunction(stringVal); } else if (typeId == Types.REAL) { return new BigDecimal(stringVal.trim()); } else if (typeId == Types.REF) { return new DatabaseFunction(stringVal); } else if (typeId == Types.ROWID) { return new DatabaseFunction(stringVal); } else if ((liquibaseDataType instanceof SmallIntType || typeId == Types.SMALLINT)) { if (scanner.hasNextInt()) { return scanner.nextInt(); } else { return new DatabaseFunction(stringVal); } } else if (typeId == Types.SQLXML) { return new DatabaseFunction(stringVal); } else if (typeId == Types.STRUCT) { return new DatabaseFunction(stringVal); } else if (liquibaseDataType instanceof TimeType || typeId == Types.TIME) { return DataTypeFactory.getInstance() .fromDescription("time", database) .sqlToObject(stringVal, database); } else if (liquibaseDataType instanceof DateTimeType || liquibaseDataType instanceof TimestampType || typeId == Types.TIMESTAMP) { return DataTypeFactory.getInstance() .fromDescription("datetime", database) .sqlToObject(stringVal, database); } else if ((liquibaseDataType instanceof TinyIntType || typeId == Types.TINYINT)) { if (scanner.hasNextInt()) { return scanner.nextInt(); } else { return new DatabaseFunction(stringVal); } } else if (typeId == Types.VARBINARY) { return new DatabaseFunction(stringVal); } else if (liquibaseDataType instanceof VarcharType || typeId == Types.VARCHAR) { return stringVal; } else if (database instanceof MySQLDatabase && typeName.toLowerCase().startsWith("enum")) { return stringVal; } else { LogFactory.getLogger() .info( "Unknown default value: value '" + stringVal + "' type " + typeName + " (" + type + "), assuming it is a function"); return new DatabaseFunction(stringVal); } }