Reputation: 13
I am trying to create a program that finds the largest number in array of integers than are inputted by the user using JOptionPane. Here is the code I have s far but I am getting errors when compiling.
import javaz.swing.JOptionPane;
public class Week9Largest {
public static void main(String[] args) {
int [] x = new int [7] ;
string myString;
int myInt;
for(int i = 0; i <= 6; i++){
myString = JOptionPane.showInputDialog (null, "Enter " + "integer " + (i + 1));
myInt = Integer.parseInt(myString);
x[i] = myInt;
}
int largest = Integer.MIN_VALUE;
for (int i=0;i<numbers.length;i++){
if(myInt[i]>largest){
largest = myInt[i];
}
}
System.out.println("Largest number in array is : " +largest);
}
Upvotes: 0
Views: 2657
Reputation: 837
1)It is javax
not javaz
import javaz.swing.JOptionPane
to
import javax.swing.JOptionPane
2) It is String
not string
string myString;
to
String myString;
3) You are Missing a closing
curly Brace }
. Add one }
at the end of your program.
4) Apart from this, there is logical
error in your code.
Correct Code:
import javax.swing.JOptionPane;
public class Week9Largest {
public static void main(String[] args) {
String myString;
int myInt,numbers[] = new int[7];
for(int i = 0; i <= 6; i++){
myString = JOptionPane.showInputDialog (null, "Enter " + "integer " + (i + 1));
myInt = Integer.parseInt(myString);
numbers[i] = myInt;
}
int largest = Integer.MIN_VALUE;
for (int i=0;i<numbers.length;i++){
if(numbers[i] > largest){
largest = numbers[i];
}
}
System.out.println("Largest number in array is : " +largest);
}
}
You are using x
for no reason, Also you are using numbers.length
while numbers
is not defined.
Instead Refer the above code, Declare numbers
or use x
as an array and copy the user input values into that variable
, then display the largest value using the
for
loop.
Upvotes: 0
Reputation: 8387
Correct this:
string myString;
To:
String myString;
Here is your code after correct some issue it's ok:
public static void main(String[] args) {
int[] x = new int[7];
String myString;
int myInt;
for (int i = 0; i <= 6; i++) {
myString = JOptionPane.showInputDialog(null, "Enter " + "integer "
+ (i + 1));
myInt = Integer.parseInt(myString);
x[i] = myInt;
}
int largest = Integer.MIN_VALUE;
for (int i = 0; i < x.length; i++) {
if (x[i] > largest) {
largest = x[i];
}
}
System.out.println("Largest number in array is : " + largest);
}
Because you has mistakes in this loop:
for (int i=0;i<numbers.length;i++){
if(myInt[i]>largest){
largest = myInt[i];
}
After correction became:
for (int i = 0; i < x.length; i++) {
if (x[i] > largest) {
largest = x[i];
}
Upvotes: 2