0

Hi guys sorry I'm a newbie to Java, this is one of the exercise in my class. I supposed to ask user input 5 numbers, then compare them if they are the same number that entered before. These are my code so far, but I can't get it work. Thanks.

import java.util.Scanner;

public class Source {
    private static int num = 0;
    private static int[] enterednum = new int[5];

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        for(int count = 0; count < enterednum.length; count++) {
            System.out.println("Enter a number.");
            num = input.nextInt();

            compare(enterednum);
        }

        System.out.println("These are the number you have entered: ");
        System.out.println(enterednum);
    }

    public static void compare(int[] enterednum) {
        for(int count = 0; count < 6; count++)
            if(num == enterednum[count])
                System.out.println("The number has been entered before.");
    }
}
4
  • What specifically is the problem? I mean, what do you mean when you said I can't get it work Commented Mar 4, 2016 at 3:33
  • Why are you running count for 6 times?Use the same .length property? Commented Mar 4, 2016 at 3:33
  • It has errors when try to run it. @kaonashi Commented Mar 4, 2016 at 3:55
  • Yeah didn't realize that... Thanks ! @SatejS Commented Mar 4, 2016 at 3:56

2 Answers 2

1

You may want something like this:

import java.util.Scanner;
public class Source 
{
    private static int enterednum[]=new int[5];
    public static void main(String args[])
    {
        int num=0; // make this local variable since this need not be class property
        Scanner input = new Scanner(System.in);


        for(int count=0;count<enterednum.length;count++)
        {
            System.out.println("Enter a number.");
            num = input.nextInt();
            compare(num, count);
            enterednum[count] = num; // store the input


        }

        System.out.println("These are the number you have entered: ");
        // print numbers in array instead the array
        for(int count=0;count<enterednum.length;count++)
        {
            System.out.println(enterednum[count]);
        }
    }
    // change the method signature to let it get the number of input
    public static void compare(int num, int inputcount)
    {
        for(int count=0;count<inputcount;count++)
        {
            if(num==enterednum[count])
                System.out.println("The number has been entered before.");
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! I will try it !
0

You can do this way if you need.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Source {    
    public static void main(String[] args) throws IOException {
        // I used buffered reader because I am familiar with it :) 
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        // Create a Set to store numbers
        Set<Integer> numbers = new HashSet<>();
        for (int i = 0; i < 5; i++) {
            System.out.print("Enter a number: ");
            String line = in.readLine();
            int intValue = Integer.parseInt(line);

            // You can check you number is in the set or not
            if (numbers.contains(intValue)) {
                System.out.println("You have entered " + intValue + " before");
            } else {
                numbers.add(intValue);
            }
        }
    }
}

1 Comment

Wow this is something advanced ... Thanks !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.