|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with Java Assignment
I am trying to promt user for a file, each line has an int, String, int and double. Then read the file into an array and then print out the contents of the array. I am having problems getting the file into the array. My code is below. Any help is appreciated.
novice programmer/student, Jim import java.io.*; import java.lang.*; import java.util.*; public class CarPartProcessing1{ public static void main (String [] args) throws IOException { int id = 0; String name = ""; int stock = 0; double price = 0.0; System.out.print("Enter file name:"); BufferedReader read = new BufferedReader( new InputStreamReader(System.in) ); String filename = read.readLine(); BufferedReader rf = new BufferedReader(new FileReader(filename)); while(rf.ready()) { String line = rf.readLine(); //reads a line from file CarPart cp = new CarPart (id, name, stock, price); int count = 0; int MAX_LENGHT = 30; if (count < MAX_LENGHT) { array[count] = cp; count++; }//end if else while( line != null ) { int n = 0; System.out.println(line); n++; line = rf.readLine(); }//end while } rf.close(); }//end main }//end class |
|
#2
|
||||
|
||||
|
In the future, try wrapping your code with [code][/code]
It looks like with this code you are simply reading the text file and printing it on the screen... correct? What exactly is the error/problem you're receiving? Where is array declared? What does your input file look like? [provide a sample line or two] |
|
#3
|
|||
|
|||
|
MadCowDzz,
Sorry for not wrapping my code. I have made some changes to my code. 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 Code:
import 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
|
|
#4
|
||||
|
||||
|
Here's how to read multiple lines of a text file...
Try and adapt it to your own code... ![]() Code:
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(line); // Print the line
}
in.close(); // Always close a stream when you are done with it
}
catch (IOException e) {
// Handle FileNotFoundException, etc. here
}
|
|
#5
|
|||
|
|||
|
MadCowDzz,
Thanks for the code, it works! Thanks. However, now I cannot get the items in the lines into the array. I've tried for about 4 hours to figure it out with no luck. Any further guidance is appreciated. Jim Code:
import java.io.*;
import java.util.*;
public class CarPartProcessing1{
public static void main(String args[])throws IOException {
BufferedReader fileIn = new BufferedReader(new InputStreamReader (System.in));
int count = 0;
int MAX_LENGTH = 30;
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(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
}//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
for(int i =0; i < array.length; i++) {
System.out.print("Array: " + array[count]);
}//end for
}//end main
}//end class
|
|
#6
|
||||
|
||||
|
Note, I will refer to my above post [#4], and your post above it [#3]...
Replace my line System.out.println(line); with something similar to what you have in your post [#3] Something like: array[cnt++] = line; (notice, i'm using cnt as a temporary place holding variable) |
|
#7
|
|||
|
|||
|
Thanks
MadCowDzz,
Thanks very much. That last post gave me the key to getting my program to work. Thanks again. I posted the final code below. Thanks, Jim Code:
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
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Need help with Java Assignment |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|