Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 8th, 2005, 01:37 PM
JOskydive JOskydive is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Lebanon, IN
Posts: 4 JOskydive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 35 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old July 8th, 2005, 03:19 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
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]

Reply With Quote
  #3  
Old July 11th, 2005, 08:35 PM
JOskydive JOskydive is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Lebanon, IN
Posts: 4 JOskydive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 35 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old July 12th, 2005, 09:17 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
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
}

Reply With Quote
  #5  
Old July 12th, 2005, 12:45 PM
JOskydive JOskydive is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Lebanon, IN
Posts: 4 JOskydive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 35 sec
Reputation Power: 0
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


Reply With Quote
  #6  
Old July 12th, 2005, 01:42 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
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)

Reply With Quote
  #7  
Old July 13th, 2005, 11:46 PM
JOskydive JOskydive is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Lebanon, IN
Posts: 4 JOskydive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 35 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Need help with Java Assignment


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT