# Thread: Items and values?

1. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Items and values?

Write LinearSearch.java containing a main method that

1. First reads a positive integer N and then reads N more integers (the items).
2. Then reads more integers (the values) until a zero value is read. For each nonzero value read, print the value followed by either yes or no depending on whether the value is or is not one of the items.
3. Use the primitive algorithm for searching the items, i.e., compare with the first item, then with the second, etc.
4. The items are not in numerical order and you are not being asked to sort them.

Does anyone understand what 1 and 2 mean? What are items and values?
Thanks.

2. ## Re: Items and values?

Let's break part (1) into two parts, (1a) and (1b):

For (1a): the program will prompt the user for a number and than accept it from the user.
For (1b): The program will then get more numbers from the user and store them. How many? The same number as the number the user entered in (1a) above. So if the user enters 12 for (1a)'s input above, the program will accept 12 more numbers and store them.

For (2): the program will accept more numbers until the user enters 0. After each number entered, the program will check to see if the number was entered in part (1b) above and respond accordingly.

3. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Re: Items and values?

Why is my second "for" loop not initializing? For some reason, it always gets skipped.

Java Code:
```import java.util.Scanner;

public class LinearSearch {

public static void main(String[] args) {

Scanner values = new Scanner (System.in);
Scanner items = new Scanner (System.in);

System.out.println("Please enter an integer: ");
int x = values.nextInt();

System.out.println("Please enter the number of items: ");
int y = items.nextInt();

int [] table = new int[y];
table[0] = x;
//System.out.println(table[0]);
//System.out.println(table.length);

for (int i = 1; i < table.length; i++){

System.out.println("Please enter an integer: ");
int z = values.nextInt();
table [i] = z;
System.out.println(table[i]);

for(int q = i-1; q < i-1; q--){

if (table[q] == table[i]){

System.out.println("yes");
break;
}

else if (table[q] != table[i] && q == table.length){

System.out.println("no");
}

}

if (z == 0){
break;
}
}

}

}```
Last edited by vx117; 02-11-2013 at 02:23 PM.

4. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: Items and values?

Hi vx117,

Check the conditions in your for loop. First you assign q to be i minus 1 then loop whist q is less than i minus 1. The latter will never be acheived because of the former.

Regards.

5. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Re: Items and values?

Ah, that's right, silly me. I got it, thanks.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•