Search code examples
javaregexjoptionpane

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);

             }



}
}

}

Solution

  • 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);
        }
    }