#How to access an array outside of an if statment?

1 messages · Page 1 of 1 (latest)

wispy tartan
#

I'm working on a school assignment where a user uses a menu to navigate through different features. One part of the menu has to be where users input data. I'm required to use arrays to store them, but I also need to access them in separate if statements. How can I do that?

steep phoenixBOT
#

<@&987246399047479336> please have a look, thanks.

steep phoenixBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

restive dock
#

declare the array before everything

#

share your code so far and I can show you

wispy tartan
#
public class SortingRectangles {

    @SuppressWarnings("null")
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Welcome to Rectangle Sorter.");
        boolean quit = false;
        while(!quit)
        {
            printMenu();
            int input = keyboard.nextInt();
            int numRectangles = 0;
            double[] rectangleLength;
            double[] rectangleWidth;
            double[] rectangleArea;
            
            if(input==1)
            {
                System.out.println("How many rectangles would you like to sort? Enter a non-zero number");
                numRectangles = keyboard.nextInt();
                while(numRectangles<=0)
                {
                    System.out.println("That is an invalid number. Enter a non-zero number.");
                    numRectangles = keyboard.nextInt();
                }
                
                rectangleLength = new double [numRectangles];
                rectangleWidth = new double [numRectangles];
                rectangleArea = new double [numRectangles];
                
                for(int i = 0;i<numRectangles;i++)
                {
                    System.out.println("Enter the length of rectangle "+(i+1));
                    rectangleLength[i] = keyboard.nextDouble();
                    System.out.println("Enter the width of rectangle "+(i+1));
                    rectangleWidth[i] = keyboard.nextDouble();
                }
                System.out.println("Your data has been succesfully stored.");
                //double[] rectangleArea = new double [numRectangles];
                for(int i = 0;i<numRectangles;i++)
                {
                    rectangleArea[i] = rectangleLength[i]*rectangleWidth[i];
                }
                
            }
            else if(input==2)
            {
                boolean hasSwapped = true;
                while(hasSwapped)
                {
                    hasSwapped = false;
                    for(int i = 0;i<rectangleArea.length-1;i++)
                    {
                        if(rectangleArea[i] > rectangleArea[i+1])
                        {
                            double temp = rectangleArea[i];
                            rectangleArea[i] = rectangleArea[i+1];
                            rectangleArea[i+1] = temp;
                            hasSwapped = true;
                        }
                    }
                }
                System.out.println("The rectangles' areas smallest to largest is:");
                for(int i = 0;i<numRectangles;i++)
                {
                    System.out.println(rectangleArea[i]);
                }
            }
            else if(input==3)
            {
                
            }
            else if(input==4)
            {
                
            }
            else if(input==5)
            {
                
            }
            else if(input==6)
            {
                
            }
            else if(input==7)
            {
                break;
            }
            else if(input<1||input>7)
            {
                System.out.println("Invalid choice.");
            }
        }
        System.out.println("Goodbye!");
        System.exit(0);

    }
    public static void printMenu()
    {
        System.out.println("Enter \"1\" to enter rectangle data.");
        System.out.println("Enter \"2\" to sort rectangle areas from smallest to largest.");
        System.out.println("Enter \"3\" to sort rectangle areas from largest to smallest.");
        System.out.println("Enter \"4\" to show the average rectangle area.");
        System.out.println("Enter \"5\" to show the minimum rectangle area.");
        System.out.println("Enter \"6\" to show the maximum rectangle area.");
        System.out.println("Enter \"7\" to quit the program.");
    }

}```
steep phoenixBOT
# wispy tartan ```import java.util.Scanner; public class SortingRectangles { @SuppressWarn...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;

public class SortingRectangles {
  @SuppressWarnings("null") public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Welcome to Rectangle Sorter.");
    boolean quit = false;
    while (!quit) {
      printMenu();
      int input = keyboard.nextInt();
      int numRectangles = 0;
      double [] rectangleLength;
      double [] rectangleWidth;
      double [] rectangleArea;
      if (input == 1) {
        System.out.println("How many rectangles would you like to sort? Enter a non-zero number");
        numRectangles = keyboard.nextInt();
        while (numRectangles <= 0) {
          System.out.println("That is an invalid number. Enter a non-zero number.");
          numRectangles = keyboard.nextInt();
        }
        rectangleLength = new double [numRectangles] ;
        rectangleWidth = new double [numRectangles] ;
        rectangleArea = new double [numRectangles] ;
        for (int i = 0; i < numRectangles; i++) {
          System.out.println("Enter the length of rectangle " + (i + 1));
          rectangleLength[i]  = keyboard.nextDouble();
          System.out.println("Enter the width of rectangle " + (i + 1));
          rectangleWidth[i]  = keyboard.nextDouble();
        }
        System.out.println("Your data has been succesfully stored.");
        //double[] rectangleArea = new double [numRectangles];
        for (int i = 0; i < numRectangles; i++) {
          rectangleArea[i]  = rectangleLength[i]  * rectangleWidth[i] ;
        }
      }
      else if (input == 2) {
        boolean hasSwapped = true;
        while (hasSwapped) {
          hasSwapped = false;
          for (int i = 0; i < rectangleArea.length - 1; i++) {
            if (rectangleArea[i]  > rectangleArea[i + 1] ) {
              double temp = rectangleArea[i] ;
              rectangleArea[i]  = rectangleArea[i + 1] ;
              rectangleArea[i + 1]  = temp;
              hasSwapped = true;
            }
          }
        }
        System.out.println("The rectangles' areas smallest to largest is:");
        for (int i = 0; i < numRectangles; i++) {
          System.out.println(rectangleArea[i] );
        }
      }
      else if (input == 3) {
      }
      else if (input == 4) {
      }
      else if (input == 5) {
      }
      else if (input == 6) {
      }
      else if (input == 7) {
        break ;
      }
      else if (input < 1 || input > 7) {
        System.out.println("Invalid choice.");
      }
    }
    System.out.println("Goodbye!");
    System.exit(0);
  }
  public static void printMenu() {
    System.out.println("Enter \"1\" to enter rectangle data.");
    System.out.println("Enter \"2\" to sort rectangle areas from smallest to largest.");
    System.out.println("Enter \"3\" to sort rectangle areas from largest to smallest.");
    System.out.println("Enter \"4\" to show the average rectangle area.");
    System.out.println("Enter \"5\" to show the minimum rectangle area.");
    System.out.println("Enter \"6\" to show the maximum rectangle area.");
    System.out.println("Enter \"7\" to quit the program.");
  }
}
wispy tartan
#

