1 Reply Latest reply on Mar 28, 2010 10:23 PM by 843853

    Reading and Writing from Java to a Txt file

      I am making a program that will assign seats to people to dinner tables. The table assigning part is working fine.

      What I need to do is make it so that, instead of having to directly edit the tables in the code from Notepad (see code section 1 below), the user can enter names from the program, which will end up in a file called Hosts.txt or a file called Students.txt.

      When this works properly, the user should be able to run the program from command line using:
       java AssignSeats update
      to update the files, or run the program without the the argument "update" and the program will work normally (that is, run and print the tables into a txt using a batch file. I have accomplished this part.)

      My question is how do I take the input from the program and put it in a txt file, and then at a later date, read from that same txt file into the program?

      Here is code section 1:
      tables.add(new Table("Aimee", 4));
      tables.add(new Table("Alex", 4));
      tables.add(new Table("Ari", 7));
      tables.add(new Table("DJ", 4));
      tables.add(new Table("Elizabeth", 4));
      tables.add(new Table("Harrisons", 6));
      tables.add(new Table("Hicksons", 5));
      tables.add(new Table("Martha", 4));
      tables.add(new Table("Michele", 7));
      tables.add(new Table("Paummi", 6));
      tables.add(new Table("Trish", 4));
      tables.add(new Table("Vacant", 2));
      This is just a sample of the code that I currently have. I want to remove the the part that says ("Aimee", 4), and pull that info from a txt file.

      Here is the complete program.
      import dinner_objects.*;*
      *import java.util.Random;*
      *import java.util.ArrayList;*
      *import java.util.Scanner;*
      *public class AssignSeats*
      *private static Scanner scanner = new Scanner(System.in);*
      *public static void main(String[] args)*
      *ArrayList<Table> tables = new ArrayList<Table>();*
      *ArrayList<Person> people = new ArrayList<Person>();*
      *Random random = new Random();*
      */**These are the tables that are available.*
      **To add a new table use the command:*
      **table.add(new Table (<#name of Table Host#>, <#available seats at the table#>));*
      **Please make the first parameter a String, and the second an int.*
      //see code section 1
      /**These are the students to be assigned to each table.*
      *To add a new person use the command:*
      *people.add(new Person (<#name of new Person#>, <#People to separate this person from#>));*
      *The second parameter is completely optional. You may use it to list any number of people that the new student cannot sit with.*
      *Please make all parameters type String.**
      Please update the grade list yearly. This way you will know what student is in what grade, and so you can more easily update the list of students every year.
      people.add(new Person ("Alvin"));
      people.add(new Person ("Annie"));
      people.add(new Person ("Beatrice"));
      people.add(new Person ("Dipesh"));
      people.add(new Person ("DJ"));
      people.add(new Person ("Peter"));
      people.add(new Person ("Trinity"));
      public static void welcome()
      System.out.println("Welcome to the SSFS Dormitory's Dinner Table Seat Assignment Program.");
      System.out.println("\nWhich of the following are you running this program to update?");
      System.out.println(" -> The Table Hosts (enter \"tables\")?");
      System.out.println(" -> The Students (enter \"students\")?");
      System.out.print("\nEnter your choice: ");
      String update = "";
      update = inputTest("tables", "students");
      public static String inputTest(String... allow)
      ArrayList<String> input = new ArrayList<String>();
      boolean valid = false;
      String returnVal = "";
      returnVal = scanner.nextLine();
      for (int a = 0; a < allow.length; a++)
      if (returnVal.equalsIgnoreCase(allow[a]))
      valid = true;
      if (!valid)
      System.out.print("Not valid. Please re-enter your choice: ");
      } while (!valid);
      return returnVal;
      thank you

      Edited by: matthewd1992 on Mar 25, 2010 9:38 PM