# Troubleshooting Array Lists in Java

• Comp Sci

## Homework Statement

Input a list of n integer values into a 1-D array and do the following:
• determine and output the number of positive values and the number of negative values
• update the array values as follow
• if the value is located at an even-numbered subscript, replace the value by its square • if the value is located at an odd-numbered subscript, replace the value by its cube
• input a list of search values and, using the Sequential Search, search for each value and report its location in the list (if found); otherwise output a message
• using the Bubble Sort algorithm, sort the list into descending order.

## Homework Equations

okay so i have most of this done, i'm just looking or some troubleshooting because something has gone wrong. The code will be below.

## The Attempt at a Solution

Code:
import java.util.Scanner;
public class Assignment6Question1{
public static void main(String[] args){
int index = 0;
int PosCount = 0;
int NegCount = 0;
int SearchKey = 0;
boolean found = false;
Scanner in = new Scanner(System.in);
System.out.print("Enter number of values you would like to input: ");
int SIZE = in.nextInt();
int [] Values = new int [SIZE];
for (int NUM = 0; NUM < SIZE; NUM++)
{
System.out.print("Enter value of number: ");
index = in.nextInt();
if (index < 0){
NegCount++;
} else {
PosCount++;
}
if ((NUM % 2 != 0)||(NUM == 0)){
index = (index * index * index);
} else {
index = (index * index);
}
Values[NUM] = index;
}
int NUM = 0;
System.out.print("Would you like to search for a value? (Y / N): ");
String ANS = in.next().toUpperCase();
while (ANS.equals("Y")){
System.out.print("Enter value you would like to search for: ");
SearchKey = in.nextInt();
while ((!found)&&(NUM < SIZE))
if (SearchKey == Values[NUM])
found = true;
else
NUM = NUM + 1;
if (found == true){
System.out.print("Search for value " + SearchKey + " was Successful! It was found at location " + (NUM + 1) + " Would you like to search for another value? (Y / N): ");
ANS = in.next().toUpperCase();
} else {
System.out.print("Search for value " + SearchKey + " was UnSuccessful. Would you like to search for another value? (Y / N): ");
ANS = in.next().toUpperCase();
}
}
System.out.println("Number of Negative Values = " + NegCount);
System.out.println("Number of Positive Values = " + PosCount);
}
}
whenever i attempt to run it and search for a number, the search is either always successful, or always a failure. Whenever it's success it says the answer is in the 1st slot in the array regardless of whatever number i put in to search.
Any suggestions what's wrong???

Code:
Enter number of values you would like to input: 2
Enter value of number: 1
Enter value of number: 2
Would you like to search for a value? (Y / N): y
Enter value you would like to search for: 1
Search for value 1 was Successful! It was found at location 1 Would you like to search for another value? (Y / N): y
Enter value you would like to search for: 2
Search for value 2 was Successful! It was found at location 1 Would you like to search for another value? (Y / N):

a read of the typescript in case what i was saying got tangly.

Last edited:

A comment not related to the bug, but still important: All variables should start with a lower case letter. Very annoying when they're not, trust me. It's a universal convention in Java and many other languages.

You have a couple of problems. The big one is that you fail to initialize NUM to 0 and found to false at the start of the search loop. That's causing the lion's share of the problems. Remember, they need to be initialized every time the loop starts.

I also suggest that, after you set found to true when you find the number, break out of the loop. You've found it, no need to keep searching.

Also watch the curly braces. I suggest using them even when not strictly needed since not using them often causes needless bugs. Seems small, but it isn't. I see it happen *all the time*. Also, it's clearer and more obvious, which is one of the big important qualities of good code.

Okay so i've gotten over the main problem of my program, but the problem i'm having now is that it seems to not be incrementing the NUM counter properly. Like it will check the value in the first element of the array and if the number isn't there it will just automatically then say it's not in the array at all. Or that's how I'm taking it now. Any advice for how i can modify that?

Scratch that. I figured it out. Thanks for your help though.

Last edited:
Okay so i've gotten over the main problem of my program, but the problem i'm having now is that it seems to not be incrementing the NUM counter properly. Like it will check the value in the first element of the array and if the number isn't there it will just automatically then say it's not in the array at all. Or that's how I'm taking it now. Any advice for how i can modify that?

Ok, so you've added this right after the 'while(ANS.equals("Y")){':
Code:
found = false;
NUM = 0;
Is that what you have? Or did you put it in the following while loop after 'while ((!found)&&(NUM<SIZE))'?

And does your matching if/else block look like this?
Code:
                                if (SearchKey == Values[NUM]) {
found = true;
break;
} else {
++NUM;
}
I should mention that I turned 'NUM = NUM + 1;' into '++NUM'. It does the same thing, but it's shorter and (I find) clearer. It's not a bug, but incrementing by one is what ++ is for. You may want to make sure you know the difference between pre and post incrementing, however.

EDIT: Ah, glad you got it working. And you're welcome.