Change the contents of a file into a string

AI Thread Summary
The discussion centers on a Java code snippet designed to read and print the contents of a file containing flight information. The code successfully outputs details such as flight number, departure and arrival locations, and passenger information. The user seeks guidance on how to extract specific pieces of data, like the flight number and airport names, from the printed output. They suggest using string manipulation techniques, particularly searching for the ": " delimiter to isolate the desired information. The inquiry emphasizes the need for a straightforward method to convert the printed lines into usable strings for creating a Flight object.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
1K
Replies
3
Views
3K
Replies
5
Views
4K
Replies
1
Views
15K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
10
Views
11K
Back
Top