I am trying to display Largest value and Smallest value in an array but when I execute I get Largest value as 0. Not able to solve the issue. Any solution is much appreciated. I have edited the code again. Please have a look at it. Following is the code:
package simpleArray;
import javax.swing.JOptionPane;
class ArrayInteractive {
static String ch = "Q";
static boolean flag = true;
static String a[];
static int b[];
static int size;
static int max;
static int min;
public static void main(String[] args) {
while (flag) {
ch = read(" Welcome to Array Interactive \n"
+ "A - Enter Size of Array" + "\n"
+ "B - Enter values in Array" + "\n"
+ "C - Find the Largest" + "\n"
+ "D - Find the Smallest" + "\n"
+ "E - Find the value position" + "\n"
+ "Q - Quit" + "\n");
switch (ch) {
case "a":
case "A":
EnterSize(readvalue("Please enter size of the array"));
break;
case "b":
case "B":
EnterValues();
break;
case "c":
case "C":
Largest();
break;
case "d":
case "D":
Smallest();
break;
case "e":
case "E":
Position (readvalue("Enter the value"));
break;
case "q":
case "Q":
JOptionPane.showMessageDialog(null, "Thank you for using Interactive Array");
flag = false;
break;
}
}
}
static String read(String s) {
String r = JOptionPane.showInputDialog(s);
return (r == null) ? "Q" : r;
}
static int readvalue(String s) {
String v1 = JOptionPane.showInputDialog(s);
int v = 0;
try {
v = Integer.parseInt(v1);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid size entered");
}
return v;
}
static void EnterSize(int v) {
a = new String[v];
size = v;
}
static void EnterValues() {
b = new int[size];
for (int i = 0; i < size; i++) {
b[i] = Integer.parseInt(JOptionPane.showInputDialog(a[i]));
}
}
static void Largest() {
max = b[0];
for (int i = 1; i < size; i++) {
if (b[i] > max) {
max = b[i];
}
}
JOptionPane.showMessageDialog(null, "The Largest value is " + max);
}
static void Smallest() {
min = b[0];
for (int i = 0; i < size; i++) {
if (b[i] < min) {
min = b[i];
}
}
JOptionPane.showMessageDialog(null, "The Smallest value is " + min);
}
}