0

Any possible way to keep entering numbers, and when the same number is entered 2 or more times an error message arises or something like that? I need this to be answered in Java only. I'm new and I don't know where to go from here. I need help searching values in the array and then printing out all the numbers that have been entered.

public class Test 
{
 public static void main(String args[])
 {

  Scanner input = new Scanner(System.in);
  System.out.println("How big is the group?: ");
  int[] group = new int[input.nextInt()];



for (int i = 0; i < group.length; i++)
{
    System.out.println("Please enter number: ");
    group[i] = input.nextInt();
}           
  • 1
    This is not much of an improvement over your previous question. However, if you don't mind the array been sorted, you could use Arrays.binarySearch method. If it returns a positive number, then the number has beeping entered before – MadProgrammer Jun 10 '13 at 03:20
  • Duplicate: [Tons](http://stackoverflow.com/questions/12632167/removing-duplicates-from-array-in-java) [of](http://bit.ly/11BN7xE) [other](http://stackoverflow.com/questions/10457751/removing-duplicates-from-an-array-in-java) [beginners'](http://stackoverflow.com/questions/12771705/removing-duplicates-from-array-in-java-cant-use-hashset) [questions](http://stackoverflow.com/questions/15756376/remove-duplicate-elements-in-an-array-java) [on](http://stackoverflow.com/questions/357421/what-is-the-best-way-to-remove-duplicates-in-an-array-in-java) [SO](http://bit.ly/15OPHjZ) – Qix - MONICA WAS MISTREATED Jun 10 '13 at 03:36
  • Not sure how this hasn't been closed yet. :P – Qix - MONICA WAS MISTREATED Jun 11 '13 at 21:11

2 Answers2

1

I think this is what you're looking for. Inside of the for loop, there's a while loop spinning to keep collecting new ints until you enter one that's not already in the list.

for (int i = 0; i < group.length; i++)
{
    System.out.println("Please enter number: ");
    int next = input.nextInt();

    while(Arrays.asList(group).contains(next)) {  // Keep asking for new input while the input is already in list
        System.out.println("That number is already in the group ... try again.");
        next = input.nextInt();
    }

    group[i] = next;
}  
Eric Wich
  • 1,504
  • 10
  • 8
  • His loss if he doesn't learn the stuff and fails in the professional world :) What goes around comes around. – Qix - MONICA WAS MISTREATED Jun 10 '13 at 03:31
  • @Qix - that is not entirely true. It is also the loss of his parents who paid for his education, his school's reputation, and his long suffering co-workers. But either way, isn't it better to discourage his bad practice while there is still a chance he can recover? – Stephen C Jun 10 '13 at 03:35
  • Yes and no. You're also making a lot of assumptions. Regardless, this is a duplicate (see my comment on OP), and it should be flagged as such. One thing is certain; the OP doesn't know how to Google for anything. – Qix - MONICA WAS MISTREATED Jun 10 '13 at 03:38
  • Yes I am making some assumptions. But there are some strong hints in the way he asked the Questions. And I'm more interested in the general principle here ... that >>WE<< shouldn't help people dig their own graves ... rather the particularities of an individual anonymous "grave digger". – Stephen C Jun 10 '13 at 03:43
  • Then flag the question. Simple as that. – Qix - MONICA WAS MISTREATED Jun 10 '13 at 04:07
  • The real problem is not with the Question. It is with the way people are answering it. – Stephen C Jun 10 '13 at 04:41
  • He had an excerpt of code and a question. I answered said question. Don't have a long winded argument via comments on an answer that I provided. Was the OP wrong by just semi-cleverly rewording a homework assignment for help? Maybe. Are you wrong for turning an answer into a debate and attacking me for trying to help? Definitely. – Eric Wich Jun 10 '13 at 14:48
  • @EricWich - I merely pointed out that it is in nobody's interest to write code for him. I fail to see how that is a personal attack. And if I don't point it out in comments on your Answer, there is no other way to bring it to your attention. If you have a problem with this, feel free to take it up with a moderator. – Stephen C Jun 11 '13 at 05:16
1

Since this is clearly a "learning exercise", it is only appropriate to give you hints:

  1. You can search an array by stepping through the array indexes and testing the elements at each index.

  2. A method and a class both need a closing } ...


I need this to be answered in Java only.

That is incorrect. What you REALLY need is some hints. If we give you Java code, you miss out on the important learning experience of writing it yourself. And THAT is the WHOLE POINT of the homework. Go ask your teacher if you don't believe me.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216