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:net.bashtech.geobot.BotManager.java

public static String postRemoteDataStrawpoll(String urlString) {

    String line = "";
    try {//from w  w  w . j  a va 2 s.c  om
        HttpURLConnection c = (HttpURLConnection) (new URL("http://strawpoll.me/api/v2/polls")
                .openConnection());

        c.setRequestMethod("POST");
        c.setRequestProperty("Content-Type", "application/json");
        c.setRequestProperty("User-Agent", "CB2");

        c.setDoOutput(true);
        c.setDoInput(true);
        c.setUseCaches(false);

        String queryString = urlString;

        c.setRequestProperty("Content-Length", Integer.toString(queryString.length()));

        DataOutputStream wr = new DataOutputStream(c.getOutputStream());
        wr.writeBytes(queryString);

        wr.flush();
        wr.close();

        Scanner inStream = new Scanner(c.getInputStream());

        while (inStream.hasNextLine())
            line += (inStream.nextLine());

        inStream.close();
        System.out.println(line);
        try {
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(line);
            JSONObject jsonObject = (JSONObject) obj;
            line = (Long) jsonObject.get("id") + "";

        } catch (Exception e) {
            e.printStackTrace();
        }

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return line;
}

From source file:net.bashtech.geobot.BotManager.java

public static String postCoebotConfig(String postData, String channel) {
    if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) {
        URL url;//from w  ww .j av  a  2s .com
        HttpURLConnection conn;

        try {
            url = new URL("http://coebot.tv/api/v1/channel/update/config/" + channel.toLowerCase() + "$"
                    + BotManager.getInstance().CoeBotTVAPIKey + "$" + BotManager.getInstance().nick);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "CoeBot");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));

            // conn.setConnectTimeout(5 * 1000);
            // conn.setReadTimeout(5 * 1000);

            PrintWriter out = new PrintWriter(conn.getOutputStream());
            out.print(postData);
            out.close();

            String response = "";

            Scanner inStream = new Scanner(conn.getInputStream());

            while (inStream.hasNextLine())
                response += (inStream.nextLine());

            inStream.close();
            return response;

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return "";
    } else
        return "";
}

From source file:net.bashtech.geobot.BotManager.java

public static String postCoebotVars(String postData, String requestURL) {
    if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) {
        URL url;/*from w  w  w.j a  va2  s.co m*/
        HttpURLConnection conn;

        try {
            url = new URL(requestURL + "$" + BotManager.getInstance().CoeBotTVAPIKey + "$"
                    + BotManager.getInstance().nick);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "CoeBot");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));

            // conn.setConnectTimeout(5 * 1000);
            // conn.setReadTimeout(5 * 1000);

            PrintWriter out = new PrintWriter(conn.getOutputStream());
            out.print(postData);
            out.close();

            String response = "";

            Scanner inStream = new Scanner(conn.getInputStream());

            while (inStream.hasNextLine())
                response += (inStream.nextLine());

            inStream.close();
            return response;

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return "";
    } else
        return "";
}

From source file:hu.petabyte.redflags.engine.gear.indicator.hu.ContrDescCartellingIndicator.java

@Override
public void beforeSession() throws Exception {
    keywords.clear();//from  w ww .  j  a va 2s  .  c  om
    Scanner s = new Scanner(new ClassPathResource(filename).getInputStream(), "UTF-8");
    while (s.hasNextLine()) {
        keywords.add(s.nextLine());
    }
    s.close();
    LOG.debug("Loaded {} expressions for cartelling", keywords.size());
    setWeight(0.5);
    super.beforeSession();
}

From source file:org.apache.streams.datasift.serializer.DatasiftInteractionActivitySerializerTest.java

@Test
@Override/*from w  w  w.j  a  v a2  s .co  m*/
public void testConversion() throws Exception {

    Scanner scanner = StreamsScannerUtil.getInstance("/rand_sample_datasift_json.txt");

    String line = null;
    while (scanner.hasNextLine()) {
        try {
            line = scanner.nextLine();
            Datasift item = MAPPER.readValue(line, Datasift.class);
            testConversion(item);
            String json = MAPPER.writeValueAsString(item);
            testDeserNoNull(json);
            testDeserNoAddProps(json);
        } catch (Exception e) {
            System.err.println(line);
            throw e;
        }
    }
}

From source file:ezbake.deployer.publishers.local.BaseLocalPublisher.java

private void inheritIO(final InputStream src, final PrintStream dest) {
    new Thread(new Runnable() {
        public void run() {
            Scanner sc = new Scanner(src);
            while (sc.hasNextLine()) {
                dest.println(sc.nextLine());
            }//from   ww w  . j ava 2s .c  o m
        }
    }).start();
}

From source file:ThreadPoolTest.java

/**
 * Searches a file for a given keyword.//from w  w  w .j ava  2 s  .  co m
 * 
 * @param file
 *          the file to search
 * @return true if the keyword is contained in the file
 */
public boolean search(File file) {
    try {
        Scanner in = new Scanner(new FileInputStream(file));
        boolean found = false;
        while (!found && in.hasNextLine()) {
            String line = in.nextLine();
            if (line.contains(keyword))
                found = true;
        }
        in.close();
        return found;
    } catch (IOException e) {
        return false;
    }
}

From source file:ZipTest.java

/**
 * Loads a file from the ZIP archive into the text area
 * @param name the name of the file in the archive
 *///from w  ww .  j a v  a  2  s  .  c  om
public void loadZipFile(final String name) {
    fileCombo.setEnabled(false);
    fileText.setText("");
    new SwingWorker<Void, Void>() {
        protected Void doInBackground() throws Exception {
            try {
                ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
                ZipEntry entry;

                // find entry with matching name in archive
                while ((entry = zin.getNextEntry()) != null) {
                    if (entry.getName().equals(name)) {
                        // read entry into text area
                        Scanner in = new Scanner(zin);
                        while (in.hasNextLine()) {
                            fileText.append(in.nextLine());
                            fileText.append("\n");
                        }
                    }
                    zin.closeEntry();
                }
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void done() {
            fileCombo.setEnabled(true);
        }
    }.execute();
}

From source file:hu.sztaki.incremental.ml.streaming.imsr.MatrixVectorPairSource.java

private void readMatricesSideBySide(Scanner scanner, RealMatrix... matrices) {
    for (int i = 0; i < matrices[0].getRowDimension(); i++) {
        if (!scanner.hasNextLine()) {
            return; //there will be some 0 rows
        }/*  w w  w . j  a v a2s.  co m*/
        String line = scanner.nextLine();
        Scanner lineScanner = initCsvScanner(new Scanner(line));
        for (RealMatrix m : matrices) {
            for (int j = 0; j < m.getColumnDimension(); j++) {
                double d = lineScanner.nextDouble();
                m.setEntry(i, j, d);
            }
        }
        lineScanner.close();
    }
}

From source file:net.bashtech.geobot.BotManager.java

public static String postRemoteDataSongRequest(String urlString, String channel, String requester) {
    if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) {
        URL url;/*  w  ww  .  ja v a  2s .co m*/
        HttpURLConnection conn;

        try {
            url = new URL("http://coebot.tv/api/v1/reqsongs/add/" + channel.toLowerCase() + "$"
                    + BotManager.getInstance().CoeBotTVAPIKey + "$" + BotManager.getInstance().nick);

            String postData = "url=" + URLEncoder.encode(urlString, "UTF-8") + "&requestedBy="
                    + URLEncoder.encode(requester, "UTF-8");
            conn = (HttpURLConnection) url.openConnection();
            System.out.println(postData);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "CoeBot");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));

            // conn.setConnectTimeout(5 * 1000);
            // conn.setReadTimeout(5 * 1000);

            PrintWriter out = new PrintWriter(conn.getOutputStream());
            out.print(postData);
            out.close();
            String response = "";
            if (conn.getResponseCode() < 400) {

                Scanner inStream = new Scanner(conn.getInputStream());

                while (inStream.hasNextLine()) {
                    response += (inStream.nextLine());
                }
                inStream.close();
            } else {
                Scanner inStream = new Scanner(conn.getErrorStream());

                while (inStream.hasNextLine()) {
                    response += (inStream.nextLine());
                }
                inStream.close();
            }
            System.out.println(response);

            return response;

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();

        }

        return null;
    } else {
        return null;
    }

}