0

Good Eve guys, this is a school activity, and i find difficulty in comparing my INPUTTED ARRAY if it is equal to 0(ZERO), greater than 0(ZERO), and lesser than 0(ZERO).

heres my code...

String display="";

    String size = JOptionPane.showInputDialog("Enter Your Prefered Size Of Your Array");
    int newsize = Integer.parseInt(size);

    JOptionPane.showMessageDialog(null,"You Entered "+newsize+".");

    String array[] = new String[newsize];

    for (int a=0; a<array.length;a++)
    {
        array[a]=JOptionPane.showInputDialog("Enter Value For Array["+a+"].");

    }


    //i'm having trouble here.........

    for (int a=0;a<array.length;a++)
    {
        if (array[a].equals(array[0]))
        {

        }
        else if (array[a] < (array[0]))
        {

        }



        display=display+array[a]+"\n";
    }
    JOptionPane.showMessageDialog(null,"Arrays\n"+display);

please someone help me......

EDITTED:

and at the lastpart i want it to display that the ARRAY is "ZERO", "POSITIVE", "NEGATIVE" (i really dont know how to do it).

2 Answers 2

4

You can't use conditional operator < in String comparison. Use compareTo method.

 array[a].compareTo(array[0])> 0 // greater    
 array[a].compareTo(array[0])< 0 // less
 array[a].compareTo(array[0])== 0 // equals

Update

Update your code with following segments.

for (int a=0;a<array.length;a++)
{
    if (array[a].compareTo(array[0])== 0)
    {
      //this is equals block
    }
    else if (array[a].compareTo(array[0])< 0)
    {
     //this is less than block.
    }
    display=display+array[a]+"\n";
 }
Sign up to request clarification or add additional context in comments.

5 Comments

how do i do that sir?
i get it, but sir, how do i display that my ARRAY is greater, equal or lesser. like ARRAY[0] contains 0 then i will display: 0 ZERO. and ARRAY[1] contains 1 then display: 1 POSITIVE
To check contains 0, than you need to use String.contains("0") method. Above is sample example of String comparison.
thank you for your help. i get it, but sir, how do i display that my ARRAY is greater, equal or lesser. like ARRAY[0] contains 0 then i will display: 0 ZERO. and ARRAY[1] contains 1 then display: 1 POSITIVE ARRAY[2] contains -1 then display -1 NEGATIVE
@user2981000, That is different scenario. Pleas, post another question with what you exaclty want.
0

If you need to compare to the number zero, change array[0] to just 0.

Comments

Your Answer

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.