开发者

help sorting the program

I need to print out the elements of a sorted array and also write them to a file. It's an array of objects of Customer class(my own deifinition). The Customer[] review consists of a max of 30 of these objects. Now, the problem is I need the number of elements in the array to do this but I can't pass that variable to the methods as it's not a part of the main method nor is it a global variable. The methods are called in the menu method. Both WriteFile and DisplayRecords use num_records. The reason I'm having this problem is because earlier I didn't have the menu method and simply carried the operations out from the main method but apparently the menu method is required. I apologize in advance if the problem seems vague.

public static int readFile(String filename, Customer[] review)throws IOException{

          int count=0;
          Scanner scan = new Scanner (new File (filename));

          /*Reading the first record separatly*/
                      Customer first = new Customer();

                      String[] a = scan.nextLine().split("=");
                      first.set_account_id(Integer.parseInt(a[1].trim()));

                      a = scan.nextLine().split("=");
                      first.set_name(a[1].toUpperCase().trim());

                      a = scan.nextLine().split("=");
                      first.set_address(a[1].trim());

                      a = scan.nextLine().split("=");
                      first.set_phone_number(a[1].trim());

                      a = scan.nextLine().split("=");
                      first.set_date_of_birth(a[1].trim());

                      a = scan.nextLine().split("=");
                      first.set_balance(Double.parseDouble(a[1].trim()));


                      scan.nextLine();// resets the buffer reader
                      review[0]= first;
                      count = count+1;

                      while (scan.hasNext()&& count>0){
                      Customer temp = new Customer();

                      String[] st = scan.nextLine().split("=");

                       for(int i=1;i<count;i++){
                             if(Integer.parseInt(st[1].trim())== review[i].get_accountid()){ // checking for duplicate records
                                  System.out.println("This account id is already in use so the record won't be read");
                                  for (int k=0; k<7; k++)
                                       scan.nextLine();
                          }
                             else
                                break;
                     }

                      temp.set_account_id(Integer.parseInt(st[1].trim()));
                      st = scan.nextLine().split("=");
                      temp.set_name(st[1].toUpperCase().trim());
                      st = scan.nextLine().split("=");
                      temp.set_address(st[1].trim());
                      st = scan.nextLine().split("=");
                      temp.set_phone_number(st[1].trim());
                      st = scan.nextLine().split("=");
                      temp.set_date_of_birth(st[1].trim());
                      st = scan.nextLine().split("=");
                      temp.set_balance(Double.parseDouble(st[1].trim()));

                      if (scan.hasNextLine()){
                          scan.nextLine();
                          }

                     int j;
                     for(j=0;j<count;j++){

                            if (temp.get_name().compareTo(review[j].get_name())<0){ // 开发者_高级运维Putting records in ascending order
                                break;
                            }
                         }

                      count=count+1;
                      for (int k=count;k>j;k--){
                          review[k]=review[k-1];
                      }

                      review[j]= temp;

                         if (count>=30){
                         System.out.println("The number of records read has exceeded the limit and it will stop reading now");
                         break;
              }

        }

        return count;
   }


      public static void displayRecords(Customer[] review, int num_records){
          for (int k=0; k< num_records; k++){
              System.out.println(review[k].get_name());
              System.out.println(review[k].get_balance());
              System.out.println(review[k].get_accountid());
              System.out.println(review[k].get_date_of_birth());
              System.out.println(review[k].get_address());
              System.out.println(review[k].get_phone_number());


          }
  }

  public static int writeFile(Customer[] review, int num_records ) throws IOException{
      int count=0;
      BufferedWriter out = new BufferedWriter(new FileWriter("newCustomers.txt"));
      for (int i=0; i<num_records;i++){
              out.write("Name = " + review[i].get_name()+ "\r\n");
              out.write("Balance = " +(int)review[i].get_balance() + "\r\n");
              out.write("Accoutn Id = " + review[i].get_accountid() + "\r\n");
              out.write("DOB = " + review[i].get_date_of_birth() + "\r\n");
              out.write("Address = " + review[i].get_address() + "\r\n");
              out.write("Phone Number = " + review[i].get_phone_number() + "\r\n");
              out.newLine();
              count++;
      }
              out.close();
              return count;
  }


      public static void menu(Customer[] records)throws IOException{
         Scanner in = new Scanner(System.in);
         int choice;
         Boolean loopcont= true;
         while (loopcont){
              System.out.println("1. To Read the data");
              System.out.println("2. To Display the date");
              System.out.println("3. To Write the data");
              System.out.println("4. Exit");
              System.out.println("Enter your choice");
              choice = in.nextInt();
          switch (choice) {
                  case 1: {
                       String filename;
                       System.out.println("Enter the filename for the input file");
                       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                       filename = reader.readLine();
                       int num_records=0;
                       try{
                           num_records= readFile(filename,records);
                           System.out.println("The number of records read = " + num_records);
                      }
                      catch(IOException e){
                      e.printStackTrace();
                      }
                      break;
                  }
                  case 2: {
                        displayRecords(records,num_records);
                        break;

                  }
                 case 3: {
                     int num_rec_written =  writeFile(records,num_records);
                     System.out.println("The number of records writeen = " + num_rec_written);
                     break;
                 }

                case 4: {
                     loopcont = false;
                     break;
                }

               default: {
                   System.out.println("Incorrect input. Kindly enter again");
                    break;
               }
             }

          }

         }


review.length will give you the number of entries in the Customer array, this might solve your problem, another way to go would be to use the foreach loop

for(Customer c : review){
    // c is each customer in the array
}


If num_records is the size of the array, then num_records = review.length is true, so no need to pass num_records explicitly.

Note that in Java 5+, you can do the following:

for (Customer customer : review){
          System.out.println(customer .get_name());
          System.out.println(customer .get_balance());
          System.out.println(customer .get_accountid());
          System.out.println(customer .get_date_of_birth());
          System.out.println(customer .get_address());
          System.out.println(customer .get_phone_number());
}


Assuming I understand the question, just get the length of the array! i.e.

 int num_recourds = review.length;


There is answer above and i want to give you some advice. Try to avoid this code in your application:

      System.out.println(customer .get_name());
      System.out.println(customer .get_balance());
      System.out.println(customer .get_accountid());
      System.out.println(customer .get_date_of_birth());
      System.out.println(customer .get_address());
      System.out.println(customer .get_phone_number());

Replace it with Customer.getString() method.


What about using a collection class (like ArrayList) instead of an array? This class encapsulates the number of elements as well as the elements itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