#Parsing and regex

1 messages · Page 1 of 1 (latest)

civic lava
#

Hello, what I am trying to do here is using the data from a text file, I want to extract 5 key pieces of information which are last name, first name, remaining visits, revenue, and date, and print them out. I am struggling to parse both String and Number from the sample input

Sample Input:
Alistair Baker-Smith has 7 visits left and paid $123.21 and he joined on November 22, 2022

File file = new File("\\Users\\name\\exampleinput.txt");
        Scanner scan = new Scanner(file);

        System.out.println(scan.nextLine());

        while (scan.hasNextLine()) {
            System.out.println(scan.nextLine());

        }
placid fiberBOT
#

<@&987246399047479336> please have a look, thanks.

placid fiberBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

severe topaz
#

and btw, you're calling scan.nextLine() 3 times are you aware of that ?

civic lava
#

oh i think i forgot to delete one

severe topaz
#

is the problem gone now ?

civic lava
#

oh, no i was referring to the scan.nextline 3 times

#

but when i ran that code only it returned the data once

severe topaz
#

can show me the contents of the file ?

#

Also its better to use the BufferedReader class , not the scanner to read the contents of A file

civic lava
#
John Doe has 0 visits left and paid $33.45 and he joined on May 13, 2022
Jane Chavez has 5 visits left and paid $5.00 and she joined on December 3, 2021
Alyssa Keys has 3 visits left and paid nothing and she joined on January 12, 2020
John Santos has 2 visits left and paid $32.21 and he joined on February 28, 2021
Phillip Davies has 0 visits left and paid nothing and he joined on March 30, 2022
Patricia Wong has 1 visit left and paid $9.21 and she joined on February 11, 2021
Moe Syzlak has 33 visits left and paid $392.45 and he joined on January 5, 2023
Joe Biden has 895 visits left and paid $15882.22 and he joined on February 1, 2023
Alistair Baker-Smith has 7 visits left and paid $123.21 and he joined on November 22, 2022
Jane Liu has 58 visits left and paid $512.00 and she joined on March 23, 2019
Mahmood Shah has 12 visits left and paid $122.00 and he joined on September 20, 2022
Emma Watson has 5 visits left and paid $52.93 and she joined on September 22, 2022
David Osbourne has 12 visits left and he paid nothing and he joined on March 8, 2023
Tom Kilkarney has 2 visits left and he paid $22.12 and he joined on March 1, 2023
Stephanie Chan has 1 visit left and she paid $44.82 and she joined on April 9, 2022
#

this is the sample input my prof provided, but he said he will be using a different one to test the code afterwards

#

of course format is the same

severe topaz
#

it's better to ; ```BufferedReader br = new BufferedReader(file);
while((line = br.readLine()) != null){
do things
}

placid fiberBOT
severe topaz
#

of course define the linebefore the while loop.

#

for each line, you split it into an array of strings line.split(" ") and write logic to get the name , etc...

civic lava
#

ill give it a shot

#

thanks 🙂

bronze cape
placid fiberBOT
#

File IO in Java should be done preferably with NIO (Java 7+), revolving around the classes Files and Path; and not with the old interface File, BufferedReader, FileReader and similar.

NIO is simple to use. The path to a file is represented using the Path class:

Path path = Path.of("myFile.txt");

All file operations can be found in the Files class:

// Reading
List<String> allLines = Files.readAllLines(path);
// or as a single string
String content = Files.readString(path);
// or with a stream
try (Stream<String> stream = Files.lines(path)) {
  stream.forEach(System.out::println);
}

// Writing
Files.write(path, lines);
// or as a single string
Files.writeString(path, "hello world");
// or with extra options
Files.writeString(path, "hello world",
  StandardOpenOption.WRITE,
  StandardOpenOption.CREATE,
  StandardOpenOption.APPEND);

If you need more control over the process, you can fallback to the old interface, but prefer using the bridge methods from NIO (Files.newBufferedReader, Files.newInputStream, path.toFile() and similar) to benefit from advantages such as correct encoding and better error detection.

Here is a simple example of how to read a file line-wise with the old interface

try (BufferedReader br = Files.newBufferedReader(path)) {
  String line;
  while ((line = br.readLine()) != null) {
    System.out.println(line);
  }
}

it is way more verbose than NIO but it gives more control.

You must not forget to close file handles, even in all exceptional cases. Closing a handle manually is very hard, which is why you should always use try-with-resources for this to let Java automatically close the handle for you:

try (SomeResource resource = ...) {
  ...
} // Automatically closed here, even in exceptional cases
severe topaz
#

BufferedReader is faster

hazy hearth
severe topaz