List of usage examples for java.util Scanner hasNextLine
public boolean hasNextLine()
From source file:com.shieldsbetter.sbomg.Cli.java
private static Object parseSbomgV1(Scanner s) throws OperationException { if (!s.hasNextLine()) { throw new OperationException("Empty."); }//from w ww .j a v a 2s . c o m String magicLine = s.nextLine(); if (!magicLine.startsWith("sbomg-")) { throw new OperationException("Input must begin with 'sbomg-'."); } String version = magicLine.substring("sbomg-".length()).trim(); if (version.isEmpty()) { throw new OperationException("Format version must follow 'sbomg-'."); } else if (!version.equals("v1")) { throw new OperationException("Unrecognized format version: " + version + ". Must be 'v1'."); } String modelDescriptorFormatSpecifier = expectLine(s, "Expecting model format specifier. Found EOF.") .trim(); if (!modelDescriptorFormatSpecifier.equals("yaml")) { throw new OperationException( "Model format specifier must be 'yaml'. Was: " + modelDescriptorFormatSpecifier); } String yamlBlock = expectBlock(s, "yaml"); Object modelDescriptorObject = new Yaml().load(yamlBlock); return modelDescriptorObject; }
From source file:Main.java
public static float[][] readMatrix(String path) throws FileNotFoundException { int n = 0, p = 0; System.err.println("Load matrix from " + path); Scanner sc = new Scanner(new BufferedReader(new FileReader(path))); while (sc.hasNext()) { String line = sc.nextLine(); if (line.startsWith("%")) { System.err.println(line); } else {//from w w w . j a va2 s .co m String[] dim = line.split(" "); n = Integer.parseInt(dim[0]); p = Integer.parseInt(dim[1]); System.err.println("num rows: " + n + "\n" + "num cols: " + p); break; } } int count = 0; float[][] mat = new float[p][n]; int row = 0, col = 0; while (sc.hasNextLine()) { String line = sc.nextLine(); if (line.isEmpty()) { break; } Float val = Float.parseFloat(line); mat[col][row] = val; row++; if (row == n) { row = 0; col++; } count++; } if (count != n * p) { System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p + "\n nun elements: " + count); return null; } System.err.println("Done."); return mat; }
From source file:Main.java
public static double[][] readMatrixDouble(String path) throws FileNotFoundException { int n = 0, p = 0; System.err.println("Load matrix from " + path); Scanner sc = new Scanner(new BufferedReader(new FileReader(path))); while (sc.hasNext()) { String line = sc.nextLine(); if (line.startsWith("%")) { System.err.println(line); } else {/*from w w w .j av a 2 s . co m*/ String[] dim = line.split(" "); n = Integer.parseInt(dim[0]); p = Integer.parseInt(dim[1]); System.err.println("num rows: " + n + "\n" + "num cols: " + p); break; } } int count = 0; double[][] mat = new double[p][n]; int row = 0, col = 0; while (sc.hasNextLine()) { String line = sc.nextLine(); if (line.isEmpty()) { break; } Double val = Double.parseDouble(line); mat[col][row] = val; row++; if (row == n) { row = 0; col++; } count++; } if (count != n * p) { System.err.println("Parse fail: not enough elements. num rows: " + n + "\n" + "num cols: " + p + "\n nun elements: " + count); return null; } System.err.println("Done."); return mat; }
From source file:io.selendroid.standalone.android.impl.DefaultAndroidEmulator.java
private static Map<String, Integer> mapDeviceNamesToSerial() { Map<String, Integer> mapping = new HashMap<String, Integer>(); CommandLine command = new CommandLine(AndroidSdk.adb()); command.addArgument("devices"); Scanner scanner; try {/*from w ww . j av a 2 s . c om*/ scanner = new Scanner(ShellCommand.exec(command)); } catch (ShellCommandException e) { return mapping; } while (scanner.hasNextLine()) { String line = scanner.nextLine(); Pattern pattern = Pattern.compile("emulator-\\d\\d\\d\\d"); Matcher matcher = pattern.matcher(line); if (matcher.find()) { String serial = matcher.group(0); Integer port = Integer.valueOf(serial.replaceAll("emulator-", "")); TelnetClient client = null; try { client = new TelnetClient(port); String avdName = client.sendCommand("avd name"); mapping.put(avdName, port); } catch (AndroidDeviceException e) { // ignore } finally { if (client != null) { client.close(); } } } } scanner.close(); return mapping; }
From source file:org.kalypso.kalypsomodel1d2d.conv.SWANDataConverterHelper.java
public static GM_Position readCoordinateShiftValues(final FileObject pFile) { GM_Position lPosRes = null;/*ww w. j av a 2s . c o m*/ Scanner scannerFile = null; Scanner scannerLine = null; try { FileObject swanShiftCoordFileObject = pFile.getParent() .getChild(ISimulation1D2DConstants.SIM_SWAN_COORD_SHIFT_FILE); if (swanShiftCoordFileObject == null) { return lPosRes; } File lFile = new File(swanShiftCoordFileObject.getURL().toURI()); scannerFile = new Scanner(lFile); Double lDoubleShiftY = null; Double lDoubleShiftX = null; while (scannerFile.hasNextLine()) { String lStrNextLine = scannerFile.nextLine(); if (lStrNextLine.contains("=")) { //$NON-NLS-1$ scannerLine = new Scanner(lStrNextLine); scannerLine.useDelimiter("="); //$NON-NLS-1$ String lStrValueName = scannerLine.next(); String lStrValue = scannerLine.next(); if (ISimulation1D2DConstants.SIM_SWAN_COORD_SHIFT_X.equalsIgnoreCase(lStrValueName)) { lDoubleShiftX = Double.parseDouble(lStrValue); } else if (ISimulation1D2DConstants.SIM_SWAN_COORD_SHIFT_Y.equalsIgnoreCase(lStrValueName)) { lDoubleShiftY = Double.parseDouble(lStrValue); } scannerLine.close(); } else { // System.out.println("Empty or invalid line. Unable to process. Processing the results without shift!"); } } if (lDoubleShiftX != null && lDoubleShiftY != null) lPosRes = GeometryFactory.createGM_Position(lDoubleShiftX, lDoubleShiftY); } catch (Exception e) { e.printStackTrace(); } finally { if (scannerFile != null) scannerFile.close(); if (scannerLine != null) scannerLine.close(); } return lPosRes; }
From source file:io.selendroid.android.impl.DefaultAndroidEmulator.java
private static Map<String, Integer> mapDeviceNamesToSerial() { Map<String, Integer> mapping = new HashMap<String, Integer>(); CommandLine command = new CommandLine(AndroidSdk.adb()); command.addArgument("devices"); Scanner scanner; try {//from w w w . jav a2 s .c o m scanner = new Scanner(ShellCommand.exec(command)); } catch (ShellCommandException e) { return mapping; } while (scanner.hasNextLine()) { String line = scanner.nextLine(); Pattern pattern = Pattern.compile("emulator-\\d\\d\\d\\d"); Matcher matcher = pattern.matcher(line); if (matcher.find()) { String serial = matcher.group(0); Integer port = Integer.valueOf(serial.replaceAll("emulator-", "")); TelnetClient client = null; try { client = new TelnetClient(port); String avdName = client.sendCommand("avd name"); mapping.put(avdName, port); } catch (AndroidDeviceException e) { // ignore } finally { if (client != null) { client.close(); } } Socket socket = null; PrintWriter out = null; BufferedReader in = null; try { socket = new Socket("127.0.0.1", port); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); if (in.readLine() == null) { throw new AndroidDeviceException("error"); } out.write("avd name\r\n"); out.flush(); in.readLine();// OK String avdName = in.readLine(); mapping.put(avdName, port); } catch (Exception e) { // ignore } finally { try { out.close(); in.close(); socket.close(); } catch (Exception e) { // do nothing } } } } scanner.close(); return mapping; }
From source file:com.shieldsbetter.sbomg.Cli.java
private static String expectLine(Scanner input, String failureMessage) throws OperationException { String result = null;//from w w w .ja va 2s. co m while ((result == null || result.isEmpty()) && input.hasNextLine()) { result = input.nextLine(); } if (result == null || result.isEmpty()) { throw new OperationException(failureMessage); } return result; }
From source file:com.shieldsbetter.sbomg.Cli.java
private static String expectBlock(Scanner input, String blockDesc) throws OperationException { String block = ""; String curLine = "dummy"; while (input.hasNextLine() && !curLine.isEmpty()) { curLine = input.nextLine();/* w w w . j av a 2 s .c om*/ if (curLine.startsWith("% ")) { block += curLine.substring(("% ".length())) + "\n"; } else { if (!curLine.trim().isEmpty()) { if (block.isEmpty()) { throw new OperationException("Expecting a " + blockDesc + "block line starting with '%' followed by a " + "space."); } else { throw new OperationException( "Text blocks must be " + "continued by block lines starting with '%' " + "followed by a space, or the block should be" + "terminated by EOF or a blank line."); } } } } if (block.isEmpty()) { throw new OperationException("Expecting a " + blockDesc + " block " + "starting with '%'. Found EOF."); } return block; }
From source file:etymology.config.CommandLineReader.java
public static void parseConfigFile(CommandLine cl, Configuration config) { String configFileName = cl.getOptionValue(CLOptions.CONFIG_FILE_OPTION); try {/*from w ww . j a v a 2s . c o m*/ if (null == configFileName) { return; } File configFile = new File(configFileName); if (!configFile.exists()) { //Does not exist System.err.println("Config file \"" + configFileName + "\" does not exist!"); System.exit(2); } //Parsing Scanner scanner = new Scanner(configFile); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.contains("#")) { line = line.substring(0, line.indexOf("#")); } line = line.trim(); if (0 == line.length()) { continue; } parseLine(line, config, cl); } } catch (IOException ex) { Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.amazonaws.auth.profile.internal.ProfilesConfigFileParser.java
/** * Loads the credentials from the given input stream. * * @param is input stream from where the profile details are read. * @throws IOException/* w w w. j av a2 s .co m*/ */ private static Map<String, Profile> loadProfiles(InputStream is) throws IOException { Scanner scanner = new Scanner(is); AWSCredentials credentials = null; String profileName = null; String accessKey = null; String secretKey = null; String line = null; boolean accessKeyRead = false; boolean secretKeyRead = false; ProfileCredentialScannerState scannerState = ProfileCredentialScannerState.READ_CONFIG_NAME; HashMap<String, Profile> profilesByName = new HashMap<String, Profile>(); try { while (scanner.hasNextLine()) { line = scanner.nextLine(); line = line.trim(); if (line.isEmpty()) continue; if (!line.startsWith("[") && !line.startsWith(AWS_ACCESS_KEY_ID) && !line.startsWith(AWS_SECRET_ACCESS_KEY)) { LOG.info("Unsupported configuration setting: " + line); continue; } switch (scannerState) { case READ_CONFIG_NAME: profileName = parseProfileName(line); scannerState = ProfileCredentialScannerState.READ_KEY; break; case READ_KEY: if (line.startsWith(AWS_ACCESS_KEY_ID) && !accessKeyRead) { accessKey = parseAccessKey(line); accessKeyRead = true; } else if (!secretKeyRead) { secretKey = parseSecretKey(line); secretKeyRead = true; } else { throw new AmazonClientException( "Unable to load Amazon AWS Credentials. File not in proper format."); } break; } if (accessKeyRead && secretKeyRead) { assertParameterNotEmpty(profileName, "Unable to load credentials into profile. ProfileName is empty. " + line); assertParameterNotEmpty(accessKey, "Unable to load credentials into profile. AWS Access Key ID is empty. " + line); assertParameterNotEmpty(secretKey, "Unable to load credentials into profile. AWS Secret Access Key is empty. " + line); credentials = new BasicAWSCredentials(accessKey, secretKey); profilesByName.put(profileName, new Profile(credentials)); scannerState = ProfileCredentialScannerState.READ_CONFIG_NAME; accessKeyRead = false; secretKeyRead = false; } } if (scannerState != ProfileCredentialScannerState.READ_CONFIG_NAME || accessKeyRead || secretKeyRead) { throw new AmazonClientException( "Unable to load credentials into profile. Profile Name or AWS Access Key ID or AWS Secret Access Key missing for a profile."); } } finally { scanner.close(); } return profilesByName; }