Java array index out of bounds
•
Java
I just started a unit in arrays, and I gave some sample code to see this very basic array starter Basically, all I have to do is make two arrays and ask about the temperature on that day of the week After collecting the information, it will simply spit out a string like this
The temperature on Monday is 16 degrees
The temperature on Tuesday is 18 degrees
... etc
Based on what I understand from the sample code I received, I'm doing the right thing But when I tried to run the program (in NetBeans), I got this error
This is the code:
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
String temp[]= new String[7];
String day[]= new String[7];
day[1]=("Monday");
day[2]=("Tuesday");
day[3]=("Wednesday");
day[4]=("Thursday");
day[5]=("Friday");
day[6]=("Saturday");
day[7]=("Sunday");
for (int i=0; i <7; i++){
System.out.println("Please enter the temperature for" + day[i]);
temp[i]=br.readLine();
}
for (int i=0; i <7; i++){
System.out.println("The high temperature on " + day[i]+ " was "+ temp[i]);
}
}
}
Solution
Arrays begin at zero Therefore, specify your first value as day [1] = ("Monday"); Yes, it should be day [0] = ("Monday"); Hope to help
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
