I'm having some issues with arrays in java. I am making an app where a user adds input to a menu by selecting option 1, which I am using a switch statement for. This array needs to be accessible to all of the other switch cases so I have declared it outside of case 1:
//Menu loop
//variable & array declaration
int myMonths[] = new int[amountOfProjects];
int index = 0;
int num;
while(choice !=6){ //while option 6 is not chosen keep showing the menu
switch (choice){
case 1:
The array length is the amount of projects that the user wishes to store in the app. For case 2 I need to display the elements/value of the indeces of the array. If up until this point the user has not entered anything, it should display a message stating this. Here is my code:
case 2:
if(myMonths.length > 0){
// print the values
for(int i=0; i < myMonths.length; i ){
System.out.println(myMonths[i] " ");
}
}else {
System.out.println("No values entered");
}
break;
Problem My code will not execute the else statement in case 2. It just prints out zeros for the array length that was assigned by the user, so I am guessing that I am just asking it to check for the array length and not the values of the elements. Is there a way I can have the code execute an error statement if the array is empty?
To add I'm not sure if this is important, but I have used a setter method on a separate file for the array:
public class MenuTestClass{
private int myMonths[];
public MenuTestClass(){
myMonths = new int[5];
}
public MenuTestClass(int[] myMonths){
this.myMonths = myMonths;
}
public void setMyMonths(int[] values){ //declare setter method
myMonths = values;
}
Thanks in advance! You have all been so helpful to me so far.
Update:
int myMonths[] = new int[amountOfProjects];
int index = 0;
int num;
while(choice !=6){ //while option 6 is not chosen keep showing the menu
switch (choice){
case 1:
int n = 1; //int n = number of projects
Scanner sc = new Scanner(System.in);
//myMonths = new int[amount];
System.out.println("** Only projects with a duration between 2 and 12 months can be included **");
System.out.println("What was the duration of your projects in months?");
for(int i=0; i<1; i ){
int a = sc.nextInt();
//display error message
if((a < 2) || (a > 12)){
System.out.println(" Please enter an amount between 2 and 12 months. ");}
//add to the array
else{myMonths[index ] = a;}
}
calc.setMyMonths(myMonths); //creating the object
break;
case 2:
if(myMonths.length > 0){
// print the values
for(int i=0; i < myMonths.length; i ){
System.out.println(myMonths[i] " ");
}
} else {
System.out.println("No values entered");
}
break;
case 3:
//display all elements with the same state
//get number user wishes to search for
boolean found = false;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the number you wish to search for");
//read user input
num = input.nextInt();
//traverse array
int k = 0;
for(k=0; k < myMonths.length; k ){
if(myMonths[k] == num){
found = true;
System.out.println("Your value has been found at the following indices: " k);
}
}
if(!found){System.out.println("not found");}
break;
//display averages
case 4:
System.out.println("Choice 4 selected\n");
// processing: invoke/call the method calculateAverage() to calculate the average value of the array's elements
calc.calculateAverage();
// output
// use the getter method getAverageCanBeCalculated() to retrieve the value which shows if the average could be calculated
boolean isMenuCalculated = calc.getAverageCanBeCalculated();
if (isMenuCalculated){ // average has been calculated (i.e. there were numbers in the array)
double avg = calc.getAverage();// use the getter method to retrieve the average value
// display the average value
System.out.println("average is: " avg);
}
else { // the instance variables f the
System.out.println("the object's array instance variable has no numbers (i.e. empty array)");
}
break;
case 5:
//invoke/call the method calculateMax() to calculate the maximum element of the array
calc.calculateMax();
// use the getter method to retrieve the maximum value
int max = calc.getMax();
// display the maximum value
System.out.println("maximum is " max);
break;
default:
System.out.println("Please enter a valid number");
}
//display menu again
displayMenu();
//Get user choice
choice = in.nextInt();
}
CodePudding user response:
There are three ways you can handle this,
Use apache library
import org.apache.commons.lang.ArrayUtils;
ArrayUtils.isEmpty(myMonths);
2). Use a boolean flag and set it as true whenever the values are entered into the array. So that you can check that flag.
3). Traverse through the array and check if the values in the array is not 0.
Let me know in case you require further help.
CodePudding user response:
Are you sure that the values are getting updated? According to the posted Code the "while(" loop has no exit condition. Also there seems to be no way to to finish the loop.
I would suggest to add a printout/breakpoint be hind the beginnig of the while loop to see if your "choice" is actually updated.
...
while (choice != 6) { // while option 6 is not chosen keep showing the menu
System.out.println(choice);
...