another looping question

reading from a txt file

Page 1 of 1

14 Replies - 1616 Views - Last Post: 26 September 2007 - 07:11 PM Rate Topic: -----

#1 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 99
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

another looping question

Posted 26 September 2007 - 10:54 AM

here is my deal,
reading from a text file,

that is formatted like so :
so the number determines which class to use,
then the name etc...

1
Harry
Hacker
5800.21
3000.50
0
Sam
Smith
1200.56



what i want to do is have a for loop before all of this...
something like
for(int i = 1; i > 0; i++){
everything else
}



and then if the employeeType returns a -1, to set i = -1, thereby ending the loop and being done.

however, whenever i do this, i put the employeeType decleration and scan.next inside the for loop, and for some reason they don't scan correctly there.

the program just breaks.

but if the for loop isn't there, it scans just fine.


for(int i = 0; i >= 0; i++){
int employeeType = scan.nextInt();
if(employeeType == 0)
{
Employee type = new Employee(scan.next(),scan.next(),scan.nextDouble());
System.out.println(type.toString());
}
else if(employeeType == 1)
{
Boss type = new Boss(scan.next(),scan.next(),scan.nextDouble(),scan.nextDouble());
System.out.println(type.toString());
}
else
{
break;
}}


any ideas? thanks.

This post has been edited by Tyler James Lee: 26 September 2007 - 11:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: another looping question

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: another looping question

Posted 26 September 2007 - 11:03 AM

have you tried using break; instead of changing the i valued? it will automatically exit the current loop.
I'll run your code in a little bit and try and find a direct answer if someone else doesn't beat me to it.
Was This Post Helpful? 0
  • +
  • -

#3 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 99
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: another looping question

Posted 26 September 2007 - 11:08 AM

yeah, thats what i was looking for, i kept trying quit and end.

flipping days of programming the ti-84 threw me off.

but thats not the most pressing matter,
the real issue is just getting this thing to loop, if you know a better way that would be great.

never read from a text file before and i can't seem to figure out how to check if the nextline returns a value or not.

hey, i made a change to my code above.

here it is again

for(int i = 0; i >= 0; i++){
int employeeType = scan.nextInt();
if(employeeType == 0)
{
Employee type = new Employee(scan.next(),scan.next(),scan.nextDouble());
System.out.println(type.toString());
}
else if(employeeType == 1)
{
Boss type = new Boss(scan.next(),scan.next(),scan.nextDouble(),scan.nextDouble());
System.out.println(type.toString());
}
else
{
break;
}}



so the program will run through until it hits a line with no information in it,
and then it just dies.
i thought that a broken line would return a -1 value, and then my last else statement could be used.
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: another looping question

Posted 26 September 2007 - 01:31 PM

http://www.dreaminco.../snippet414.htm
nextLine will hold the text currently read in on each line, just add the if statement to catch the beginning of each record, and it will automatically stop looping at the end of teh file

If you need more direction, feel free to ask :)
Was This Post Helpful? 0
  • +
  • -

#5 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 99
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: another looping question

Posted 26 September 2007 - 02:36 PM

hey i did a real quick throwing in there.

if i understand correctly,
the buffered reader takes the txt file, and converts it to a string,
which then i can scan and not have a problem getting through.

whenever it gets to catch it says illegal start of expression.

import java.util.*;
import java.io.*;
/*
	 File: TestEmployee.java

	 Description: Tests both the boss and employee classes, and reads from a file to input data
	 
	 Name: Tyler Lee

	 UT EID: Tjl262
				
	 Course Name: CS 313E

	 Unique Number: 56440

	 Date Created: 9/25/2007

	 Date Last Modified: 9/26/2007
	 */	
public class TestEmployee{
public static void main(String[] args) throws IOException{

BufferedReader in = new BufferedReader(employee.txt);
	 String nextLine = "";
	 String theText = "";
	 while(nextLine != null){
		  nextLine = in.readLine();
		  if(nextLine != null)
			   theText += nextLine + "\n";
	 }
	 //theText now contains the contents of the text file
	}
catch(Exception ex) {
	System.out.println("Error Opening Text File");
	JOptionPane.showMessageDialog(view, "The File cannot be opened.", 
	 "Information", JOptionPane.INFORMATION_MESSAGE);
}
/*
File empList = new File("employee.txt");
Scanner scan = new Scanner(theText);
*/
for(int i = 0; i >= 0; i++){
int employeeType = scan.nextInt();
if(employeeType == 0)
{
Employee regularJoe = new Employee(scan.next(),scan.next(),scan.nextDouble());
System.out.println(regularJoe.toString());
}
else if(employeeType == 1)
{
Boss bossMan = new Boss(scan.next(),scan.next(),scan.nextDouble(),scan.nextDouble());
System.out.println(bossMan.toString());
}
else
{
break;
}}
}}



i would have integrated it better, but i have another class right now,
if you think i will get it as i move forward thats fine, but as of right now i'm stumped as to why its stopping there.
Was This Post Helpful? 0
  • +
  • -

#6 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: another looping question

Posted 26 September 2007 - 04:58 PM

this:
BufferedReader in = new BufferedReader(employee.txt);
needs quotes:
BufferedReader in = new BufferedReader("./employee.txt");
the way i did it assumes employee.txt is in the same folder
Was This Post Helpful? 0
  • +
  • -

#7 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 99
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: another looping question

Posted 26 September 2007 - 05:29 PM

no change

