Question about Arrays

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 8
Reputation: PRob99 is an unknown quantity at this point 
Solved Threads: 0
PRob99 PRob99 is offline Offline
Newbie Poster

Question about Arrays

 
0
  #1
Nov 18th, 2008
I'm working on a problem where I need to use a one-dimensional array to write an application that inputs five numbers, each between 10 and 100, inclusive. As each number is read, it displays it only if it is not a duplicate of a number already
read. The program should keep asking for input until 5 unique numbers are input. Use the smallest possible array to solve this program. Display the complete set of unique values input after the user enters each new value.

I'm stuck. Everything works correctly except for the nested if loop. It always accepts this for true. Any ideas on how I might rearrange my code to make this work properly? Thanks in advance!
Also, I apologize for the sloppy code. I don't know how to make it translate to look pretty on here.

  1. import java.util.Scanner;
  2. public class UniqueTest {
  3.  
  4. public static void main(String[] args) {
  5. //declare an array with 5 elements
  6. int num[] = new int[5];
  7. int index = 0;
  8. Scanner input = new Scanner(System.in);
  9. //declare the input object (she didn't do this for us)
  10.  
  11.  
  12. while(index < num.length){
  13. System.out.println("Enter an integer between 10 and 100: ");
  14. //accept a keyboard input and assign it to a variable
  15. int number = input.nextInt();
  16.  
  17. if (number>=10&&number<=100)
  18. {
  19. ++index;
  20. num[index] += number;
  21.  
  22. System.out.printf("%d\n", num[index]);
  23.  
  24. if (number == num[index])
  25. {
  26. System.out.println("Value has already been entered.");
  27. }
  28. }
  29. else
  30. {
  31. System.out.println("Error: You did not enter a number between 10 and 100.");
  32. }
  33. }
  34. }
  35. }
Last edited by ~s.o.s~; Nov 18th, 2008 at 11:04 pm. Reason: Fixed code tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Question about Arrays

 
0
  #2
Nov 18th, 2008
you are doing a single check with the if loop

do a for

  1. bool contains = false;
  2. for(int i=0; i<index; i++)
  3. {
  4. if (number == num[i])
  5. {
  6. System.out.println("Value has already been entered.");
  7. contains = true;
  8. }
  9. }
  10.  
  11. if(!contains)
  12. {
  13. num[index] = number;
  14. ++index;
  15. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: PRob99 is an unknown quantity at this point 
Solved Threads: 0
PRob99 PRob99 is offline Offline
Newbie Poster

Re: Question about Arrays

 
0
  #3
Nov 18th, 2008
Thank you very much for your help. That worked out perfectly. Now I just noticed one final problem with my code. Instead of saving each value in the array and displaying it like 88, then 88,53 then 88,53,44 and so on, it just repeats whatever number I have most recently entered. How can I store each number permanently into the array and display it accordingly?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Question about Arrays

 
0
  #4
Nov 18th, 2008
use the same for loop concept, put it at the bottom of the while loop

  1. for(int i=0; i<index; i++)
  2. {
  3. System.out.println(num[i]);
  4. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: PRob99 is an unknown quantity at this point 
Solved Threads: 0
PRob99 PRob99 is offline Offline
Newbie Poster

Re: Question about Arrays

 
0
  #5
Nov 18th, 2008
Originally Posted by dickersonka View Post
use the same for loop concept, put it at the bottom of the while loop

  1. for(int i=0; i<index; i++)
  2. {
  3. System.out.println(num[i]);
  4. }
You've been very helpful and I thank you greatly! I apologize for my ignorance, but you should've seen me about 2 months ago! Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Question about Arrays

 
0
  #6
Nov 18th, 2008
hey man, it takes time to grasp onto the concept of programming, (althought we all can't admit it :-) ), i commend you on trying and thats what we like to see here, not just give me an answer

keep up the good work and keep showing your effort!!!
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC