Example usage for java.util Scanner hasNextLine

List of usage examples for java.util Scanner hasNextLine

Introduction

In this page you can find the example usage for java.util Scanner hasNextLine.

Prototype

public boolean hasNextLine() 

Source Link

Document

Returns true if there is another line in the input of this scanner.

Usage

From source file:com.att.voice.AmazonEchoApi.java

public boolean checkItemId(String itemId) throws IOException {
    File file = new File("Items.txt");
    boolean ret = false;
    try {// w  ww  .  j  a v a2s  . co m
        if (!file.exists()) {
            file.createNewFile();
        }
        Scanner scanner = new Scanner(file);
        int lineNum = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            lineNum++;
            if (line.contains(itemId)) {
                ret = true;
            }
        }
    } catch (Exception e) {
        ret = false;
    }
    return ret;
}

From source file:com.x460dot10.b.registrar.StartupManager.java

/**
 * Imports data/courses.txt into <code>University.courses</code>
 *
 * @return             Indicates import of courses was successful
 * @throws IOException // w  ww. jav  a 2 s  . c  o  m
 */
public void importCourses() throws IOException {
    //Boolean importCoursesSuccessful = true;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader("data/students.txt"));
        Scanner scanner = new Scanner(reader);
        while (scanner.hasNextLine()) {
            //String line = scanner.nextLine();
            //uni.addStudent(Student.parseStudent(line));
        }
    } catch (Exception ex) {
        // TODO send error message to a log file
        System.err.println("Error: " + ex.getMessage());
        //importCoursesSuccessful = false;
    } finally {
        if (reader != null)
            reader.close();
    }
}

From source file:com.x460dot10.b.registrar.StartupManager.java

/**
 * Imports data/registrations.txt into <code>University.registrations</code>
 *
 * @return             Indicates import of registration data was successful
 * @throws IOException //ww w.  java 2s . c  o  m
 */
public boolean importRegistrations() throws IOException {
    //Boolean importRegistrationsSuccessful = true;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader("data/students.txt"));
        Scanner scanner = new Scanner(reader);
        while (scanner.hasNextLine()) {
            //String line = scanner.nextLine();
            //uni.addStudent(Student.parseStudent(line));
        }
    } catch (Exception ex) {
        // TODO send error message to a log file
        System.err.println("Error: " + ex.getMessage());
        //importRegistrationsSuccessful = false;
    } finally {
        if (reader != null)
            reader.close();
    }
    return false;
}

From source file:com.btobits.automator.fix.utils.FileMessageFactory.java

public void loadInternal(ITcpProccesor inProcessor, int repeatDelay, String nameClient, int inTimeOut)
        throws Exception {
    try {//from   w  w w.  j  av a 2 s.  c  om
        Scanner scanner = new Scanner(new FileInputStream(file));
        //scanner.useDelimiter(delimeter);

        while (scanner.hasNextLine()) {
            final String line = scanner.nextLine();
            inProcessor.sendMessage(new ITcpMessage() {
                public byte[] toRawMessage() {
                    return line.getBytes();
                }

                public String getMessageAsString() {
                    return line;
                }
            }, inTimeOut);

            inProcessor.log(line, false);
            if (repeatDelay > 0) {
                try {
                    Thread.sleep(repeatDelay);
                } catch (Exception e) {
                }
            }
        }
    } catch (SocketException ex) {
        throw new BuildException(ex.getMessage());
    } catch (Exception ex) {
        throw new Exception("Error send data from file, error: " + ex.getMessage(), ex);
    }
}

From source file:org.apache.accumulo.core.client.mock.MockShell.java

public int start() throws IOException {
    if (configError)
        return 1;

    String input;/*  w  w  w.  j a v  a 2 s.  c om*/
    if (isVerbose())
        printInfo();

    if (execFile != null) {
        java.util.Scanner scanner = new java.util.Scanner(new File(execFile));
        try {
            while (scanner.hasNextLine() && !hasExited()) {
                execCommand(scanner.nextLine(), true, isVerbose());
            }
        } finally {
            scanner.close();
        }
    } else if (execCommand != null) {
        for (String command : execCommand.split("\n")) {
            execCommand(command, true, isVerbose());
        }
        return exitCode;
    }

    while (true) {
        if (hasExited())
            return exitCode;

        reader.setDefaultPrompt(getDefaultPrompt());
        input = reader.readLine();
        if (input == null) {
            reader.printNewline();
            return exitCode;
        } // user canceled

        execCommand(input, false, false);
    }
}

From source file:com.joliciel.talismane.machineLearning.TextFileMultivaluedResource.java

public TextFileMultivaluedResource(File file) {
    try {//from   w  w w .  j a  v  a 2 s.c  om
        this.name = file.getName();

        Scanner scanner = new Scanner(file);
        int numParts = -1;
        int i = 1;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.length() > 0 && !line.startsWith("#")) {
                StringBuilder sb = new StringBuilder();
                String[] parts = line.split("\t");
                if (parts.length == 1 && line.startsWith("Name: ")) {
                    this.name = line.substring("Name: ".length());
                    i++;
                    continue;
                }
                if (parts.length == 1 && line.startsWith("Multivalued: ")) {
                    boolean multivalued = line.substring("Multivalued: ".length()).equalsIgnoreCase("true");
                    if (!multivalued)
                        throw new JolicielException("Expected multivalued resource");
                    i++;
                    continue;
                }
                if (numParts < 0)
                    numParts = parts.length;
                if (parts.length != numParts)
                    throw new JolicielException(
                            "Wrong number of elements on line " + i + " in file: " + file.getName());

                for (int j = 0; j < numParts - 2; j++) {
                    sb.append(parts[j]);
                    sb.append("|");
                }
                String key = sb.toString();
                List<WeightedOutcome<String>> resultList = resultsMap.get(key);
                if (resultList == null) {
                    resultList = new ArrayList<WeightedOutcome<String>>(1);
                    resultsMap.put(key, resultList);
                }
                String outcome = parts[numParts - 2];
                double weight = Double.parseDouble(parts[numParts - 1]);
                resultList.add(new WeightedOutcome<String>(outcome, weight));

            }
            i++;
        }
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:org.amanzi.asn1.parser.token.TokenAnalyzerTest.java

private List<IToken> getEtalonListOfTokens(URL outputResource) throws IOException {
    List<IToken> tokens = new ArrayList<IToken>();

    Scanner scanner = new Scanner(outputResource.openStream());

    while (scanner.hasNextLine()) {
        tokens.add(new SimpleToken(scanner.nextLine()));
    }/*w  w w . ja  v a  2  s  . co  m*/

    return tokens;
}

From source file:com.yahoo.ycsb.bulk.hbase.RangePartitioner.java

private synchronized Text[] getCutPoints() throws IOException {
    if (cutPointArray == null) {
        String cutFileName = conf.get(CUTFILE_KEY);
        Path[] cf = DistributedCache.getLocalCacheFiles(conf);

        if (cf != null) {
            for (Path path : cf) {
                if (path.toUri().getPath().endsWith(cutFileName.substring(cutFileName.lastIndexOf('/')))) {
                    TreeSet<Text> cutPoints = new TreeSet<Text>();
                    Scanner in = new Scanner(new BufferedReader(new FileReader(path.toString())));
                    try {
                        while (in.hasNextLine())
                            cutPoints.add(new Text(Base64.decodeBase64(in.nextLine().getBytes())));
                    } finally {
                        in.close();//from   w  ww . j  a  va 2 s  .c o  m
                    }
                    cutPointArray = cutPoints.toArray(new Text[cutPoints.size()]);
                    break;
                }
            }
        }
        if (cutPointArray == null)
            throw new FileNotFoundException(cutFileName + " not found in distributed cache");
    }
    return cutPointArray;
}

From source file:com.mapr.db.utils.ImportCSV.java

public void readSchema(String path) {

    String schemaLine = "";
    StringTokenizer st;//from  w ww  .  j a v  a  2 s.  c  o  m
    String column = "", datatype = "";

    countColumnsInSchema = 0;

    try {
        Scanner scan = new Scanner(new FileReader(path));

        while (scan.hasNextLine()) {
            schemaLine = scan.nextLine().trim();
            if (schemaLine.startsWith("#"))
                continue;
            st = new StringTokenizer(schemaLine, " ");
            while (st.hasMoreTokens()) {
                column = st.nextToken();
                datatype = st.nextToken();
                valueTypesInSchema.add(countColumnsInSchema, datatype);
                columnNamesInSchema.add(countColumnsInSchema, column);
            }
            countColumnsInSchema++;
        }
        scan.close();
    } catch (Exception e) {
        System.out.println("Error reading schema: " + schemaLine + "\n(Column Name:" + column + "\tData type:"
                + datatype + ")");
        e.printStackTrace();
        System.exit(-1);
    }
}

From source file:com.intel.hadoop.graphbuilder.demoapps.wikipedia.docwordgraph.WordCountGraphTokenizer.java

private void loadDictionary(String path) throws IOException {
    FileStatus[] stats = fs.listStatus(new Path(path));
    dictionary = new HashSet<String>();
    for (FileStatus stat : stats) {
        LOG.debug(("Load dictionary: " + stat.getPath().getName()));
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(fs.open(stat.getPath()))));
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            dictionary.add(line);//from   ww w . j a  v a 2  s .  c o m
        }
    }
}