ublic class TestEmployee{
public static void main(String[] args) throws IOException{

BufferedReader in = new BufferedReader("./employee.txt");
	 String nextLine = "";
	 String theText = "";
	 while(nextLine != null){
		  nextLine = in.readLine();
		  if(nextLine != null)
			   theText += nextLine + "\n";
	 }
	 //theText now contains the contents of the text file
	}
catch(Exception ex) {
	System.out.println("Error Opening Text File");
	JOptionPane.showMessageDialog(view, "The File cannot be opened.", 
	 "Information", JOptionPane.INFORMATION_MESSAGE);
}



catch causes hte error still.
Was This Post Helpful? 0
  • +
  • -

#8 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: another looping question

Posted 26 September 2007 - 05:59 PM

http://java.sun.com/...eredReader.html
public boolean ready()
throws IOException

Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.


also:
BufferedReader in = new BufferedReader(new FileReader("employee.txt"));
you need to create a file reader.

then use in.nextLine() to get the next line.


this should get you started in the right direction:
	public class TestEmployee{
	   public static void main(String[] args) throws IOException{
	  
		 BufferedReader in = new BufferedReader(new FileReader("employee.txt"));
		 String inputLine = "";
		 
	  
		 while (in.ready()) {
			inputLine=in.readLine();
			//check to see if its a blank line if so skip the line.
			if (inputLine!=null) {
			
				//now that we know its not a blank line assign it to employee number
			   int employeeType = Integer.parseInt(inputLine);
			   
			   //test to see which type of employee it is
			   if(employeeType == 0 ){
			   /*read in all the other lines pertaining to this employee using in.readLine()
			   Example: 
			   String name=in.readLine();*/
			   }
			   if(employeeType == 1){
			   /*read in all the other lines pertaining to this employee using in.readLine()
			   Example: 
			   String name=in.readLine();*/
			   }
			}
		 
		 }//if (inputLine!=null) {
	  }
	  
   	
   	
   }// end TestEmployee



the reason for using a try catch block is to gracefully exit the program with an error vs it just being thrown where its not so graceful. If you wanna use a try catch block you need forgot the try part all you have is the catch part and you would only put the part where you read the input in inside the try part.

This post has been edited by sl4ck3r: 26 September 2007 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: another looping question

Posted 26 September 2007 - 06:01 PM

try
		{
			BufferedReader in = new BufferedReader(new FileReader("./employee.txt"));
			String nextLine = "";
			while(nextLine != null)
			{
				nextLine = in.readLine();
				if(nextLine != null)
				{
					if(nextLine.equals("0"))
					{
						Employee regularJoe = new Employee(in.readLine(),in.readLine(),Double.parseDouble(in.readLine()));
						System.out.println(regularJoe);
					}
					else if(nextLine.equals("1"))
					{
						Boss bossMan = new Boss(in.readLine(),in.readLine(),Double.parseDouble(in.readLine()),Double.parseDouble(in.readLine()));
						System.out.println(bossMan);
					}
					else
					{
						System.out.println("Missing Employee Identifier, must be 0 or 1");
					}
				}
			}
			//theText now contains the contents of the text file
		}
		catch(Exception ex)
		{
			System.out.println("Error Opening Text File");
			JOptionPane.showMessageDialog(null, "The File cannot be opened.","Information", JOptionPane.INFORMATION_MESSAGE);
		}
	


this works, with a file named employee.txt

I created my own private Boss and Employee classes in my example, and it works quite nicely.

If you still need more help, i did finish the program, not just this code section.
Was This Post Helpful? 0
  • +
  • -

#10 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: another looping question

Posted 26 September 2007 - 06:04 PM

william wilson is also correct that you should be using the .equals method to compare strings if you do it that way.

This post has been edited by sl4ck3r: 26 September 2007 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#11 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 99
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: another looping question

Posted 26 September 2007 - 06:15 PM

first off,
i fully appreciate your time, thanks a bunch.

secondly, for testing purposes i just cut and paste your code and f'ing bluej doesn't want to recognize te.readEmployees() as a method.



blue j is about to get destroyed by my wrath.

spent most of the day on this and one other program that was easier, but alot more actual code, and i want to kill it.

p.s. :) i know my code looks like nastyness.
Was This Post Helpful? 0
  • +
  • -

#12 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: another looping question

Posted 26 September 2007 - 06:21 PM

View PostTyler James Lee, on 26 Sep, 2007 - 06:15 PM, said:

first off,
i fully appreciate your time, thanks a bunch.

secondly, for testing purposes i just cut and paste your code and f'ing bluej doesn't want to recognize te.readEmployees() as a method.



blue j is about to get destroyed by my wrath.

spent most of the day on this and one other program that was easier, but alot more actual code, and i want to kill it.

p.s. :) i know my code looks like nastyness.


did you try my code? you have to import java.io.*; if you didnt.
Was This Post Helpful? 0
  • +
  • -

#13 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: another looping question

Posted 26 September 2007 - 06:25 PM

personally i liked jcreator LE better for learning with.


do you have testing code that you need to work with? If so you need to make sure you are creating method names to match those requested.
If you ever get super stuck I am on kasamba, but i won't link here for Chris' sake :)
Was This Post Helpful? 0
  • +
  • -

#14 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 99
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: another looping question

Posted 26 September 2007 - 06:51 PM

alright, thanks both, proj was due at 9 i put together a pretty good system, wasn't perfect but it worked.

and my prof uses bluej so thats why i stick with it.
Was This Post Helpful? 0
  • +
  • -

#15 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: another looping question

Posted 26 September 2007 - 07:11 PM

yeah its generally nice to stick with what the prof is using... im currently using jGrasp
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1