Problem creating class instances from a flat file

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 1149 Views - Last Post: 03 January 2012 - 06:10 AM Rate Topic: -----

#1 Hammerhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-December 11

Problem creating class instances from a flat file

Posted 30 December 2011 - 01:05 PM

Hi,

I have a question about what part of this code means:

class Pet
{
   private String name;
   private int age;
	
   public Pet(String n, int a)
   {

	_______________________________________


	_______________________________________

   }
	
   public String getName()
   {
	return name;
   }
	
   public int getAge()
   {
	return age;
   }

   public String toString()
   {
	return ____________________() + " - " + _______________________();
   }
}


public class ClassCompleteWorkSheet6
{
   public static void main(String args[]) throws Exception
   {
	Scanner file = new Scanner([b]new File("worksheet6.dat"));[/b]
	int runCount = file.nextInt();
	for(int r=1; r<=runCount; r++){

	   //Construct a Pet
 
	   _______________________________  pet = new ___________________________________	

	   //Print the Pet




	}
   }
}

It was given to me as a word file so it is a litle wierd. I bolded the part I'm confused about. It looks like I need a file for it but I have never seen that before so I am a bit confused.

oops well the new File("worksheet6.dat") is what I'm confused about. I think this file is suppose to print out 6 names and 6 corresponding ages for these pets. I'm pretty sure according to the output I was told I should get it should be just adding in the methods from public class Pet. But it seems to be working funny. I was thinking about adding a scanner into public class Pet but I kept getting a exception error.

*Edited: topic title renamed. Far from being a simple question

This post has been edited by pbl: 02 January 2012 - 09:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem creating class instances from a flat file

#2 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:06 PM

Ummm... Idk if it is just me, but I am seeing a lot of '-' where there should be some code. And the part you have bolded
new File("worksheet6.dat")


is an argument in the Scanner's constructor.

Have you ever used a Scanner to read from a file in Java before?
Was This Post Helpful? 0
  • +
  • -

#3 Hammerhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:08 PM

Yeah, I'm filling that in and I think it is pretty quick. Should I make a file with all that information? And the program will send it back to me?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:09 PM

Looks like the .dat file should contain a single integer, giving the number of iterations for the loop in your code.
Was This Post Helpful? 0
  • +
  • -

#5 Hammerhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:11 PM

Wouldn't whatever I put in for the .nextInt give me the number of iterations? I don't see where I can add in the different names of the animals.
Was This Post Helpful? 0
  • +
  • -

#6 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:13 PM

The .dat file is most likely where the animal names are supposed to reside. Are you doing some form of tutorial? it looks like they give you the number of records, then you have to process that many times.
Was This Post Helpful? 0
  • +
  • -

#7 Hammerhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:15 PM

This can be done in a java file? Could I do it through Excel?
Was This Post Helpful? 0
  • +
  • -

#8 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:17 PM

View PostHammerhead, on 30 December 2011 - 01:15 PM, said:

This can be done in a java file? Could I do it through Excel?



We have to know what kind of file you are trying to work with. What the contents of the file look like?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:19 PM

It's difficult to just guess. All that's being explicitly read from the file is what i already said. It's conceivable that might be followed by n animal names in the file, but that's not shown
Was This Post Helpful? 0
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:22 PM

You can't put anything in for nextInt(). It reads the next integer in the file you passed to the Scanner's constructor. But since that's the first thing you read from the file, is the number of iterations, or how many pets there will be, I imagine a file looks something like this

3 Lassie 11 Fido 12 Kitty 7

3 is the number of Pets that follow, and then you would have to read their names and age and create the Pet from that.

Also for the constructor you use String n and int a for the parameter names. n is short for name and a is short for age. While this is fine and dandy, for me I see a constructor with two parameters, one a number and another a series of letters. So I think the number is n (Default variable for math class) and the letter is a... first letter of the alphabet. You can see how that could get confusing. It wouldn't really make it that much harder to just put name and age all spelled out so there was no confusion. :)
Was This Post Helpful? 0
  • +
  • -

#11 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:23 PM

View Postg00se, on 30 December 2011 - 01:19 PM, said:

It's difficult to just guess. All that's being explicitly read from the file is what i already said. It's conceivable that might be followed by n animal names in the file, but that's not shown


I feel the same way. That is why I believe he is following some form of Tutorial that didnt give the file to download.

@Hammer, do you have a write-up that you are following?
Was This Post Helpful? 0
  • +
  • -

#12 Hammerhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:27 PM

6
dog 1
cat 7
spot 9
fido 3
garfield 22
odie 8


I made a file called worksheet6.dat, but I am getting an error (says it cannot find symbol) for New File. Any ideas on how to change this?
Was This Post Helpful? 0
  • +
  • -

#13 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:28 PM

View PostHammerhead, on 30 December 2011 - 01:27 PM, said:

6
dog 1
cat 7
spot 9
fido 3
garfield 22
odie 8


I made a file called worksheet6.dat, but I am getting an error (says it cannot find symbol) for New File. Any ideas on how to change this?

Can you give us the exact error and a code line that it is flagged at?
Was This Post Helpful? 0
  • +
  • -

#14 Hammerhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:31 PM

View PostAmatore, on 30 December 2011 - 01:23 PM, said:

@Hammer, do you have a write-up that you are following?


I just have

INPUT - worksheet6.dat
6
dog 1
cat 7
spot 9
fido 3
garfield 22
odie 8


OUTPUT
dog - 1
cat - 7
spot - 9
fido - 3
garfield - 22
odie - 8

public class ClassCompleteWorksheet6
{
   public static void main(String args[]) throws Exception
   {
	Scanner file = new Scanner(new File("worksheet6.dat"));
	int runCount = file.nextInt();
	for(int r=1; r<=runCount; r++){

	   //Construct a Pet

	    Pet pet = new Pet();
	    System.out.println(pet);
	    pet.getAge();
	    pet.getName();
	    pet.toString();


Scanner file = new Scanner(new File("worksheet6.dat"));
^
symbol: class File
location: class ClassCompleteWorksheet6
1 error

Process completed.
That is the error.

It is line 5. Sorry, Jcreator Pro doesn't give lines.
Was This Post Helpful? 0
  • +
  • -

#15 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Problem creating class instances from a flat file

Posted 30 December 2011 - 01:33 PM

Thats fine. From the looks of it you dont have java.io.File imported, that would be my first check.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3