program enter element
and if num is already exist give message enter other number no duplicate
my code give me alwayes else condition

/**
 * @(#)Enter.java
 *
 *
 * @author
 * @version 1.00 2011/11/18
 */

import java.util.Scanner;
public class Enter {
	public static void main(String []args){

Scanner input=new Scanner(System.in);
int size ;
System.out.println("plz size");
size=input.nextInt();
int [] a = new int [size];
System.out.println("plz element");
for(int i=0;i<a.length;i++)
{	for(int j=1;j<a.length-1;j++){
	if (a[j]!=a[j-1])
	a[i]=input.nextInt();
else
		System.out.print(" NO Enter other num");
}}
for(int m=0;m<a.length;m++)
System.out.println(a[m]);
}

}

Recommended Answers

Im sorry but the looping logics uve used doesnt make much sense at all.

I suggest you come up with a proper algorithm for doing this problem and then write a pseudocode with that algorithm and then proceed to coding.

When U come up with a proper algorithm …

Jump to Post

you need to put your test after inputting the number.

Jump to Post

All 6 Replies

Im sorry but the looping logics uve used doesnt make much sense at all.

I suggest you come up with a proper algorithm for doing this problem and then write a pseudocode with that algorithm and then proceed to coding.

When U come up with a proper algorithm post it here.

Good luck.

you need to put your test after inputting the number.

I don't know how much help this might be, but maybe a Set instead of an array?

I don't know how much help this might be, but maybe a Set instead of an array?

Set is a great solution. But he needs to know how to do this. The algorithm behind this. So let him come up with an algorithm for doing this.

Thats hard to read code, you can use the enhanced for loop

void foo(int[]  f){
for(int x : f){
System.out.println(x);
}
}

to help you maintain the progy.

Thats hard to read code, you can use the enhanced for loop

void foo(int[]  f){
for(int x : f){
System.out.println(x);
}
}

to help you maintain the progy.

What exactly is this for?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.