473,396 Members | 1,990 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,396 software developers and data experts.

Reading a text file

1
Hi, i'm doing an assigment in java. I have a text file, the format is the following:

Massachusettes: 016,017,018,019,020,021,022,023,024,025,026,027,05 5
Rhode Island: 028,029
New Hampshire: 030,031,032,033,034,035,036,037,038
Maine: 039,040,041,042,043,044,045,046,047,048,049
Vermont: 050,051,052,053,054,056,057,058,059
Connecticut: 060,061,062,063,064,065,066,067,068,069

I have to store this data in an array. My idea is used a 2D-array. I'm having problems with the StringTokenizer. I have never used StringTokenizer for 2D arrays.

Also, i have another problem, since the number of columns in the array don't have a determined size. I don't know how to set the size.

Once I stored the data into the 2D-array, i need to compare a number given by the user with the numbers stored in the array. If a number match with some number in the array I need to print the Name of the State where the number belongs.

Hope, someone can help or give more ideas!! Thanks!


I have the following:

public class readTest{
// Main method
public static void main (String args[]){
// Stream to read file
FileInputStream fin;

String zipRange [][];
zipRange = new String[4][6];

try
{
// Open an input stream
fin = new FileInputStream ("test.txt");

String line = new DataInputStream(fin).readLine();
StringTokenizer st = new StringTokenizer (line,":");
int n = 0;
int m = 0;
while (line != null){

String state = st.nextToken();
zipRange [n][m] = state;
n++;
while (st.hasMoreTokens()){
StringTokenizer st2 = new StringTokenizer (line, ",");
String range = st2.nextToken();
zipRange [n+1][m] = range;
m++;
n++;

}

}


// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
}
}
Jun 19 '07 #1
1 1686
r035198x
13,262 8TB
Hi, i'm doing an assigment in java. I have a text file, the format is the following:

Massachusettes: 016,017,018,019,020,021,022,023,024,025,026,027,05 5
Rhode Island: 028,029
New Hampshire: 030,031,032,033,034,035,036,037,038
Maine: 039,040,041,042,043,044,045,046,047,048,049
Vermont: 050,051,052,053,054,056,057,058,059
Connecticut: 060,061,062,063,064,065,066,067,068,069

I have to store this data in an array. My idea is used a 2D-array. I'm having problems with the StringTokenizer. I have never used StringTokenizer for 2D arrays.

Also, i have another problem, since the number of columns in the array don't have a determined size. I don't know how to set the size.

Once I stored the data into the 2D-array, i need to compare a number given by the user with the numbers stored in the array. If a number match with some number in the array I need to print the Name of the State where the number belongs.

Hope, someone can help or give more ideas!! Thanks!


I have the following:

public class readTest{
// Main method
public static void main (String args[]){
// Stream to read file
FileInputStream fin;

String zipRange [][];
zipRange = new String[4][6];

try
{
// Open an input stream
fin = new FileInputStream ("test.txt");

String line = new DataInputStream(fin).readLine();
StringTokenizer st = new StringTokenizer (line,":");
int n = 0;
int m = 0;
while (line != null){

String state = st.nextToken();
zipRange [n][m] = state;
n++;
while (st.hasMoreTokens()){
StringTokenizer st2 = new StringTokenizer (line, ",");
String range = st2.nextToken();
zipRange [n+1][m] = range;
m++;
n++;

}

}


// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
}
}
1.) Please use code tags when posting code
2.) Please chose a title that describes your problem
3.) Do not use tokenizer, use the String.split method.
4.) Do not use DataInputStream for reading a text file, use FileReader or Scanner
5.) Is it an assignment requirement that you use an array? You could use ArrayList (dynamic) or better still
HashMap<String, <ArrayList<String>>


Edit: I've renamed the thread.
Jun 19 '07 #2

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

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.