...

View Full Version : Reading in integers into Array



anonymous9
02-20-2012, 05:48 PM
I'm trying to read integers into my array but I'm getting an error. What am I doing wrong?

import java.util.Scanner;
public class array {


public static void main(String[] args) {

int[] grades;
int size = 5;
grades = new int[size];

System.out.println("Please enter 5 unique numbers");
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the first unique numbers");
int[0] grades = keyboard.nextInt();
System.out.println("Please enter the second numbers");
int[1] grades
int second = keyboard.nextInt();
System.out.println("Please enter the third numbers");
int third = keyboard.nextInt();
System.out.println("Please enter the fourth numbers");
int fourth = keyboard.nextInt();
System.out.println("Please enter the fifth numbers");
int fifth = keyboard.nextInt();


}

}

alykins
02-20-2012, 11:37 PM
public class demo {
public static void main(String[] args) throws IOException {

int[] grade = new int[5];
Scanner keyboard = new Scanner(System.in);
Boolean validate = false;
System.out.println("Please Enter 5 Unique numbers...");
for(int i=0; i<grade.length;i++)
{
validate = false;
System.out.printf("Please enter the %d number\n", i+1);
while(!validate)
{
try
{
grade[i] = keyboard.nextInt();
for(int j=0; j< i; j++)
{
if(grade[i] == grade[j])
throw new Exception();
}
validate = true;
}
catch(Exception ex)
{
System.out.println("That number already exists...\nPlease choose another number.");
}
}
}
System.out.println("All done, the numbers are...");
for(int g : grade)
{
System.out.printf("%d\n", g);
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum