I need a code that reads in a text file into an object Computers and sorts the specific indexes in descending order, as you can see the first one vendors is already sorted so you just need to print that one. Then in my case 2 I need to print the fifth index in the lines called mmax in descending order, case 3 prints the sixth index cach in descending order case 4 prints the ninth prp in descending order finally five exits. Im having a few errors with my code and after consorting with my book and google Im not sure what else to do for this program. Here are a couple lines of the text file its long so I only will paste enough to get the gist of the file.

Machine.txt
adviser,32/60,125,256,6000,256,16,128,198,199
amdahl,470v/7,29,8000,32000,32,8,32,269,253
amdahl,470v/7a,29,8000,32000,32,8,32,220,253
amdahl,470v/7b,29,8000,32000,32,8,32,172,253
amdahl,470v/7c,29,8000,16000,32,8,16,132,132
amdahl,470v/b,26,8000,32000,64,8,32,318,290
amdahl,580-5840,23,16000,32000,64,16,32,367,381
amdahl,580-5850,23,16000,32000,64,16,32,489,381
amdahl,580-5860,23,16000,64000,64,16,32,636,749
amdahl,580-5880,23,32000,64000,128,32,64,1144,1238
apollo,dn320,400,1000,3000,0,1,2,38,23
apollo,dn420,400,512,3500,4,1,6,40,24
basf,7/65,60,2000,8000,65,1,8,92,70
basf,7/68,50,4000,16000,65,1,8,138,117
bti,5000,350,64,64,0,1,4,10,15
bti,8000,200,512,16000,0,4,32,35,64

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


class Computer implements Comparable<Computer>{
    String vendors;
    int[] mmax;
    int cach;
    int prp;
    public Computer(int[] value)
    {
        this.mmax=value;
    }

    public String getVendor()
    {
        return vendors;
    }
    public void setVendor(String vendors) {
        this.vendors = vendors;
    }

    public int[] getMmax() {
        return mmax;
    }

    public void setMmax(int[] mmax) {
        this.mmax = mmax;
    }

    public int getCach() {
        return cach;
    }

    public void setCach(int cach) {
        this.cach = cach;
    }

    public int getPrp() {
        return prp;
    }

    public void setPrp(int prp) {
        this.prp = prp;
        }


    public int compareTo(Computer x)
    {
        return getMmax()-x.getMmax();
    }
  }
public class Prog4
{
    static Scanner input;
    static File filename;
    /**
     * This function displays the menu for the user to choose an option from 
     */
    public void menu()
    {
        System.out.println("Option 1: Sort by VENDOR: ");
        System.out.println("Option 2: Sort decreasing number by MMAX: ");
        System.out.println("Option 3: Sort decreasing number by CACH: ");
        System.out.println("Option 4: Sort decreasing number by PRP: ");
        System.out.println("Option 5: Quit program");
    }
      /**
       * Constructor to handle the cases in the menu options
     * @throws FileNotFoundException 
     * @throws IOException 
       */
      public Prog4() throws FileNotFoundException
      {
           //Accepts user input
        Scanner in=new Scanner(System.in);

           //calls the menu method
        menu();
           //Initializes the run variable making the program loop until the user terminates the program
        Boolean run=true;

           //While loop
           while(run)    
           {
            switch (in.nextInt()) 

            {
                case 1:
              System.out.println("Option 1 selected");
              System.out.println("Sorted by vendor:");

              filename = new File("machine.txt");
            //Instantiate Scanner s with f variable within parameters
            //surround with try and catch to see whether the file was read or not
            try {
                 input = new Scanner(filename);
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            }
            //Instantiate a new Array of String type
            String array [] = new String[10];
            //while it has next ..
            while(input.hasNext()){
                //Initialize variable 
                int i=0;
                //store each word read in array and use variable to move across array
                array[i]=input.next();
                //print 
                System.out.println( array[i]);
                //so we increment so we can store in the next array index
                i++;
            } 


                case 2:
                    System.out.println("Press any key to continue");
                    Scanner input2=new Scanner(System.in);
                    String x=input2.nextLine();
                    if(x.equals(0))continue;
                    System.out.println("Option 2 selected") ;

                ArrayList<Computer> al=new ArrayList<Computer>();
                Computer comp=new Computer(0);
                filename = new File("machine.txt");
                //Instantiate Scanner s with f variable within parameters
                //surround with try and catch to see whether the file was read or not

                try {
                     input = new Scanner(filename);
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                }

                while(input.hasNextLine()){
                    Scanner s2=new Scanner(input.nextLine());
                    //String s=s2.next();
                    //comp.setVendor(s);
                    //int i=Integer.parseInt(s2.next());
                    //int i=s2.nextInt();
                    int i=0;
                    String array3[]=new String[1000];
                    array3[i]=input.next();

                    System.out.println(array3[i]);

                    comp.setMmax(i);
                    //comp.setCach(i);
                    //comp.setPrp(i);

                al.add(comp);   
                }
                int array2[]=new int[1000];
                for(int i=0;i<al.size();i++)
                {
                    comp=al.get(i);
                    int a=comp.getMmax();
                    array2[i]=a;
                }





                case 3:
                    System.out.println("Press any key to continue");
                    Scanner input3=new Scanner(System.in);
                    String x1=input3.nextLine();
                    if(x1.equals(0))continue;
                System.out.println("Option 3 selected");

                case 4:
                    System.out.println("Press any key to continue");
                    Scanner input4=new Scanner(System.in);
                    String x2=input4.nextLine();
                    if(x2.equals(0))continue;
                System.out.println("Option 4 selected");

                //End of second case
                case 5:
                System.out.println("Option 5 selected");
                System.out.println("Input any key to quit");
                Scanner input5=new Scanner(System.in);
                String x3=input5.nextLine();
                if(x3.equals(0))continue;
                    System.out.println("Program terminated");
                    //Terminates user input
                    run=false; 
                   break;
                  //default case if one of the options selected were not allowed
                default:
                    System.out.println("Not one of the options 1-5");
                    System.out.println("Program terminated.");
                  break;
                    }    
              }
      }

