FILE SAVING assistance
Author |
Message |
venom
|
Posted: Fri Oct 13, 2006 8:41 am Post subject: FILE SAVING assistance |
|
|
curretly in java we have been dooing file saving currently im having a bit of a
problem with retrive old data and some other parts >.>
code: |
// The "FileSaving" class.
import java.awt.*;
import hsa.Console;
import java.io.*;
//--Part A) Without methods, mainline olny.
//--Keyboard entry get array names a adress city ,etc to a dat file to Oct 11
//--Repeat this in a while,check the yes/no response
//--Part B) With a method
//--Part C) Start the program to read data , ask to read the old data
//--Hint: Read the data file first
//--
public class FileSaving
{
static Console c; // The output console
public static void data (String name[], String city[], String age[], String address[], String postal[], int num)
{
c.print ("name", 10);
c.print ("age", 10);
c.print ("city", 10);
c.print ("address", 10);
c.print ("postal", 10);
for (int i = 0 ; i < num ; i++)
{
c.print (name [i], 10);
c.print (age [i], 10);
c.print (city [i], 10);
c.print (address [i], 10);
c.print (postal [i], 10);
}
}
int num;
public static int check ()
{
num = c.readInt ();
if (!(num == 1) & !(num == 2) & !(num == 3))
{
c.println ("please enter 1,2 or 3.");
check ();
}
return (num);
}
public static void main (String[] args)
throws IOException
{
String choice = "o";
String ch = "o";
String name[];
String city[];
String postal[];
String age[];
String address[];
String text;
String oldtext[];
int num;
int counter = 1;
c = new Console ();
// Place your program here. 'c' is the output console
while (!(ch.equals ("y") || ch.equals ("n")))
{
c.println ("would you like to see previous data?");
ch = c.readLine ();
}
while (!choice.equals ("n"))
{
choice = "o";
PrintWriter output;
BufferedReader input;
input = new BufferedReader (new FileReader ("oct11.dat"));
// output = new PrintWriter (new FileWriter ("oct11.dat"));
text = input.readLine ();
while (text != null)
{
if (ch.equals ("y"))
{
c.println (text);
}
counter = counter + 1;
text = input.readLine ();
}
oldtext = new String [counter + 1];
counter = 0;
BufferedReader in;
in = new BufferedReader (new FileReader ("oct11.dat"));
oldtext [0] = in.readLine ();
while (oldtext [counter] != null)
{
counter = counter + 1;
oldtext [counter] = in.readLine ();
}
c.println ("how many people are submitting information");
num = c.readInt ();
name = new String [num + counter]; //gives name a number
city = new String [num + counter]; //gives age a number
age = new String [num + counter]; //gives city a number
postal = new String [num + counter]; //gives postal code a number
address = new String [num + counter]; //gives address a number
for (int i = 0 ; i < num ; i++)
{
c.println ("enter your name."); //asks name
name [i] = c.readLine (); //gets name
c.println ("enter your age."); //asks age
age [i] = c.readLine (); //gets age
c.println ("enter your city."); //asks city
city [i] = c.readLine (); //gets city
c.println ("enter your address."); //asks address
address [i] = c.readLine (); //gets address
c.println ("enter your postal code."); //asks postal code
postal [i] = c.readLine (); //gets postal code
c.clear (); //clears screen
}
output = new PrintWriter (new FileWriter ("oct11.dat")); // saves data to a data file
for (int i = 0 ; i < num ; i++)
{
output.println ((i + 1) + " " + name [i]);
output.println (" " + age [i]);
output.println (" " + city [i]);
output.println (" " + address [i]);
output.println (" " + postal [i]);
}
// BufferedReader input;
// input = new BufferedReader (new FileReader ("oct11.dat"));
for (int i = 0 ; i < counter ; i++)
{
output.println (oldtext [i]);
}
output.close ();
while (!choice.equals ("y") || choice.equals ("n"))
{
c.println ("wouldyou like to continue? y/n");
choice = c.readLine ();
}
}
} // main method
} // FileSaving class
|
i would ask my teacher for help but he is less than useless when it comes to actually teaching java and turing and im reading through the tuts on this forum so i may figure it out on my own |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Fri Oct 13, 2006 11:46 am Post subject: (No subject) |
|
|
Looking at this I really think you need to go back to basics with Java. No, I do not mean "BASIC."
You have numerous extraneous comments, you use the HSA garbage so that you won't be able to receive help from a huge chunk of the Java community, you have extremely poorly named methods and variables ("c" and "data", for instance.
You write array declarations as "String foo[]", which while it may work, is a syntactic relcic that is not used any longer by any sane Java programmer.
You have things like: "!(num == 1)" when you should know about "num != 1".
You use & when you clearly mean to be using &&.
Back to syntax... your use of spaces between methods and their argument lists will leave a large segment of the community with no desire to read your code.
On a deper semantic level, your class is not modeling an object. This is the very reason objects exist. By not doing so, you can see how (for instance in the method "data", it makes things very awkward.
Now, I am not saying this is your fault.
However, it is your responsibility, if you wish to become a good Java programmer, to overcome these problems. |
|
|
|
|
|
venom
|
Posted: Fri Oct 13, 2006 12:44 pm Post subject: (No subject) |
|
|
well this is what happens when you have to deal with a failure of a teacher teaching something you really want to learn but a rock could be more help then the teacher
im still reading through that java tut so i hope to be getting better a java X3 |
|
|
|
|
|
CroAnte
|
Posted: Wed Oct 18, 2006 9:13 am Post subject: (No subject) |
|
|
So what is it you're trying to do? I could cut and paste the code into my Java (we use Holt at my school too), but I'm too lazy. |
|
|
|
|
|
venom
|
Posted: Thu Oct 19, 2006 7:56 am Post subject: (No subject) |
|
|
more or less i was trying to get the file saving program to work in a method
but that not important atm more or less i wanted to check for old data before inputing new data. doesnt matter now i figured it out :p |
|
|
|
|
|
|
|