-3

I need a way to print 3 these arrays...

public static void viewCatalog(){
    String[] description = new String[15];
    description[0]= "Alumni Drink ware";
    description[1]= "Binders";
    description[2]= "Bookbag";
    description[3]= "Fabulous Desserts";
    description[4]="Folders";       
    description[5]="Gift Cards";        
    description[6]="Highlighters";
    description[7]="Jackets";
    description[8]="JAVA Programming";
    description[9]="Network Solutions";
    description[10]="Pencils";        
    description[11]="Pens";
    description[12]="Shorts";
    description[13]="Sweatshirts";
    description[14]="Tshirts";
    description[15]="Web Design Ideas";


    String[] category = new String[15];
    category[0]= "Gifts";
    category[1]= "School Supplies";
    category[2]= "School Supplies";
    category[3]= "Textbooks";
    category[4]="School Supplies";       
    category[5]="Gifts";        
    category[6]="School Supplies";
    category[7]="Campus Gear";
    category[8]="Textbooks";
    category[9]="Textbooks";
    category[10]="School Supplies";        
    category[11]="School Supplies";
    category[12]="Campus Gear";
    category[13]="Campus Gear";
    category[14]="Campus Gear";
    category[15]="Textbooks";

    double[] price = new double[15];
    price[0]= 25.00;
    price[1]= 3.00;
    price[2]= 20.00;
    price[3]= 25.00;
    price[4]=1.00;       
    price[5]=25.00;        
    price[6]=2.00;
    price[7]=65.00;
    price[8]=150.00;
    price[9]=75.00;
    price[10]=1.00;        
    price[11]=2.00;
    price[12]=10.00;
    price[13]=40.00;
    price[14]=15.00;
    price[15]=55.00;

    System.out.println(Arrays.toString(description));
    System.out.println(Arrays.toString(category));
    System.out.println(Arrays.toString(price));
}

In this method...

public static void sort(){
    System.out.println("How would you like to sort?");
    System.out.println("a) Increasing Price \n"
            + "b) Decreasing Price \n"
            + "c) Description \n"
            + "d) Category \n"
            + "Option: ");

    Scanner input = new Scanner(System.in); 
    String option=input.next();

    if (option=="a") {



    }

}

I need to be able to print them in varying orders, but right now I can't get them to print at all. I keep getting errors that say "class not found" etc. I've tried to pass by value but I don't think I'm doing it right. Please help! I'm not good at Java by any means and need all of the help that I can get.

Allie Carroll
  • 9
  • 1
  • 1
  • 3
  • [`Use equals() to compare string`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – sam Nov 10 '15 at 18:52
  • Your first issue is that you are defining the arrays to be too small. Arrays are zero-based (ie., starting at zero). That means an array of size 15 holds the element 0,1,2...,13,14 – Jamie Nov 10 '15 at 18:54
  • Do you have 2 separate programs? It is inefficient to pass arrays from one function to another by printing them to the console, and then trying to read them back from the console. That would only work if you have one program (viewCatalog) piped into the other program (sort) on the command line. – Jamie Nov 10 '15 at 18:59

2 Answers2

0

Take out the declaration of your arrays out of your viewCatalog() method (After fixing the array length problem Jaime mentioned in comments). Continue poplulating them in that mehtod just take out where you declare them. Then you can access the public static arrays in your sort method. Something like this.

public class Main{

    public static String[] description;
    public static String[] category;
    public static double[] price;

    public static void viewCatalog(){
        //Populate Arrays
        description[0] = "foo";
    }
    public static void sort(){
        //Sort and print
        System.out.println(description[0]);
    }
}
gonzo
  • 2,103
  • 1
  • 15
  • 27
0

A possible solution is to make a class and store your array elements as fields

clas Item extends Comparable{
   private double price;
   private String desc;
   private String category;

   int sortSelection = (your input here);

   public int compareTo(item i){
       switch(sortSelection){
            case 1: // sort by price
            case 2: // sort by desc
            case 3: // sort by category
       }
   }
}

Then to make an array of instances of Item and simply call sort each time.

A good example is shown here http://examples.javacodegeeks.com/java-basics/java-comparable-example/

samvel1024
  • 1,123
  • 4
  • 15
  • 39