List of usage examples for java.util Scanner hasNextDouble
public boolean hasNextDouble()
From source file:MainClass.java
public static void main(String args[]) throws IOException { int i;/*w ww . ja v a 2s .c o m*/ double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("string true false 1 2 3 4.12"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { int i;/* www. j a v a2 s.co m*/ double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("Testing Scanner 10 12.2 one true two false"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); }
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 1.3985"; Scanner scanner = new Scanner(s); // assign locale as US to recognize double numbers in a string scanner.useLocale(Locale.US); while (scanner.hasNext()) { // check if the scanner's next token is a double System.out.println(scanner.hasNextDouble()); System.out.println(scanner.next()); }/*from ww w .ja v a 2s . co m*/ scanner.close(); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { FileWriter fout = new FileWriter("test.txt"); fout.write("2, 3.4, 5,6, 7.4, 9.1, 10.5, done"); fout.close();/* w w w .ja v a 2 s . c om*/ FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); // Set delimiters to space and comma. // ", *" tells Scanner to match a comma and zero or more spaces as // delimiters. src.useDelimiter(", *"); // Read and sum numbers. while (src.hasNext()) { if (src.hasNextDouble()) { System.out.println(src.nextDouble()); } else { break; } } fin.close(); }
From source file:uk.ac.gda.dls.client.views.MonitorCompositeFactory.java
void setVal(String newVal) { if (decimalPlaces != null) { Scanner sc = new Scanner(newVal.trim()); if (sc.hasNextDouble()) { NumberFormat format = NumberFormat.getInstance(); format.setMaximumFractionDigits(decimalPlaces.intValue()); newVal = format.format(sc.nextDouble()); }// www.j a va2 s . c o m sc.close(); } val = newVal; if (!isDisposed()) display.asyncExec(setTextRunnable); }
From source file:uk.ac.gda.dls.client.views.ReadonlyScannableComposite.java
private void setVal(String newVal) { if (decimalPlaces != null) { Scanner sc = new Scanner(newVal.trim()); if (sc.hasNextDouble()) { NumberFormat format = NumberFormat.getInstance(); format.setMaximumFractionDigits(decimalPlaces.intValue()); newVal = format.format(sc.nextDouble()); }//ww w. j a v a 2 s . c o m sc.close(); } val = newVal; if (!isDisposed()) { if (minPeriodMS != null) { if (!textUpdateScheduled) { textUpdateScheduled = true; display.asyncExec(new Runnable() { @Override public void run() { display.timerExec(minPeriodMS, setTextRunnable); } }); } } else { display.asyncExec(setTextRunnable); } } }
From source file:com.rockhoppertech.music.DurationParser.java
/** * Copies new events into the TimeSeries parameter - which is also returned. * /*from ww w . j a v a 2 s . co m*/ * @param ts * a {@code TimeSeries} that will be added to * @param s * a duration string * @return the same{@code TimeSeries} that you passed in */ public static TimeSeries getDurationAsTimeSeries(TimeSeries ts, String s) { String token = null; Scanner scanner = new Scanner(s); if (s.indexOf(',') != -1) { scanner.useDelimiter(","); } while (scanner.hasNext()) { if (scanner.hasNext(dpattern)) { token = scanner.next(dpattern); double d = getDottedValue(token); ts.add(d); logger.debug("'{}' is dotted value is {}", token, d); } else if (scanner.hasNext(pattern)) { token = scanner.next(pattern); double d = durKeyMap.get(token); ts.add(d); logger.debug("'{}' is not dotted value is {}", token, d); // } else if (scanner.hasNext(tripletPattern)) { // token = scanner.next(tripletPattern); // double d = durKeyMap.get(token); // ts.add(d); // System.out.println(String // .format("'%s' is not dotted value is %f", // token, // d)); } else if (scanner.hasNextDouble()) { double d = scanner.nextDouble(); ts.add(d); logger.debug("{} is a double", d); } else { // just ignore it. or throw exception? String skipped = scanner.next(); logger.debug("skipped '{}'", skipped); } } scanner.close(); return ts; }
From source file:hudson.plugins.plot.XMLSeries.java
/** * Convert a given object into a String. * //from w ww . j a v a2 s . c om * @param obj * Xpath Object * @return String representation of the node */ private String nodeToString(Object obj) { String ret = null; if (nodeType == XPathConstants.BOOLEAN) { return (((Boolean) obj)) ? "1" : "0"; } if (nodeType == XPathConstants.NUMBER) return ((Double) obj).toString().trim(); if (nodeType == XPathConstants.NODE || nodeType == XPathConstants.NODESET) { if (obj instanceof String) { ret = ((String) obj).trim(); } else { if (null == obj) { return null; } Node node = (Node) obj; NamedNodeMap nodeMap = node.getAttributes(); if ((null != nodeMap) && (null != nodeMap.getNamedItem("time"))) { ret = nodeMap.getNamedItem("time").getTextContent(); } if (null == ret) { ret = node.getTextContent().trim(); } } } if (nodeType == XPathConstants.STRING) ret = ((String) obj).trim(); // for Node/String/NodeSet, try and parse it as a double. // we don't store a double, so just throw away the result. Scanner scanner = new Scanner(ret); if (scanner.hasNextDouble()) { return String.valueOf(scanner.nextDouble()); } return null; }
From source file:com.rockhoppertech.music.DurationParser.java
public static List<Double> getDurationsAsList(String durations) { String token = null;/* w ww .ja va2 s. c o m*/ List<Double> list = new ArrayList<Double>(); Scanner scanner = new Scanner(durations); if (durations.indexOf(',') != -1) { scanner.useDelimiter(","); } while (scanner.hasNext()) { if (scanner.hasNext(dpattern)) { token = scanner.next(dpattern); double d = getDottedValue(token); list.add(d); logger.debug("'{}' is dotted value is {}", token, d); } else if (scanner.hasNext(pattern)) { token = scanner.next(pattern); double d = durKeyMap.get(token); list.add(d); logger.debug("'{}' is not dotted value is {}", token, d); // } else if (scanner.hasNext(tripletPattern)) { // token = scanner.next(tripletPattern); // double d = durKeyMap.get(token); // ts.add(d); // System.out.println(String // .format("'%s' is not dotted value is %f", // token, // d)); } else if (scanner.hasNextDouble()) { double d = scanner.nextDouble(); list.add(d); logger.debug("{} is a double", d); } else { // just ignore it. or throw exception? String skipped = scanner.next(); logger.debug("skipped '{}'", skipped); } } scanner.close(); return list; }
From source file:org.talend.dataprep.schema.csv.CSVFastHeaderAndTypeAnalyzer.java
/** * Performs type analysis for fields of the record at the specified index. * //from w ww . j a v a 2s . c o m * @param i the index of the record to be analyzed. * @return the list of types of the record */ private List<Integer> setFieldType(int i) { List<Integer> result = new ArrayList<>(); String line = i < sampleLines.size() ? sampleLines.get(i) : null; if (StringUtils.isEmpty(line)) { return result; } List<String> fields = readLine(line); for (String field : fields) { Scanner scanner = new Scanner(field); scanner.useDelimiter(Character.toString(separator.getSeparator())); // called integer but we are looking for long in Java parlance if (scanner.hasNextLong()) { result.add(INTEGER); scanner.next(); } else if (scanner.hasNextDouble()) { result.add(DECIMAL); scanner.next(); } else if (scanner.hasNextBoolean()) { result.add(BOOLEAN); scanner.next(); } else { String text = scanner.hasNext() ? scanner.next() : StringUtils.EMPTY; switch (text) { case "": result.add(EMPTY); break; default: // used to detect a stable length of a field (may be it is a date or a pattern) result.add(text.length()); } } scanner.close(); } return result; }