1

I have the following file

This file will t3est the pr0gram t0 ch3ck if the f1le contains 0.04 and 1000 
letters .4
also this file          makes no sense -88 at all. Test you program
7this       9.....

I want to read all the integer and double values. But my program is only able to read 1000-88. Is there a better delimiter that could be used?

Here is my code

public static double sumNumbers(String filename) {

    Scanner s = null;
    try {
        s = new Scanner(new File(filename)).useDelimiter("\\s+");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    while (s.hasNext()) {
        if (s.hasNextInt()) { 
            System.out.print(s.nextInt()); 
        } else {
            s.next();
        }
    }
    return 0;
}
Maddy
  • 2,025
  • 5
  • 26
  • 59

4 Answers4

2

You can check for double number as well. Try the following code. I just modified your code to add a check for double numbers inside while loop.

public static double sumNumbers(String filename) {
    Scanner s = null;
    try {
        s = new Scanner(new File(filename)).useDelimiter("\\s+");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    while (s.hasNext()) {
        if (s.hasNextInt()) {
            System.out.println(s.nextInt());
        } else if (s.hasNextDouble()) {
            System.out.println(s.nextDouble());
        } else {
            s.next();
        }
    }
    return 0;
}

If you want the numbers in between the strings. The following code should work.

BufferedReader br = new BufferedReader(new FileReader(new File("TestScanner.txt")));
StringBuffer buff = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
    buff.append(line);
}
String result = buff.toString().replaceAll("[\\-]*[^0-9]+[\\.]*[^0-9]+", " ");
System.out.println(Arrays.asList(result.trim().split(" ")));
Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
1

Considering you have already read the content of the file in a String, you can use regex to get the integer and double values

String content = <I assume you have content here>;
Pattern pattern = Pattern.compile("-?\\d+\\.?\\d*"); // you might need to tweak to get exactly what you want
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
    System.out.println(matcher.group(0));
}

Above will output following values

3
0
0
3
1
0.04
1000
4
-88
7
9. // you can always process it to remove the extra dot in the case
AKS
  • 18,983
  • 3
  • 43
  • 54
  • I am passing the file directly(using a main method). I dont think that is same as storing it in a String? – Maddy Jul 30 '15 at 05:25
  • But you can always do that, right? here http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file – AKS Jul 30 '15 at 05:25
0

This will print the numbers. you may want to change the pattern to match negative numbers and decimal number starting with .. I also matches the numbers in between the text.

    try 
    {
        Scanner scan = new Scanner(new File("File name"));
        Pattern pattern = Pattern.compile("\\d+\\.{0,1}\\d*"); 
        while (scan.hasNext()) {
            Matcher matcher = pattern.matcher(scan.next());
            while (matcher.find()) {
                System.out.println(matcher.group(0));
            }
        }
        scan.close();
    } catch (Exception e) {

    }
Ace
  • 700
  • 7
  • 37
  • This would be a better pattern that matches your requirement `Pattern pattern = Pattern.compile("[\\-\\+]{0,1}\\d*\\.{0,1}\\d+"); ` – Ace Jul 30 '15 at 05:50
0

with help of sir AKS's answer, this may do:

 public static void main(String[] args) throws FileNotFoundException {

        String content = "";
        Scanner s = new Scanner(new File(filename));
        while (s.hasNext()) {
            content = content + s.nextLine() + "\n";
        }
        System.out.println(content);
        Pattern pattern = Pattern.compile("-?\\d+\\.?\\d*"); // you might need to tweak to get exactly what you want
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            System.out.println(matcher.group(0));
        }
    }
rhitz
  • 1,892
  • 2
  • 21
  • 26