0

I need to make a program to compare if an element in an array has the same value, if there is an element in the array that are the same, it prints "Yes", otherwise "No".

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DistinctNumbers {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("numbers.in"));
        int sizeN = scanner.nextInt();
        int[] arrayA = new int[sizeN];
        for (int i = 0; i < arrayA.length; i++) {
            for (int j = i + 1; j < arrayA.length; j++) {
                arrayA[j] = scanner.nextInt();
                if (arrayA[0] == arrayA[i++]) {
                    System.out.println("Yes");
                } else {
                    System.out.println("No");
                }
            }
        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • You should say where your problem is. With just a glance it seems like you're only checking if the element arrayA[0] is already in, but not if arrayA[1] == arrayA[2] etc. EDIT: and you're are increasing i inside the j for loop this code looks really flawed – Nordiii Feb 13 '20 at 08:49
  • Does this answer your question? [How do I determine whether an array contains a particular value in Java?](https://stackoverflow.com/questions/1128723/how-do-i-determine-whether-an-array-contains-a-particular-value-in-java) – Amongalen Feb 13 '20 at 08:51
  • @Nordiii do I need to add 'else if'? When I try running the code for output it is printing "Yes" "No" "No" "No" – helloplshelp Feb 13 '20 at 08:52

2 Answers2

0

[Edited] (see below)

Do you mean something like this?

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("numbers.in"));
    int sizeN = scanner.nextInt();
    int[] arrayA = new int[sizeN];
    for (int i = 0; i < sizeN; i++) {
        arrayA[i] = scanner.nextInt();
        for (int j = 0; j < i; j++) {
            if (arrayA[i] == arrayA[j]) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}

Can you please give us an expected input with an expected output? You want a simple Yes or No as output or a series of Yes or Nos as an output?

[Edit]

If you are trying to check whether there are 2 or more numbers in the array with the same value this might work for you (however it's not optimal):

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("numbers.in"));
    int sizeN = scanner.nextInt();
    int[] arrayA = new int[sizeN];
    boolean found = false;
    for (int i = 0; !found && i < sizeN; i++) {
        arrayA[i] = scanner.nextInt();
        for (int j = 0; !found && j < i; j++) {
            if (arrayA[i] == arrayA[j]) {
                found = true;
            }
        }
    }
    if (found) {
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }
}
dfritsi
  • 1,224
  • 3
  • 14
  • 24
  • for example when the array elements are say {1, 2, 1, 3} it will simply print "Yes". and if there are no elements of some value, like {5, 4, 1, 2, 3}. "No" will be printed instead. – helloplshelp Feb 13 '20 at 08:59
0

I think you wants to do :

        if (arrayA[i] == arrayA[j]) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }

if any elements of array same, it will be 'Yes'