Check duplicates of numbers input by user - java

I'm doing a program where user input five numbers and in the end the numbers are printed out which is working fine. What I can't get to work is a boolean function to check for duplicates. It should check for duplicates as the user write them in, so e.g. if number one is 5 and the second numbers is also 5, you should get an error until you write in a different number. Meaning if the user input a duplicate it should NOT be saved in the array. This is obviously an assignment, so I'm just asking for a hint or two.
This program is written based on pseudo-code given to me, and therefore I have to use a boolean to check for duplicates with the public boolean duplicate( int number ) class.
I've tried getting my head around it and tried something by myself, but obviously I'm doing a stupid mistake. E.g.:
if(int i != myNumbers[i])
checkDuplicates = false
else
checkDuplicates = true;
return checkDuplicates;
DuplicatesTest class:
public class DuplicatesTest {
public final static int AMOUNT = 5;
public static void main(String[] args) {
Duplicates d = new Duplicates(AMOUNT);
d.inputNumber();
d.duplicate(AMOUNT);
d.printInputNumbers();
}
}
Duplicates class:
public class Duplicates {
private int amount;
private int[] myNumbers;
private boolean checkDuplicates;
public Duplicates(int a) {
amount = a;
myNumbers = new int[amount];
}
public void inputNumber() {
for(int i = 0; i < amount; i++ ) {
int input = Integer.parseInt(JOptionPane.showInputDialog("Input 5 numbers"));
myNumbers[i] = input;
}
}
public boolean duplicate( int number ) {
<BOOLEAN TO CHECK FOR DUPLICATES, RETURN FALSE OR TRUE>
}
public void printInputNumbers() {
JTextArea output = new JTextArea();
output.setText("Your numbers are:" + "\n");
for(int i = 0; i < myNumbers.length; i++) {
if (i % 5 == 0) {
output.append("\n");
}
output.append(myNumbers[i] + "\t");
}
JOptionPane.showMessageDialog(null, output, "Numbers", JOptionPane.PLAIN_MESSAGE);
}
}
Sorry if the code tag is messy, I had some trouble with white fields in between and such. I'm new here.

Don't store the numbers in an array. Use a Set<Integer> instead. And then do a Set#contains() operation. It's O(1) operation which is actually far better than iterating over the array to search for duplicates.
Ok, if it's a compulsion to use an array, then you should modify your current approach, to return true as soon as you find a duplicate, instead of iterating over the array again. In your current approach, since you are setting the boolean variable to false in the else block, your method will return false if the last element of the array is not the same as what you are checking. So, just modify your approach to:
// loop over the array
if (number == myNumbers[i])
return true;
// outside the loop, if you reach, return false
return false;
Note that your current if statement will not compile. You are declaring an int variable there, which you can't do.
if (int i == myNumbers[i]) // this is not a valid Java code.

int nums[] = new int[5];
int count = 0;
public boolean duplicate(int number)
{
boolean isDup = false;
for (int i = 0; i <= count; i++)
{
if (number == nums[i])
{
isDup = true;
break;
}
}
if (!isDup)
{
count++;
nums[count] = number;
}
return isDup;
}

Related

Number is not added at back of existing array

