'Creating a menu with an array accessible in all classes [duplicate]
I am trying to create a menu in java. First I have to display all the options from 1-6, and get the users selection. Each of the options have a different function to carry out.1st - add items and store them in an array2nd - display all items of the array ... and so onI have created the array for option 1 but I cannot figure out how I can display the items under option 2. Is there a way I can make this array accessible for each option selected? Any help would be greatly appreciated! Here's the code from my first file so far:
import java.util.Scanner;
import java.util.ArrayList;
public class TestProject
{public static void main(String[] args){System.out.println("Please provide the duration for up to five projects.");
//print menu
System.out.println("Application Menu - Item type: Project");
System.out.println("1- Add an item" + "\n" + "2 - Display all items" + "\n" + "3- Search and display all the items whose mandatory state is the same given value" + "\n" + "4 -Calculate and display the average value of the mandatory state of all the items entered until that point" + "\n" + "5 - Item with the highest mandatory state" + "\n" + "6 - Exit application");
// Create scanner object & get user inputScanner choice = new Scanner(System.in);System.out.println("Please choose by entering the number of your choice");
//Read user inputint userChoice = choice.nextInt();
# //switch statement
switch (userChoice){
case 1:
Scanner myInput = new Scanner(System.in);
TestJavaClass get = new TestJavaClass();
//declare an int\[\] variable
int\[\] myMonths;
//create an array of int elements
myMonths = new int\[1\];
// counter controlled loop
for (int i = 0; i \< 5; i++){
int order = i + 1;
System.out.print("enter value " + myMonths + " : ");
myMonths\[i\] =myInput.nextInt();
}
break;
case 2:
for (int i=0; i \< myMonths.length; i++){
System.out.println("the element at index " + i + " is: " + myMonths\[i\]);
}
System.out.println("Choice 2: Display all items");
break;
case 3:
System.out.println("Choice 3");
break;
case 4:
System.out.println("Choice 4");
break;
case 5:
System.out.println("Choice 5");
break;
case 6:
System.out.println("Choice 6");
break;
default:
System.out.println("Please enter a valid number");
}
}
}
I get this error when I try to compile:TestProject.java:41: error: variable myMonths might not have been initialized for (int i=0; i < myMonths.length; i++){ ^
I have tried adding methods to each of the switch case but it seems I cannot access the array from case 1.
Solution 1:[1]
The scope of the variables declared within each case clause is the whole switch statement. Therefore, even you have declared it within case 1, you can still use it within case 2. But you have initialized myMonths variable within case 1. If the user select case 2 as first choice, there is a possibility that the myMonths variable to be null. So you are getting compiler issues.
You can define myMonths array outside of your switch statement to solve this.
int[] myMonths = new int[1];
//switch statement
switch (userChoice)
{
case 1:
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
