Help with arrays

Ok, here's what I have so far:

publicclass Duplication{

/**

* Method main

*

*

* @param args

*

*/

publicstaticvoid main(String[] args){

//int array[] = {32, 27, 64, 18, 18};

int[] array =newint[5];

array[0] = 14;

array[1] = 13;

array[2] = 95;

array[3] = 24;

array[4] = 24;

System.out.printf("%s%8s\n","Index","Value");

for(int counter = 0; counter < array.length; counter++){

if(array[counter] == array[counter + 1])

System.out.printf("%4d%8d\n", counter,"0");

else

System.out.printf("%4d%8d\n", counter, array[counter]);

}

}

}

My problem is this, my job was to input 5 numbers, any numbers between 10 and 100 and display it. However, if the nubers repeat, then return nothing or just the previous number. So the problem with this code is that i get:

--Configuration: testing - JDK version 1.6.0 <Default> - <Default>--

IndexValue

014

113

295

3Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String

at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3992)

at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2708)

at java.util.Formatter$FormatSpecifier.print(Formatter.java:2660)

at java.util.Formatter.format(Formatter.java:2432)

at java.io.PrintStream.format(PrintStream.java:920)

at java.io.PrintStream.printf(PrintStream.java:821)

at Duplication.main(Duplication.java:44)

Process completed.

Can anybody help me please?

[2449 byte] By [CloackedGCa] at [2007-11-26 16:46:22]
# 1
What happen to all the replies on this thread?Did people start to get abusive after I left? Or perhaps Chuck came in and stunk up the place with his usual crap?
floundera at 2007-7-8 23:13:47 > top of Java-index,Java Essentials,Java Programming...
# 2
I rarely use printf but your exception is because this lineSystem.out.printf("%4d%8d\n", counter, "0");I think you're formatting a number but passing the string "0"
manuel.leiriaa at 2007-7-8 23:13:47 > top of Java-index,Java Essentials,Java Programming...
# 3

Anyway a few questions arise:

what if there's no repeated numbers? it will blow up (ArrayIndexOutOfBoundsException) because

array[counter + 1]

Another question is, if:

array[0] = 24;

array[1] = 13;

array[2] = 95;

array[3] = 22;

array[4] = 24;

do you think it will work?

manuel.leiriaa at 2007-7-8 23:13:47 > top of Java-index,Java Essentials,Java Programming...
# 4

Since there will be no repeat with the first number, we can initial the loop with " int counter = 1; ", then, we can compare all the number with its previous number.

You also need to make change with this line:

-- From --

System.out.printf("%4d%8d\n", counter, "0");

-- To --

System.out.printf("%4d%8d\n", counter, 0);

It's because "0" is string value and it conflicts with your printf format.

Here should be the new loop:

for(int counter = 1; counter < array.length; counter++)

{

if( array[counter] == array[counter-1] )

{

System.out.printf("%4d%8d\n", counter, 0);

}

else

{

System.out.printf("%4d%8d\n", counter, array[counter]);

}

}

mysmileisfakea at 2007-7-8 23:13:47 > top of Java-index,Java Essentials,Java Programming...