简体   繁体   中英

Regular expression float number on a input text file

I need to do a payroll report. You have to type the file that has the text information. The Program checks if the file exist or not. Then you make a output file of the program. The programs prints the names, hours, and rates of workers in a text file. I program only runs the last set of numbers.

 import java.text.NumberFormat;
 import javax.swing.JTextArea;
 import java.awt.Font;
 import javax.swing.JOptionPane;
 import java.util.Scanner;
 import java.io.*;

 public class Homework {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    String answer, filename;


    filename = JOptionPane.showInputDialog("Enter the Input File Path:");

    File input = new File(filename);
    if (!input.exists()) {
        JOptionPane.showMessageDialog(null, "The input file:\n" + filename + "\ndoes not exist!");
        System.exit(0);
    }
    filename = JOptionPane.showInputDialog("Enter the Output File Path:");

    File output = new File(filename);
    if (output.exists()) {
        answer = JOptionPane.showInputDialog("The output file alaready exist!\nDo you want to overwrite it?");
        if (!answer.toLowerCase().equals("yes")) {
            System.exit(0);
        }
    }






    PrintWriter outFile = new PrintWriter(filename);
    Scanner in = new Scanner(input);

    double numberWords = 0, countNumber = 0;


   double value;

    String num, words, message;
    String amtStr, line = "";

    String alphaRegex = ".*[A-Za-z].*";
    String numRegex = ".*[0-9].*";



    while (in.hasNext()) {
        words = in.next();

        if (words.matches(alphaRegex)) {

            numberWords++;
            message = "The name is "+words+"\n"; //The Line is but leave +line+

     JOptionPane.showMessageDialog (null, message);



        } else if (words.matches(numRegex)) {
            countNumber++;
           num = in.next();


           message = "The number is "+num+"\n"; //The Line is but leave +line+

     JOptionPane.showMessageDialog (null, message);

             }



}
}

}

It was not your regular expression that was causing the problem. It was the if statement in the while loop. When the word matches numRegex, than you asign in.next() to num, causing the scanner to skip the current word and chossing the next one, that in your case happens to be a num as well.

Replace the while loop with this and it will work (I have tested the code):

while (in.hasNext()) {
    words = in.next();

    if (words.matches(alphaRegex)) {
        numberWords++;
        message = "The name is "+words+"\n";
        JOptionPane.showMessageDialog (null, message);

    } else if (words.matches(numRegex)) {
        countNumber++;
        num = words; // instead of num = in.next()
        message = "The number is "+num+"\n";
        JOptionPane.showMessageDialog (null, message);
    }
}

I'm assuming that you're looking for a regex to identify floating point numbers in text. If that's the case, here's one:

"(\\.\\d+)|(\\d+(\\.\\d+)?)"

This matches any lone decimal followed by one or more digits or any sequence of digits followed by an optional decimal with one or more digits following it. Note that this does not address leading zeros, if that's an issue.

This website is a fantastic resource for building and testing regular expressions:

http://www.regexr.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM