#How to access an array outside of an if statment?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
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.");
}
}```
Detected code, here are some useful tools:
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.");
}
}
i tried declaring the array above everything but creating the arrays dependent on user input under one of the if statements
here
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍
the problem is the in one branch you do not initialize rectangleArea
so you need to make sure that it is always definitely assigned
Book teaching how to write modern and effective Java. It is maintained by the community, anyone can contribute.
thank you!!
under the second if statement is there any reason the bubble sort would not work?
@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();
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++)