i tried declaring the array above everything but creating the arrays dependent on user input under one of the if statements

restive dock
steep phoenixBOT
# restive dock here

I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍

restive dock
#

the problem is the in one branch you do not initialize rectangleArea

#

so you need to make sure that it is always definitely assigned

wispy tartan
#

thank you!!

#

under the second if statement is there any reason the bubble sort would not work?

#

@restive dock

restive dock
#

yes

#

you only do one scan

#

you need to keep swapping until it is sorted

#

also

#

you want to move the variable declaration for rectangeArea up higher

#

since it will be reset every time through the loop

#
                boolean sorted = false;
                while(!sorted)
                {
                    sorted = true;
                    for(int i = 0;i<rectangleArea.length;i++)
                    {
                        if(rectangleArea[i] > rectangleArea[i+1])
                        {
                            double temp = rectangleArea[i];
                            rectangleArea[i] = rectangleArea[i+1];
                            rectangleArea[i+1] = temp;
                            sorted = false;
                        }
                    }
                }
#
        int numRectangles = 0;
        double[] rectangleArea = new double[0];

        boolean quit = false;
        while(!quit)
        {
            printMenu();
            int input = keyboard.nextInt();



wispy tartan
#

thanks again! i found that declaring my rectnagleArea variable further up fixed the problem and that i needed to set the for loop as for(int i = 0;i<rectangleArea.length-1;i++)