Change the contents of a file into a string

Click For Summary
SUMMARY

The discussion focuses on transforming the contents of a file into a String in Java, specifically for creating a Flight object. The provided code utilizes the Scanner class to read a file line by line and print its contents. The user seeks a method to extract specific data elements, such as the flight number and airport names, from the printed output. The suggestion includes searching for delimiters like ": " to isolate the desired information.

PREREQUISITES
  • Basic understanding of Java programming
  • Familiarity with the Scanner class in Java
  • Knowledge of file handling in Java
  • Understanding of String manipulation techniques in Java
NEXT STEPS
  • Learn how to use Java String.split() method for data extraction
  • Explore Java regular expressions for advanced string searching
  • Investigate Java object-oriented programming principles for creating the Flight class
  • Study Java exception handling to manage file-related errors effectively
USEFUL FOR

Java beginners, students in introductory programming courses, and developers looking to manipulate file data in Java applications.

JaysFan31
Hi. I'm in an elementary Java course and I wrote the following code to print the contents of a file I created earlier:

Code:
public static void load(String fileName) throws IOException
  {    
    try
    {      
      Scanner fileScan = new Scanner(new File(fileName));      
      while (fileScan.hasNext())
      {
        System.out.println(fileScan.nextLine());
      }
    }        
    catch (FileNotFoundException exception)
      {
        System.out.println("The file was not found.");
      }
  }

It prints (correctly) the following lines of the file:
Flight Number: 1234
From: Lester B. Pearson International Airport (YYZ): Toronto, Ontario
To: Not Set
7 passenger(s):

Name: Roy Halladay
Age: 30

Name: AJ Burnett
Age: 30

Name: Dustin McGowan
Age: 25

Name: Shaun Marcum
Age: 25

Name: Josh Towers
Age: 30

Name: Casey Janssen
Age: 25

Name: Jeremy Accardo
Age: 25

This represents a particular flight in a class I wrote before. I need to use this information to create a Flight object. I just need to know if there is an easy way to transform (using elementary Java code) all this information into a String so that I can get particular aspects of it. Basically, I need to get the flight number "1234" as a String. I need to get the original airport etc as Strings. Can I do this?
 
Technology news on Phys.org
Is there some way in java to search for a certain element? If so, you could search for the ": " that you have in front of each data item you (I think) are looking for. Anyway, just a random idea. Have a nice day.
 
fileScan.nextLine() is already a String.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
15K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
12K
  • · Replies 11 ·
Replies
11
Views
3K