      /**
       * Main method
       * @param args
     * @throws FileNotFoundException 
       */

      public static void main(String[] args) throws FileNotFoundException
      {
          //Calls the constructor
          new Prog4();
          //static Scanner input;

      }

      public static void quickSort(int arr[], int left, int right) 
      {
      if(left<right)
        {
            int q=partition(arr,left,right);
            quickSort(arr,left,q);
            quickSort(arr,q+1,right);
        }
      }
        private static int partition(int arr[], int left, int right)
           { 
                int x = arr[left];
                int i = left-1 ;
                int j = right+1 ;
                while (true) 
                {
                    i++;
                while ( i< right && arr[i] < x)
                    i++;
                    j--;
                while (j>left && arr[j] > x)
                    j--;
                if (i < j)
                    swap(arr, i, j);
                else
                    return j;
                }
        }
        private static void swap(int[] arr, int i, int j) {

            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

       }

There's a lot of very tangled code there, so maybe best to start again and go one step at a time.
First, you need to read the file and create an array of Computer objects. Read each line, extracting each of the values on that line to local variables, then use those to create a new Computer object, then add that object to the array.

Now print the contents of the array to confirm that you have read it properly.

Then, and only then, move on to the different ways of sorting that array. You can get more help here when you get to that step.

Your code doesn't compile.

In class "Computer":

In (line 52), "compareTo", you state that your return type is "int". However, the return type for "getMmax" is "int []" (an int array). You attempt to subtract two int[] (and return the result).

In class "Prog4":

  • You attempt to create a new instance of "Computer" (line 127) Computer comp=new Computer(0); , but your only constructor in "Computer" accepts int[] (an int array).

  • In line 151, you do the following: comp.setMmax(i); , however "setMmax" accepts "int[]" (an int array) not "int" (an int).

  • In line 161, int a=comp.getMmax(); . "getMmax" returns "int[]" not "int".

Line 75, public Prog4() throws FileNotFoundException, delete throws FileNotFoundException, your program already catches this error, and it is not thrown again.

Line 98-103 & 132-137, a user doesn't want to see a stack trace, he/she wants to see a "friendly" error message. A stack trace gives the impression that something has gone terribly wrong. Try something like the following:

        filename = new File("machine.txt");

        try {
                input = new Scanner(filename);
        } catch (FileNotFoundException e) {

            System.out.println();
            System.out.println("Error: File: '" + filename.getAbsolutePath() + "' not found.");
            System.out.println();
            return;

        }//catch

Reading from a file is slow, if possible you only want to read the file once--not over and over again.

Many of your "case" statements are missing the "break;" statement.

Your indentation is inconsistent, and makes your code difficult to follow.

Line 234-236, you use a while statement without braces, only the first statement will be executed. Always use braces, even if not required. It will help to prevent unexpected results. Click Here
to read about some pitfalls in Java.

There may be more things, but this should help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.