| | |
Need help with Java Assignment
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Solved Threads: 0
I'm glad I found this forum. Just doing seraches has helped me already. However, I am having a problem with my assigment.
I want to open data.txt file, read the file line by line. Each line has four items, int, string, int, and double. Then put the items into an array, and then print out the items in the array.
At this time, the code reads one line only.
Here is what is in my data.txt file:
3176 battery 15 45.25
2217 tire 10 12.50
Here is my code. Any help is appreciated,
Jim
I want to open data.txt file, read the file line by line. Each line has four items, int, string, int, and double. Then put the items into an array, and then print out the items in the array.
At this time, the code reads one line only.
Here is what is in my data.txt file:
3176 battery 15 45.25
2217 tire 10 12.50
Here is my code. Any help is appreciated,
Jim
Java Syntax (Toggle Plain Text)
iimport java.io.*; import java.util.*; public class ReadLines{ public static void main(String args[]) { int count = 0; int MAX_LENGTH = 3; int partID = 0; String partName = " "; int partStock = 0; double partPrice = 0; try{ BufferedReader in = new BufferedReader(new FileReader ("data.txt"));// read file CarPart[] array = new CarPart[MAX_LENGTH];//array String line = in.readLine();//read a line in file StringTokenizer st = new StringTokenizer(line);//tokenizer if (line != null) { partID = Integer.parseInt(st.nextToken()); partName = st.nextToken(); partStock = Integer.parseInt(st.nextToken()); partPrice = Double.parseDouble(st.nextToken()); CarPart cp = new CarPart(partID, partName, partStock, partPrice); array[count] = cp; count++; }//end if System.out.println("Part: " + partID +" " +partName + " " + partStock + " " + partPrice); in.close(); }//end try catch (Exception e) { System.err.println(e); // Print the exception to warn. }//end catch }//end main }//end class
•
•
Join Date: Mar 2004
Posts: 10
Reputation:
Solved Threads: 0
Hey jim,
You need a loop to make it repeat( for, while , do ...while etc..)
this line ----> if (line != null) { should be
while ( line != null )
{
....
...
....
line = in.readline(); // last thing u should do to get to next line, it will exit if //there are no more lines, hope this helps
}
Good luck,
Mel
You need a loop to make it repeat( for, while , do ...while etc..)
this line ----> if (line != null) { should be
while ( line != null )
{
....
...
....
line = in.readline(); // last thing u should do to get to next line, it will exit if //there are no more lines, hope this helps
}
Good luck,
Mel
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
If you want to get each item seperate, you will need to use a StringTokenizer.
While looping, you could take the read line and tokenize it:
You get the drift.
While looping, you could take the read line and tokenize it:
Java Syntax (Toggle Plain Text)
ArrayList first = new ArrayList(); ArrayList second = new ArrayList(); ArrayList third = new ArrayList(); ArrayList fourth = new ArrayList(); int count = 1; while (br.readLine() != null) { StringTokenizer st = new StringTokenizer(br.readLine()); while (st.hasMoreTokens()) { if (count == 1) { first.add(st.nextToken()); } else if (count == 2) { second.add(st.nextToken(); } } }
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Solved Threads: 0
Thanks for the help. I finally got the program working. I posted the final code below.
Thanks again,
Jim
Thanks again,
Jim
Java Syntax (Toggle Plain Text)
public static void main(String args[])throws IOException { BufferedReader fileIn = new BufferedReader(new InputStreamReader (System.in)); int count = 0; int MAX_LENGTH = 4; int partID = 0; String partName = " "; int partStock = 0; double partPrice = 0; CarPart [] array = new CarPart[MAX_LENGTH];//array String fileName; System.out.print("Enter file name: "); fileName = fileIn.readLine(); try { BufferedReader in = new BufferedReader(new FileReader(fileName)); String line; while((line = in.readLine()) != null) { // Read line, check for end-of-file System.out.println("From File: " +line); // Print the line StringTokenizer st = new StringTokenizer(line);//tokenizer partID = Integer.parseInt(st.nextToken()); partName = st.nextToken(); partStock = Integer.parseInt(st.nextToken()); partPrice = Double.parseDouble(st.nextToken()); CarPart cp = new CarPart(partID, partName, partStock, partPrice); if (count < MAX_LENGTH){ array[count]=cp; count++; }//end if //line = in.readLine(); }//end while in.close(); // Always close a stream when you are done with it }//end try catch (IOException e) { // Handle FileNotFoundException, etc. here }//end catch
![]() |
Similar Threads
- Using UTF's, IndexOf's and substrings (Java)
- Java Assignment Help Needed!!!!!!!!!! (Java)
- Another Java Noob :( (Java)
- Help: need feedback on my Java assignment about thread sleep. It's already coded. (Java)
- New to Java, please help with first Assignment (Java)
Other Threads in the Java Forum
- Previous Thread: Connecting to Oracle through Blackberry Handheld Device
- Next Thread: get current time
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time tree webservices windows






