-
A newb question
Heres my code, i got it to so what i want with this array except the problem is i need to limit the user input between 0 and 50. Any help would be appreciated! This is my first post so forgive me if i put htis in the wrong section:::
import java.util.*;
public class Lab5 {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int[] array;
array = new int[12];
do{
System.out.println("Please enter the monthly income for January: ");
array[0] = input.nextInt();
System.out.println("Please enter the monthly income for February: ");
array[1] = input.nextInt();
System.out.println("Please enter the monthly income for March: ");
array[2] = input.nextInt();
System.out.println("Please enter the monthly income for April: ");
array[3] = input.nextInt();
System.out.println("Please enter the monthly income for May: ");
array[4] = input.nextInt();
System.out.println("Please enter the monthly income for June: ");
array[5] = input.nextInt();
System.out.println("Please enter the monthly income for July: ");
array[6] = input.nextInt();
System.out.println("Please enter the monthly income for August: ");
array[7] = input.nextInt();
System.out.println("Please enter the monthly income for September: ");
array[8] = input.nextInt();
System.out.println("Please enter the monthly income for October: ");
array[9] = input.nextInt();
System.out.println("Please enter the monthly income for November: ");
array[10] = input.nextInt();
System.out.println("Please enter the monthly income for December: ");
array[11] = input.nextInt();
System.out.println(array[0]);
}while(input.nextInt()<= 50 & >=0);
}
public static void divider(){
System.out.println("--------------------------------------");
}
}
-
Re: A newb question
To validate that the input is in range use an if statement that tests the input value against the end of the range values.
Do you know how to use arrays? Your code would be much simpler if you put the name of the months in an array and used a loop's index variable to get the name of the month from the array and to store the value that the user entered.
-
Re: A newb question
No im very new to arrays. This is the first time ive tried to use them :/ Thanks for the help :)