Learning about Arrays. I am not able to figure out why a new number is not added to the back of my existing array. I read in two textfiles in file_1.txt are the numbers '1 2 3' and in file_2.txt is the number '91'. Basically without the method of Void addBack() the program does what I expect, however by adding the method it seems not make a new Array. Even when I go over the elements[i] = elements[i-1] it won't print it as a whole. I am expecting to print for the first part
The numbers are: 1 2 3 and the second part The numbers are: 1 2 3 91.
public class ExampleLecture {
IntRow readIntRow(Scanner input) {
IntRow result = new IntRow();
while (input.hasNext()) {
result.add(input.nextInt());
}
return result;
}
IntRow setBack(Scanner input) {
IntRow result = new IntRow();
while(input.hasNext()) {
result.addBack(input.nextInt());
System.out.println("here");
}
return result;
}
void print(IntRow row) {
for (int i = 0; i < row.numberOfElements; i++) {
System.out.printf("%d ", row.elements[i]);
}
System.out.printf("\n");
}
void start() {
Scanner in = UIAuxiliaryMethods.askUserForInput().getScanner();
Scanner in2 =UIAuxiliaryMethods.askUserForInput().getScanner();
IntRow row = readIntRow(in);
IntRow row2 = setBack(in2);
System.out.printf("the numbers are: ");
print (row);
System.out.printf("the new numbers are: ");
print (row2);
}
public static void main(String[] args) {
new ExampleLecture().start();
}
}
package examplelecture;
class IntRow {
static final int MAX_NUMBER_OF_ELEMENTS = 250;
int[] elements;
int numberOfElements;
IntRow() {
elements = new int[MAX_NUMBER_OF_ELEMENTS];
numberOfElements = 0;
}
void add(int number) {
elements[numberOfElements] = number;
numberOfElements += 1;
}
void addBack(int number) {
for (int i = numberOfElements; i>0; i--) {
elements[i] = elements[i-1];
elements[i] = number;
}
}
}
You have 2 successive assignments which write to the same position:
elements[i] = elements[i-1];
elements[i] = number;
The value is alway overwritten with number, so the first statement has no effect.
Also in your addBack method your for cycle:
for (int i = numberOfElements; i>0; i--) {
What happens if numberOfElements is 0?
You call it addBack but it looks like a better name for the method is addFirst. Usually index 0 is considered the front, not the back.
First off, both the readIntRow() and setBack() methods create new IntRow objects row and row2. If you want the result to be appended to the first IntRow object created i.e. to row , you should call:
IntRow row = readIntRow(in);
IntRow row2 = row.setBack(in2);
and setBack() needs to be modified to:
IntRow setBack(Scanner input) {
while(input.hasNext()) {
this.add(input.nextInt());
System.out.println("here");
}
return this;
}
Note that in setBack(), if you are trying to append numbers to the end of the IntRow object, you should call add() instead of addBack() as above. If you are trying to add to the front, you should call addBack() [and it might be better to call it addFront() instead].
Also, in the implementation of addBack(), if you are trying to add to the front of the IntRow object, the element[i] = number operation should take place only once, after the loop. Otherwise all the values in indices <= numberOfElements would be overwritten with number.
void addBack(int number) {
for (int i = numberOfElements; i>0; i--) {
elements[i] = elements[i-1];
}
elements[0] = number;
}
Admittedly it is not entirely clear what you are trying to accomplish. But you may have several problems. The first is as follows:
IntRow setBack(Scanner input) {
IntRow result = new IntRow();
while (input.hasNext()) {
result.addBack(input.nextInt());
System.out.println("here");
}
return result;
}
IntRow has nothing in it since it is new. So all you are doing is iterating over the new file which has just 91 in it. Remember, result has no items. So it won't even iterate once in addBack.
So just do the following:
Change your addBack method to just add the numbers. Why use a loop to cascade down the elements since you are doing this within the same instance of IntRow? Just add it on to the end using the numberofElements as the next index.
void addBack(int number) {
elements[numberOfElements++] = number;
}
If you want to copy the contents of one IntRow object to another you would need another method in the IntRow class. Something like:
public void copy(IntRow r) {
for (int i = 0; i < r.numerOfElements; i++) {
elements[i] = r.elements[i];
}
numerOfElements = r.numberOfElements;
}
And keeping with good design it might be better to return numberOfElements in a method such as public int size();

How check if the array is full and only add to it if it's not? [duplicate]

I've got array. I've got an isFull method, which checks if the array is full, but I don't know how to use this to check if it's full, then if it's not full add to the array, otherwise disregard the add call.
The array should take 10 elements and then not accept any more. After 10 elements, it should 'be full' and disregard any addSpy calls.
How would you implement this?
public class ConcreteSubject extends AbstractSubject {
public int arySize;
private int i = 0;
private static AbstractSpy[] spies;
public ConcreteSubject(int a) {
arySize = a;
spies = new AbstractSpy[a];
}
#Override
public void addSpy(AbstractSpy spy) {
if (spies.length < 10) {
spies[i] = spy;
System.out.println("spy added at index " + i);
i++;
}
}
public void isFull() {
//1
boolean b = false;
for (int i = 0; i < spies.length; i++) {
if (spies[i] == null) {
b = true;
}
}
if (!b) {
System.out.println("Array is full");
} else {
System.out.println("Array not full");
}
}
public class TestSpies {
public static void main(String[] args) {
ConcreteSubject cs = new ConcreteSubject(10);
AbstractSpy spy = new ConcreteSpy();
AbstractSpy[] spies = new AbstractSpy[10];
cs.addSpy(spy);
cs.addSpy(spy);
cs.addSpy(spy);
cs.isFull();
}
}
spies.length < 10 isn't correct. It should be spies.length > 0 && i < spies.length to make sure that the following assignment spies[i] = spy; is always valid.
void isFull() should be boolean isFull(). Your implementation looks OK, just return b. full is a tricky word because technically an array is always "full". A better adjective would be populated, filled.
Since addSpy isn't filling null gaps but simply adds a spy to the end, isFull could be rewritten to return spies.length == i;.
The simplest way of doing it would be like that:
#Override
public void addSpy(AbstractSpy spy) {
if (!isFull())
{
spies[i] = spy;
System.out.println("spy added at index " + i);
i++;
}
}
To use that, you should change your isFull method to:
public boolean isFull() {
for (int i = 0; i < spies.length; i++) {
if (spies[i] == null) {
return false;
}
}
return true;
}
Keep a track of the number of filled cells of the array using a variable. And before inserting anything into it, check if the filled cells count strictly less than the size of the array (obviously you want to keep track of the array total size as well).

Compile Errors Java: Boolean Array Method

getting compile errors and just cannot get this code to work the way I need it to, it's beginning to drive me mad! Basically I am trying to get my boolean array in my method to iterate through the array, find out if False is listed more consecutively or if True is and return either true or false. In my program the array i have listed should return false. Any ideas? thanks in advance.
public class trueOrfalse
{
public static void main(String [] args)
{
boolean[] guess = {false,true,false,false,false,true,true};
boolean result = longerTF(guess);
}
public static boolean longerTF(boolean[] guess)
{
int variable = 0;
for(int x = 0; x < guess.length; x++)
{
if(guess[x] > true)
{
return true;
}
else
{
return false;
}
}
}
i don't know where to start
first of all this is wrong
if(guess[x] > true)
it should be
if(guess[x]==true)
since an if statement expects a boolean result and you have boolean values in your array this will have the same effect
if(guess[x])
you also missed a case. when the array is empty you would never run into the for loop, but your method still needs to return a boolean value. you could throw a runtime-exception or a default value like return false; at the end of your method
your for-loop does not make sense, since your method will return a result within the first iteration (if the array is not empty). your longerTF method could be also looking like this
public static boolean longerTF(boolean[] guess) {
if(guess.length>0)
return guess[0];
throw new IllegalArgumentException("The array must not be empty");
}
i'd suggest a general book like "programming: Learn the Fundamentals of Computer Programming Languages". you need to understand first the basics of programming before you try to implement anything.
The comparison​ operators >, etc., are neither legal nor meaningful for boolean operands. What did you intend guess[x] > true to accomplish?
Since guess is a boolean[] you are allowed to test
if (guess[x])
or
if (! guess[x])
and to
return guess[x];
EDIT
You want the loop to count consecutive values. This loop does not, but it shows how such a structure works for a simpler problem.
public boolean dominant(boolean[] guess) {
int tCount = 0;
for (int ix = 0; ix < guess.length; ++ix) {
if (guess[ix]) {
++tCount;
}
}
return tCount >= guess.length / 2;
}
Here is a "corrected" version:
public class trueOrfalse {
public static void main(String[] args) {
boolean[] guess = { false, true, false, false, false, true, true };
boolean result = longerTF(guess);
}
public static boolean longerTF(boolean[] guess) {
int variable = 0;
for (int x = 0; x < guess.length; x++) {
if (guess[x]) {
variable++;
}
}
return (variable > (guess.length-variable));
}
}
You forgot one closing braket }, a return statement and boolean compare cannot be with < or >.
As mentioned by everyone above. You can't use > to compare two booleans.
For your purpose to count the number of consecutive true/false, you need two different counters. You can run a loop and keep incrementing the counter as you encounter repeated item of true/false, if not you can just reset the counter to 1. I have put on a hasty solution below to give you an idea. I haven't tested it well it seems to work. Hope this helps.
public class trueOrfalse {
public static void main(String[] args) {
boolean[] guess = { false,true,false,false,false,true,true };
boolean result = longerTF(guess);
System.out.println("result: " +result);
}
public static boolean longerTF(boolean[] guess) {
int consecutiveFalseCount = 1;
int consecutiveTrueCount = 1;
for (int x = 0; x < guess.length; x++) {
if (guess[x] == true) {
if(x!=0 && x<guess.length){
if(guess[x-1] == true){
consecutiveTrueCount = consecutiveTrueCount + 1;
} else {
consecutiveTrueCount = 1;
}
}
} else {
if(x!=0 && x<guess.length-1){
if(guess[x-1] == false){
consecutiveFalseCount = consecutiveFalseCount + 1;
} else {
consecutiveFalseCount = 1;
}
}
}
}
System.out.println("Consecutive True count: " +consecutiveTrueCount);
System.out.println("Consecutive False count: " +consecutiveFalseCount);
if(consecutiveTrueCount>consecutiveFalseCount){
return true;
} else {
return false;
}
}
}

Generating random numbers to put in an array, if number is a duplicate draw then reroll. Boolean check is "sometimes" working

Edit: Uh well now that I pasted code into here and looked at it.. I have 2 .. "duplicate methods" .. Eclipse for some god awful reason hid the first "boolean checkforDupes()" from me. It seems to be fixed now, still poking it to make sure, should I just like.. delete the post or something?
I have a class assignment, which I've done essentially everything for, but I'm having issues with generating some random numbers to put into an array, and if they number has already been "drawn" then the number is re-generated so that there are no duplicates.
I've looked through several articles and have seen various ways to accomplish what I'm trying to do, but I'm really just trying to locate the flaw in my method/reasoning/etc.
(Pulling numbers from a fully populated array randomly, or shuffling an array and picking a handful, using a Set for unique numbers etc)
Essentially the program is just supposed to generate 5 lotto numbers, player(or computer player in this case) also selects 5 numbers. Each number set is to be unique, and then you compare the Array/List/whatever and pull out matches for assumed points or notoriety.
Somewhere between my generateNumbers() and checkForDupes() methods my logic has failed me and I've been stumped for a few hours. Sometimes the generator works and recognizes that it has rolled a duplicate and will reroll, but other times it will say reroll the first number but numbers 3 & 4 are duplicates which it ignores.
Any insight into this would be much appreciated.
package lottery;
import java.util.*;
public class Lottery {
final int chance = 5; //holds the number of lottery numbers to be picked
private int lotteryNumbers[] = new int[chance]; //array to hold the lottery numbers
private int playerNumbers[] = new int[chance]; //array to hold player's numbers
//Maybe rewrite with a Set instead of Array.
//Or generate random numbers and put them in array and "draw" lotto numbers from the array. Well now that I'm looking at my post, this comment is more of my I give up, next step stuff !
public Lottery(){
generateNumbers(lotteryNumbers);
System.out.println("Lottery numbers");
generateNumbers(playerNumbers);
System.out.println("Player numbers");
}
public String returnDate(){
Date date = new Date();
// display time and date using toString()
return date.toString();
}
public int[] getLotteryNumbers() {
return lotteryNumbers;
}
public int[] getPlayerNumbers() {
return playerNumbers;
}
private int[] generateNumbers(int[] numbers){
int check;//variable to pass for checking dupes
int count = 0;
Boolean DoIt=null;
Random rng = new Random(); //Used to pick lottery numbers
while (count<chance)
{
check = rng.nextInt(5)+1;//assigns random number to check
DoIt = checkforDupes(check, numbers);//passes check and the array to be checked for dupes
if (DoIt == true) //to execute if dupe checker says its ok
{
numbers[count] = check;
System.out.print(numbers[count]+" in ["+count+"], ");
count++;
}
else //supposed to restart the loop without incrementing for a new number if dupe
{
System.out.println(" Dupe rerolling "+check+" ["+count+"], ");
}
}
return numbers;
}
private boolean checkforDupes(int check, int[] array){
//pass in the generated variable and the array, check array if duplicates then return true or false to add the number
for(int i=0; i<chance; i++)
if (check == array[i])
return false;
else
return true;
return false;
}
private Boolean checkForDupes(int check, int[] array){
Boolean doIt = false;
for (int i=0; i<array.length; i++)
{
if (check == array[i])
{
doIt = false;
System.out.println("DON'T!"); //not printing anything to console
break;
}
else
doIt = true;
}
System.out.println("Am I even being called"); //also not printing to console...
return doIt;
}
public void checkMatches(int[] array1, int[] array2){
for (int index = 0; index<array1.length; index++)
if (array1[index] == array2[index])
System.out.print(array1[index]+" ");
System.out.println();
System.out.println("**** Possible Matches listed above ****");
}
}
if (check == array[i])
return false;
else
return true;
This is in a for loop, so it runs once for each array element (with i being the index of that element).
If check equals this array element, you return false. Otherwise, you return true.
And it never gets to go around the loop again, because returning exits the method immediately.
Here i have implemented with c# you can convert it into java if needed. I have used dictionary for faster searching.
Here is the code, You can modify it accordingly:
static Dictionary<int, int> randomNoArray = new Dictionary<int, int>();
static int[] arrayHavingManyNo= new int[] { 3, 4, 5,........... };
static void Main(string[] args)
{
while (true)
{
int randomNo = 0;
Console.WriteLine("press 1 to generate random and insert.");
Console.WriteLine("Press 2 to display No in Array");
if (Console.Read() == 1)
{
randomNo = GenerateRandom(1, 100);
if (!checkInArray(randomNo))
{
InsertInArray(Full[randomNo], Full[randomNo]);
}
}
else
{
foreach (KeyValuePair<int, int> pair in randomNoArray)
{
Console.WriteLine(pair.Value);
}
}
}
}
static int GenerateRandom(int start,int end)
{
return Convert.ToInt32(new Random().Next(start, end));
}
static bool checkInArray(int no)
{
return (randomNoArray.ContainsKey(Full[no]));
}
static void InsertInArray(int key , int value)
{
randomNoArray.Add(key,value);
}
}

Java, Return true if array contains duplicate values

I am trying to have a method (duplicates) return true if a given array called x (entered by user in another method), contains duplicate values. Otherwise it would return false. Rather then checking the entire array, which is initialized to 100, it will check only the amount of values entered, which is kept track of with a global counter: numElementsInX.
What is the best way to accomplish this?
public static boolean duplicates (int [] x)
I am prompting for user data like so:
public static void readData (int [] x, int i){
Scanner input = new Scanner(System.in);
System.out.println("Please enter integers, enter -999 to stop");
while (i <= 99) {
int temp = input.nextInt();
if(temp == -999){
break;
}
else {
x[i++]=temp;
}
// else
}//end while
printArray(x,i);
}//end readData
public static void printArray(int [] x, int numElementsInX){
int n = numElementsInX;
for (int i = 0; i < n; i++){
System.out.print(x[i] + " ");
}//end for
System.out.println();
}//end printArray
I am sure there is a better way to do this, but this is how I have been taught so far.
Here is a solution that:
Compiles and executes without throwing.
Uses numElementsInX as you requested.
Returns as soon as it finds a duplicate.
This approach tests whether each member of the array has been seen before. If it has, the method can return immediately. If it hasn't, then the member is added to the set seen before.
public static boolean duplicates (int [] x, int numElementsInX ) {
Set<Integer> set = new HashSet<Integer>();
for ( int i = 0; i < numElementsInX; ++i ) {
if ( set.contains( x[i])) {
return true;
}
else {
set.add(x[i]);
}
}
return false;
}
Here's a sample program containing the above code.
this should do it.
public boolean containsDuplicates(Integer[] x) {
return new HashSet<Integer>(Arrays.asList(x)).size() != x.length
}
You dont need numElementsInX as this is the same as x.length
edit after comment from Louis. Arrays.asList does not work with int arrays.
To convert int[] to Integer try this question How to convert int[] to Integer[] in Java?
or do soemthing like this (not tested but from memory)
Integer[] newArray = new Integer[a.length];
System.arraycopy(a, 0, newArray, 0, a.length);
This certainly isn't the most efficient way, but since you don't know about Sets yet, you can use two loops:
public static boolean duplicates (int [] x){
for (int i=0; i<numElementsInX; i++){
for (int j=i+1; j<numElementsInX; j++){
if (x[j]==x[i]) return true;
}
}
return false;
}
"set.add()" returns true if the element is not already present in the set and false otherwise. We could make use of that and get rid of "set.contains()" as in the above solution.
public static boolean duplicates (int[] x, int numElementsInX) {
Set<Integer> myset = new HashSet<>();
for (int i = 0; i < numElementsInX; i++) {
if (!myset.add(x[i])) {
return true;
}
}
return false;
}
For java, return true if the array contains a duplicate value,
boolean containsDuplicates(int[] a) {
HashSet<Integer> hs = new HashSet<>();
for(int i = 0; i<a.length; i++) {
if(!hs.add(a[i])){
return true;
}
}
return false;
}

Categories

Resources