
-----------------------------------
venom
Fri Oct 13, 2006 8:41 am

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 >.>

// 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  :roll: and im reading through the tuts on this forum so i may figure it out on my own

-----------------------------------
wtd
Fri Oct 13, 2006 11:46 am


-----------------------------------
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 fooNow, 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
Fri Oct 13, 2006 12:44 pm


-----------------------------------
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
Wed Oct 18, 2006 9:13 am


-----------------------------------
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
Thu Oct 19, 2006 7:56 am


-----------------------------------
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
