Results 1 to 2 of 2
- 10-24-2010, 08:27 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 1
- Rep Power
- 0
Loop through array and check values...
I have this array. I need to go through the array and compare two int variables. If both variables are the same then print all the variables that have the same value.
If the variable is not the same/ does not exist then ask the user to try again.
Java Code:import java.util.*; public class LoopMe { private static Scanner scan = new Scanner(System.in); public static void main (String args[]) { int num1 [] = {1,2,3,4,5,6,6,7}; int testVal; System.out.println("Enter a value"); testVal = Integer.parseInt(scan.nextLine()); for(int i =0; i<num1.length;i++) { if(testVal == num1[i]) { System.out.println("Hi"); System.exit(0); } } } }
- 10-24-2010, 10:17 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
If your code finds the value in the array it prints "Hi" and quits; that's not what your assignment asks for; you should register the fact that your code found an identical value and continue the search (a boolean variable perhaps?). If, after the search has finished and nothing was found your code should ask for another value. There should be a while loop around that for loop that does the search.
kind regards,
Jos
Similar Threads
-
Adding up all values generated from a for-loop
By BariMutation in forum New To JavaReplies: 4Last Post: 10-10-2010, 05:31 PM -
question on how to check values before storing
By SMHouston in forum New To JavaReplies: 19Last Post: 09-21-2009, 10:54 PM -
How to check whether two elements are available in an array?
By venkatteshb in forum New To JavaReplies: 8Last Post: 08-27-2008, 10:45 PM -
How an array can check that profile whether exists or not???
By hien_NU in forum New To JavaReplies: 1Last Post: 01-10-2008, 01:18 AM -
How to add interceptor to persistent ejb entity to add check before returning values
By umen in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 11-30-2007, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks