aspose file tools*
The moose likes Beginning Java and the fly likes Input/Output PetRecord Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Input/Output PetRecord" Watch "Input/Output PetRecord" New topic
Author

Input/Output PetRecord

Richard Java
Greenhorn

Joined: Feb 10, 2005
Posts: 5
I need to write a program that the user can either create a new file that ask the user to enter a Pets name, weight and age or pull up an existing file and add pets to the file. I am stuck and wondering if anyone here can get me back on the right track. Any help would be greatly appreciated! Thanks!



import java.io.*;

public class Grp6ClassDemo
{
public static void main(String[] args)
{


String name = null;

System.out.println("You have the option to display a previously created pet record");
System.out.println("or you can create a new one");
System.out.println("Please enter 'C' if you wish to create a new pet record ");
System.out.println("or anything else if you want to display a previously created pet record");
char action = SavitchIn.readLineNonwhiteChar();
if(action != 'C' || action != 'c')
{

System.out.println("Please enter the name of the new pet record you wish to create");
name = SavitchIn.readLineWord( );

PrintWriter outputStream = null;
try
{
outputStream =
new PrintWriter(new FileOutputStream(name));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening a new file.");
System.exit(0);
}

PetRecord usersPet = new PetRecord(name);
System.out.println("Please enter the pet name:");
String correctName = SavitchIn.readLine( );
System.out.println("Please enter the pet age:");
int correctAge = SavitchIn.readLineInt( );
System.out.println("Please enter the pet weight:");
double correctWeight = SavitchIn.readLineDouble( );
usersPet.set(correctName, correctAge, correctWeight);
System.out.println("New pet record has been created:");
usersPet.writeOutput( );
}
else
{
System.out.println("Please input the name of the pet record file you wish to display");
name = SavitchIn.readLineWord( );
File fileObject = new File(name);
while (( ! fileObject.exists( )) || ( ! fileObject.canRead( )))
{
if ( ! fileObject.exists( ))
System.out.println("No such file");
else if ( ! fileObject.canRead( ))
System.out.println("That file is not readable.");
System.out.println("Enter file name again:");
name = SavitchIn.readLineWord( );
fileObject = new File(name);
}

try
{
BufferedReader fileInput =
new BufferedReader(new FileReader(name));
System.out.println("the pet record you requested is:");
//max number of 3 lines in a pet record file.
for(int i=1;i<=3;i++)
{
String Line = fileInput.readLine( );
System.out.println(Line);
}
fileInput.close( );
}
catch(IOException e)
{
System.out.println("Problem reading from file.");
}
}
}
}
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Richard Java:
I am stuck and wondering if anyone here can get me back on the right track. Any help would be greatly appreciated! Thanks!

Let us know where you are stuck, and someone would be happy to help you. Are you getting a compile error? Is something not working the way it is suppose to?
Richard Java
Greenhorn

Joined: Feb 10, 2005
Posts: 5
I am getting an error at this location:

PetRecord usersPet = new PetRecord(name);

Something isn't defined correct or something!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Richard Java:
I am getting an error at this location:

PetRecord usersPet = new PetRecord(name);
You're not on trial here, so there's no need to be evasive. Can you tell us what error you're getting?

Is it a compiler error? Then post the exact error message.

Is it a runtime error? Then post the exception message and stack trace.

If your goal is to get an answer to your question, you should make it as easy as possible for someone to help you.

[ Hint: You haven't shown us the definition of the PetRecord class. Does it have a constructor that takes a String as its only parameter? ]
[ February 20, 2005: Message edited by: David Harkness ]
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
Originally posted by Richard Java:
I am getting an error at this location:

PetRecord usersPet = new PetRecord(name);

Something isn't defined correct or something!



When I started learning Java I would usually forget to declare both constructors in my class,1 empty and 1 accepting argument which gave me headache all the time he he, I bet you didn't declare constructor for your Pet class with String argument so check that first.



kind regards
Igor
Richard Java
Greenhorn

Joined: Feb 10, 2005
Posts: 5
Thanks, I was able to figure it out. Couple of things I clearly missed after taking a couple hours break and then coming back to it I noticed it. Thanks for the suggestions!

Rich
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Input/Output PetRecord