Results 1 to 5 of 5
Thread: Items and values?
- 02-11-2013, 12:48 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 19
- 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.
-
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.
- 02-11-2013, 01:20 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 19
- 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 01:23 PM.
- 02-11-2013, 01:30 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
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.
- 02-11-2013, 01:59 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Unable to add Items to a JComboBox when those items are read from a file
By jiffi in forum AWT / SwingReplies: 8Last Post: 12-10-2011, 04:54 PM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks