-2

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);

}

}

  • 2
    You are recreating the array of values in each methods. It's pretty obvious that you will loose all the values. You also have other issues, but start by fixing that! – Stefano Sanfilippo Nov 15 '14 at 14:43
  • 1
    You'll set your `size` and then in your `Largest()` method you create a new `int[]` at the length of size, so what's happening is the `int[]` is populated with as many `0`s as the number `size`, so if `size` is `5`, the array is `[0, 0, 0, 0, 0]`. – CoderMusgrove Nov 15 '14 at 14:47
  • @StefanoSanfilippo If I dont declare array in each method then I get null pointer exception. So really not aware how to handle it; – Ashwiinn Karaangutkar Nov 15 '14 at 14:47
  • 1
    Please read this page: http://www.java-made-easy.com/variable-scope.html. A any other page that describes `variable scopes` in Java. – Tom Nov 15 '14 at 14:47
  • @CoderMusgrove so how I can use the array in which I have saved values in method EnterValues(). Please let me know If you know the solution as I am new to Java. – Ashwiinn Karaangutkar Nov 15 '14 at 14:49
  • u may do a sort of the array using arrays.sort and then ur first and last value gives the max and min .. thats after you have made sure there's data in the array itself.. see this link for sorting though http://stackoverflow.com/questions/8938235/java-sort-an-array – nathandrake Nov 15 '14 at 14:50

2 Answers2

0

If you create the array with new int[n]; it will contain n zeros. So if there are no other numbers except zeros the largest number will be zero.

In addition to your comment:

a) Return the created array from the EnterValues()-method:

static int[] EnterValues(){
    int b[] = new int[size];
    for(int i=0; i<size; i++){
        b[i]=Integer.parseInt(JOptionPane.showInputDialog(a[i]));
    }
    return b;
}

b) Save it somewhere within the main-method:

public static void main(String[] args){
     int[] values;
     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"
                + "Q - Quit" + "\n");

        switch (ch) {
            case "a":
            case "A":
                EnterSize(readvalue("Please enter size of the array"));
                break;

            case "b":
            case "B":
                values = EnterValues();
                break;

            case "c":
            case "C":
                Largest(values);
                break;

            case "d":
            case "D":
                Smallest();
                break;

            case "q":
            case "Q":
                JOptionPane.showMessageDialog(null, "Thank you for using Interactive Array");
                flag = false;
                break;

        }
    }
}

c) and pass it to the Largest()-method

static void Largest(int[] b) {

    for (int i = 1; i < size; i++) {
        max = b[0];
        if (b[i] > max) {
            max = b[i];
            JOptionPane.showMessageDialog(null, "The Largest value is " + max);

        } else {
            JOptionPane.showMessageDialog(null, "The Largest value is " + max);

        }
    }
}
masinger
  • 775
  • 6
  • 13
  • I am getting Null pointer exception If i dont create an array in each method. Is there any solution to handle this? – Ashwiinn Karaangutkar Nov 15 '14 at 14:57
  • yes. You could return the array from the EnterValues()-method and save it within the scope of the main-method. If you call Smallest you can pass it as a parameter. – masinger Nov 15 '14 at 17:45
0

Part of the problem is that there is no guarantee that the user will have added values before searching for the largest or smallest numbers. Int arrays have a default value of 0 when initialized, so if you aren't adding values to the array before searching, you will naturally get zero as your largest and smallest. I would recommend against using the switch for this reason.

redeagle47
  • 1,355
  • 4
  • 14
  • 26
  • There is the problem in returning `0` for the largest and the smallest value if the user haven't entered any number yet? – Tom Nov 15 '14 at 14:51
  • if the asker expects there to be a value other than zero, yes. And i was only focusing on the switch, others have touched on the heart of the problem, which is that the asker is recreating the array in each method call. – redeagle47 Nov 15 '14 at 14:53
  • @redeagle47 I am getting Null pointer exception If i dont create an array in each method. Is there any solution to handle this? – Ashwiinn Karaangutkar Nov 15 '14 at 14:57
  • He certainly expects the highest value, which he cannot get, due to the messed up code. I can't see any clue that he tries to avoid `0` for all reasons. – Tom Nov 15 '14 at 14:57
  • 1
    have you looked at the variable scope page that Tom linked to? It looks like a pretty good explanation of how to solve that problem. – redeagle47 Nov 15 '14 at 14:59
  • @AshwiinnKaraangutkar I've given you a link in a comment. Read it, understand it and then fix your code. Currently you're creating completely new arrays in each method. They cannot contain something else than `0`. – Tom Nov 15 '14 at 14:59
  • this is true, and if this is the way it has to be written because its for an assignment then that's just that was it has to be. I just think it makes more sense for what the asker is asking to have added values other than the defaults. I agree that he should be able to find zero if that's all that exists. – redeagle47 Nov 15 '14 at 15:01
  • @Tom redeagle47 I have fixed the issue and updated the code. Thanks everyone for there help. I appreciate your time and efforts. – Ashwiinn Karaangutkar Nov 16 '14 at 17:33