0

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);
 } 
Abdelhak
8,4174 gold badges24 silver badges37 bronze badges
asked Dec 7, 2015 at 12:58
3
  • String myString; - use capital S for String and javax.swing.JOptionPane (not javaz.) Commented Dec 7, 2015 at 13:00
  • You're also missing a closing curly bracket } Commented Dec 7, 2015 at 13:02
  • When posting a question you should state the expected and observed results. The expected result is obvious here, but you should report what compiler errors you see. Commented Dec 7, 2015 at 13:06

2 Answers 2

2

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];
 }
answered Dec 7, 2015 at 13:02
0
0

There are Many Errors in Your Code:

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.

answered Dec 7, 2015 at 13:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.