Troubleshooting Array Lists in Java

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Java program that processes an array of integers. Participants address issues related to counting positive and negative values, modifying array elements based on their indices, searching for values using a sequential search, and sorting the array using bubble sort. The focus is on debugging and refining the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • The original poster describes issues with the search functionality, noting that it always reports the first slot as the location for found values.
  • One participant suggests that the variables NUM and found should be re-initialized at the start of the search loop to avoid incorrect behavior.
  • Another participant advises breaking out of the search loop once the value is found to improve efficiency.
  • There are comments on coding conventions, specifically regarding variable naming and the use of curly braces for clarity.
  • The original poster later expresses that they resolved the main issue but encountered a new problem with the NUM counter not incrementing properly during the search.
  • Another participant confirms that initializing found and NUM at the start of the search loop is crucial and discusses the use of pre-incrementing for clarity in the code.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper initialization of variables and the importance of following coding conventions. However, the discussion reflects ongoing troubleshooting and refinement, indicating that some issues remain unresolved.

Contextual Notes

There are limitations regarding the handling of variable initialization and control flow within loops, which may affect the program's functionality. Specific mathematical or logical steps in the code are not fully resolved.

Hypnos_16
Messages
148
Reaction score
1

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 into 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:
Physics news on Phys.org
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:
Hypnos_16 said:
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K