Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with Arrays

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with Arrays

    please note that I can only use loops
    I need to code a program which prompts the user to input integers. When the user enters three unique integers, the program should stop and display the unique integers.
    Example

    Enter a integer:
    1
    Enter a integer:
    1
    Enter a integer:
    2
    Enter a integer:
    1
    Enter a integer:
    3
    First 3 unique numbers entered are: 1 2 3

    I need help in figuring out how I can search the recently input value to the previous values and discard them if they are the same (duplicates) using for loops

    import java.util.Scanner;
    public class uniqueinteger {
     
     
    	public static void main(String[] args) {
     
    		int userInput, array[];
    		int i = 0;
    		array = new int[3];
     
    		Scanner myScan = new Scanner (System.in);
     
    		while (i <= 3){
    			System.out.println("Enter a non negative integer: ");
    			userInput = myScan.nextInt();
    			array[i] = userInput;
    			i++;
     
     
     
    		if (userInput < 0)
    			i--;
     
    		}
     
    		System.out.println("First 3 unique numbers entered are: " + array[i] + "  ");
     
    	}
     
    }
    Last edited by xdx; October 9th, 2012 at 09:45 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Arrays

    for each previously saved number...
       if current number == any previously saved number,
          reject current number
          otherwise save current number as unique

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays

    I did figure that out but I have tried many ways to implement it but nothing seems to work so far.
     while (i <= 3){ 
    	System.out.println("Enter a non-negative integer:");
    	userInput = myScan.nextInt();
    	array[i] = userInput;
    	i++;		
     
    	for (int j = 1; j<=array.length; j++){
    		if (array[i] == array[j])
    			i--;
    		else
    			i++;
     
     
    	}
    	if (userInput < 0)
    		i--;
     
    		if (i == 3)
    			break;
    			}

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Arrays

    1) I'm not so sure adding a value to the array before it has been validated is a good idea. That would be like having the line to the club,......inside the club... It can get confusing to the reader. Perhaps store the current number in a variable for validating, and if it passes allow it a home in the array, and if not just overwrite it on the next iteration of the loop. What happens when you reach the last number of the array? Wouldn't the test always be true since it is testing the number against itself?

    2)if (array[i] == array[j]) Assuming you are testing what you think you are testing.... Does it make sense to i-- or i++ for every element of the array as it is being checked?
    The posted algorithm suggests to check the entire array, and then decide to i-- and discard the current number, or i++ and keep the current number as unique.

    if (userInput < 0)
    		i--;
    Java allows you to leave the curly braces out in this case. Put them in anyway. The program is no different with or without them, the difference is to the reader of the code. Box up these 1-liner if statements just like the rest of the code and keep everything in a neat and orderly style.
    if (userInput < 0)
    		i--;
     
    		if (i == 3)
    			break;
    			}
    This just looks like crazyness with the indentation so far off and the braces missing.

    Start back at the algorithm. Take the sample given in post #2 and break the steps down into smaller steps each. When you can no longer break the steps down it is time to start translating that into code. Make sure you have a full understanding of each step individually from start to finish and don't focus so much on the finished product or the big picture as you write code for each broken down step.

Similar Threads

  1. Arrays
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 6
    Last Post: December 9th, 2011, 07:24 PM
  2. Can Anyone Help With Arrays?
    By metaleddie13 in forum Collections and Generics
    Replies: 9
    Last Post: November 16th, 2011, 12:12 AM
  3. Arrays
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 7th, 2011, 11:18 PM
  4. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM