0

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]);
}

}
5
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by stevanity
0

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.

0

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.

0

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.

0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.