Dead6hot
Dead6hot

Reputation: 1

Searching for an int in an array

I am trying to search for an int value in an array that has random numbers this is what I have so far:

String names[]={"Peter","John","Rudy"};
int number[]=new int[3];
for(int z=0;z<3;z++)
{
    number[z]=(int)(1+Math.random()*200);
}
int option=Integer.parseInt(JOptionPane.showInputDialog("choose and option:\n1.names\n2.sorting according to alphabet\n3.search number\n4.J"));
switch(option)
{
    case 1:
    {
        for(int l=0;l<names.length;l++)
        {
        jTextArea1.append(""+names[l]+"\t"+number[l]+"\n");
        }
    }
    break;
    case 2:
    {
        String temp="";
        for(int ii=0;ii<names.length;ii++)
        {
            for(int j=0;j<names.length;j++)
            {
                if(names[ii].compareToIgnoreCase(names[j])<0)
                {
                    temp=names[ii];
                    names[ii]=names[j];
                    names[j]=temp;
                }
            }
        }
        for(int x=0;x<names.length;x++)
        {
            jTextArea1.append(""+names[x]+"\n");
        }
    }
    break;
    case 3:
    {
        boolean found=false;
       int searchvalue=Integer.parseInt(JOptionPane.showInputDialog("number?"));
       for(int i=0;i<number.length;i++)
       {
           if(number[i]==searchvalue)

           {

               found=true;
           }
       }
       if(found==true)
       {
           jTextArea1.append("number is found"+"\n");
       }
       else
       {
           jTextArea1.append("number is not found"+"\n");
       }
    }
    break;
    case 4:
    {
        for(int q=0;q<names.length;q++)
        {
            if(names[q].startsWith("J"))
            {
                jTextArea1.append(names[q]);
            }
        }
    }

}

Even when I type the correct answer it gives me the "number is not found" message.I'm dumbfounded on what to do.Any help will be greatly appreciated.

Upvotes: 0

Views: 64

Answers (2)

RenegadeAndy
RenegadeAndy

Reputation: 5690

Well the answer above works for me! So the answer is your :

 int searchvalue=Integer.parseInt(JOptionPane.showInputDialog("number?"));

That line is probably causing the problem.

Try adding:

System.out.println("The number entered was:"+searchvalue);

and then compare it to the number you entered.

Upvotes: 0

user3299512
user3299512

Reputation:

Try something like this. I am not sure if this is exactly what you are looking for...

        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean found = false;
        int searchvalue;
        int[] lottoNumber = new int[6];  
        lottoNumber[0] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[1] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[2] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[3] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[4] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[5] = (5);
        System.out.print("number?");
        searchvalue = input.nextInt();
        for (int i = 0; i < lottoNumber.length; i++) {
            if (lottoNumber[i] == searchvalue) {

                found = true;
            }
        }
        if (found == true) {
            System.out.print("number is found" + "\n");
        } else {
            System.out.print("number is not found" + "\n");
        }
    }
}

This does not have GUI imports in this by the way.What exactly are you trying to do with this? Can you add the whole code?

Upvotes: 1

Related Questions