473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

change the contents of a file into a string

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

Expand|Select|Wrap|Line Numbers
  1. public static void load(String fileName) throws IOException
  2.   {    
  3.     try
  4.     {      
  5.       Scanner fileScan = new Scanner(new File(fileName));      
  6.       while (fileScan.hasNext())
  7.       {
  8.         System.out.println(fileScan.nextLine());
  9.       }
  10.     }        
  11.     catch (FileNotFoundException exception)
  12.       {
  13.         System.out.println("The file was not found.");
  14.       }
  15.   }
  16.  
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?
Jun 22 '07 #1
1 1426
Make use of split() method in the String.
nextLine( ) method returns String and using split method - break it into number of tokens.
split method returns a String[].
Create Object of Flight class and set the values for all attributes and work wiht it as an Object.
Refer to documentation for String class
Jun 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
3
by: Don Pasquale | last post by:
The following function intends to delete "numberoflines" lines from a text file, named "s" (string pointer) and pointed to by file pointer "fp", starting from line "line". Now, the function...
0
by: Anonieko Ramos | last post by:
Answer. Use IHttpHandler. thanks Ro ry for coming up with this code. It processes css file to add variables. neat idea using System; using System.IO; using System.Text; using...
3
by: Mark | last post by:
For purposes of sending the contents of a file to a function that requires a string, I am trying to find a way to read a file to a string variable & pass it along. For example, if the file...
1
by: Beowulf | last post by:
I have a report laid out in Design View as shown at the end of this message. I have code that performs the following steps: 1. In main report's Report_Open(), DELETE any old rows in tblTOC for...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
2
by: lab3terch | last post by:
I am a student finishing a class in Visual Basic, and would like some assistance with a program I am working on. I need to read the contents of a file (showing makes of cars on one line and a daily...
6
by: John Kotuby | last post by:
Hi all, I am using a 3rd party program in a VS2005 web project. The tool takes as input a string containing HTML and converts it to RTF. I have been creating a page by dynamically loading...
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.