Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By vx117

Thread: Items and values?

  1. #1
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default 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. #5
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Items and values?

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

Similar Threads

  1. Replies: 8
    Last Post: 12-10-2011, 05:54 PM
  2. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  3. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-11-2009, 12:35 AM
  4. Replies: 0
    Last Post: 05-22-2008, 11:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 02:04 PM

Posting Permissions

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