Using exception for validate inputs - java

I'm new to programming & I tried to make an algorithm to find the grade of the marks entered by the user for each subject. At the beginning of the program user inputs all the marks for each subject. After that computer should automatically print all the subjects in the following order subject - mark - grade.
I used the do-while loop for avoiding when the user inputs an out of range number. Also, I thought to use try-catch and do-while to avoid getting an invalid variable type from the user. I put the whole try-catch into another do-while loop. So, it has to be loop until the user inputs an integer value when an exception exists. But when I enter a wrong variable type like A while the program is running, the computer decides that all the remaining inputs are invalid variables without the user's entry and prints the "marks" as zero and null for all those "grades" >>Subject_4 - 0 - null. Otherwise, if I input a correct variable it should print like this >>Subject_1 - 56 - C. How can I fix this? Please help me.
public class findResults {
int getMark;
int setMark;
String setGrade;
int arrayIndex;
String [] subjects = {"Subject_1","Subject_2","Subject_3","Subject_4"};
int [] marks = new int [subjects.length];
String[] grades = new String [subjects.length];
public findResults(){
Scanner scan = new Scanner (System.in);
System.out.println("Enter your marks for each subjects!");
for (int t = 0;t<subjects.length;t++){
arrayIndex = t;
int x = 0;
do{
try {
do{
System.out.print(subjects[t]+" - ");
getMark = scan.nextInt();
setMark = ((getMark <= 100 && getMark >= 0)? getMark : (x=x-1));
x = x+1;
}while(x!=1);
} catch (Exception e) {
System.out.println("Please input valid number ..!");
}
}while(x!=x);
}
for (int i = 0; i<subjects.length;i++)
System.out.println(subjects[i]+" - "+marks[i]+" - "+ grades[i]);
}
public void setResult(){
if (setMark >= 75){
setGrade = "A";
marks[arrayIndex]= setMark;
grades[arrayIndex]= setGrade;
}
else if(setMark >=65){
setGrade = "A";
marks[arrayIndex]= setMark;
grades[arrayIndex]= setGrade;
}
else if(setMark >=65){
setGrade = "B";
marks[arrayIndex]= setMark;
grades[arrayIndex]= setGrade;
}
else if(setMark >=55){
setGrade = "C";
marks[arrayIndex]= setMark;
grades[arrayIndex]= setGrade;
}
else if(setMark >=35){
setGrade = "S";
marks[arrayIndex]= setMark;
grades[arrayIndex]= setGrade;
}
else if(setMark >0){
setGrade = "W";
marks[arrayIndex]= setMark;
grades[arrayIndex]= setGrade;
}else{
setGrade = "F";
marks[arrayIndex]= setMark;
grades[arrayIndex]= "Fail";
}
}
}

Consider the following.
private static String setGrade(int setMark) {
String setGrade;
if (setMark >= 75) {
setGrade = "A";
}
else if (setMark >= 65) {
setGrade = "B";
}
else if (setMark >= 55) {
setGrade = "C";
}
else if (setMark >= 35) {
setGrade = "S";
}
else if (setMark > 0) {
setGrade = "W";
}
else {
setGrade = "F";
}
return setGrade;
}
public static void main(String[] args) {
String[] subjects = {"Subject_1", "Subject_2", "Subject_3", "Subject_4"};
int[] marks = new int[subjects.length];
String[] grades = new String[subjects.length];
Scanner scan = new Scanner(System.in);
System.out.println("Enter your marks for each subject.");
for (int t = 0; t < subjects.length; t++) {
System.out.printf("%s - ", subjects[t]);
try {
String str = scan.nextLine();
marks[t] = Integer.parseInt(str);
if (marks[t] < 0 || marks[t] > 100) {
System.out.println("Between 0 and 100, please. Re-enter.");
t--;
continue;
}
else {
grades[t] = setGrade(marks[t]);
}
}
catch (NumberFormatException xNumberFormat) {
System.out.println("Invalid value. Re-enter.");
t--;
continue;
}
}
for (int i = 0; i < subjects.length; i++) {
System.out.println(subjects[i] + " - " + marks[i] + " - " + grades[i]);
}
}
Below is the output from a sample run.
Enter your marks for each subject.
Subject_1 - -2
Between 0 and 100, please. Re-enter.
Subject_1 - a
Invalid value. Re-enter.
Subject_1 - 10
Subject_2 - 90
Subject_3 - 4.3
Invalid value. Re-enter.
Subject_3 - 43
Subject_4 - 007
Subject_1 - 10 - W
Subject_2 - 90 - A
Subject_3 - 43 - S
Subject_4 - 7 - W

Related

Check password, verify, and loop again

I have a problem that requires at least 2 uppercase letters, at least 3 lowercase letters and 1 digits.
Here is the exact problem:
Write an application that prompts the user for a password that contains at least two uppercase letters, at least three lowercase letters, and at least one digit. Continuously prompt the user until a valid password is entered. Display Valid password if the password is valid; if not, display the appropriate reason(s) the password is not valid as follows:
For example, if the user enters "Password" your program should output: Your password was invalid for the following reasons: uppercase letters digits
If a user enters "passWOrd12", your program should output: valid password
Here is my coding so far, I am having the problem of the program promting the user to enter in more passwords once they enter and incorrect one.
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
System.out.println("");
}
public static String PassCheck(String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58)
|| (Password.charAt(x) >= 64 && Password.charAt(x) <= 91)
|| (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
capCount++;
}
length = (x + 1);
}
if (numCount < 2) {
result = "digits";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (numCount < 2 && capCount < 2) {
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}
You need a while loop. This will ensure that your code will keep looping until it succeeds. When it succeeds, the boolean becomes true and it doesn't loop. Write whatever you want to happen next after the while loop.
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean success=false;
while(!success){
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
if(PassCheck(inputPassword).equals("Valid Password")) success = true;
System.out.println("");
}
}
you can do it by changing the return type of PassCheck method to a boolean result and use Character class to check for Uppercase Lowercase Digit characters and count occurence of each one at the end check for conditions and if it pass then the result is true else it is false and you can iterator your do-while according to result of PassCheck method
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean isValidPassword = false;
do {
System.out.print("Password: ");
inputPassword = input.next();
isValidPassword = PassCheck(inputPassword);
System.out.println("");
}while(!isValidPassword);
}
public static boolean PassCheck(String Password) {
boolean isValid = false;
String result = "";
int numCount = 0;
int capCount = 0;
int lowCount = 0;
for (int x = 0; x < Password.length(); x++) {
if(Character.isDigit(Password.charAt(x))) {
numCount++;
}
if(Character.isUpperCase(Password.charAt(x))) {
capCount++;
}
if(Character.isLowerCase(Password.charAt(x))) {
lowCount++;
}
}
if(numCount >= 1 && capCount >= 2 && lowCount >= 3) {
result = "Password Valid";
isValid = true;
} else {
isValid = false;
result = "Password is Invalid: ";
if(Password.length() < 2) {
result += " Password is Too Short!";
}
if(numCount < 1) {
result += " no digit";
}
if(capCount < 2) {
result += " at lease 2 Uppercase";
}
if(lowCount < 3) {
result += " at lease 3 Lowercase";
}
}
System.out.println(result);
return isValid;
}
}
Below is the finished code that worked on the Cengage platform for me:
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean success=false;
while(!success){
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
if(PassCheck(inputPassword).equals("Valid Password")) success = true;
System.out.println("");
}
}
public static String PassCheck(String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
(Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
capCount++;
}
length = (x + 1);
}
if (numCount < 2) {
result = "digits";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (numCount < 2 && capCount < 2)
{
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}

Returning a set of array to the main method using for loop, gives error deadcode

Currently programming a Clock with an alarm and met my first Dead Code error.
User input have already stored data into the following variables; aAlarm, aHour, and aMinute.. but I can't seem to get them to display into the main method. I have tried searching other problems regarding dead error and none seem to solve my problem. Below is the code, the variable 'instances' equals to 1 and will increment for the amount of times the user creates an alarm.
import java.util.*;
public class Frontend {
public static void main(String args[]) {
Backend nyet = new Backend();
Scanner scn = new Scanner(System.in);
int dec, dec2;
System.out.print("The time is: ");
System.out.println(nyet.displayClock());
//Class clock----------------------------
//Class setTime--------------------------
System.out.print("Do you wish to alter time| 1 = Yes, 0 = No:");
dec = scn.nextInt();
if (dec == 1) {
System.out.print("Input Hour:");
int hour = scn.nextInt();
if (hour < 0 || hour > 24) {
System.out.println("Sorry, there are only 24hrs in one day.");
System.exit(0);
}
System.out.print("Input Minute:");
int minute = scn.nextInt();
if (minute < 0 || minute > 60) {
System.out.println("Sorry, there are only 60mins in one hour.");
System.exit(0);
}
System.out.print("Input Second:");
int second = scn.nextInt();
if (second < 0 || second > 60) {
System.out.println("Sorry, there are only 60second in one minute.");
System.exit(0);
}
nyet.setTime(hour, minute, second);
scn.close();
System.out.print("The time is: ");
System.out.println(nyet.displayClock());
} //Class setTime--------------------------
//Class setAlarm-------------------------
System.out.print("Do you wish to set an alarm| 1 = Yes, 0 = No:");
int dec1 = scn.nextInt();
if (dec1 == 1) {
do {
int instc = 1;
System.out.print("Input alarm number:");
int aNum = scn.nextInt();
System.out.print("Input Hour:");
int aHr = scn.nextInt();
if (aHr < 0 || aHr > 24) {
System.out.println("Sorry, there are only 24hr in one day.");
System.exit(0);
}
System.out.print("Input Minute:");
int aMin = scn.nextInt();
if (aMin < 0 || aMin > 60) {
System.out.println("Sorry, there are only 60mins in one hour.");
System.exit(0);
}
System.out.print("Do you wish to set another alarm| 1 = Yes, 0 = No:");
dec2 = scn.nextInt();
if (dec2 == 1)
instc++;
nyet.setAlarm(instc, aNum, aHr, aMin);
}while (dec2 != 0);
} //Class setAlarm-------------------------
System.out.print("Show alarm| 1 = Show, 0 = Nothing:");
int z = scn.nextInt();
if (z == 1)
nyet.displayAlarm();
}
}
import java.time.OffsetTime;
public class Backend {
OffsetTime nyet = OffsetTime.now();
private int cHour, cMinute, cSecond, instances;
private int[] aAlarm, aHour, aMinute;
private boolean[] alarmOn;
public Backend() {
cHour = nyet.getHour();
cMinute = nyet.getMinute();
cSecond = nyet.getSecond();
aHour = new int[2];
aMinute = new int[2];
aAlarm = new int[2];
alarmOn = new boolean[2];
for (int i = 0; i < 2; i++) {
alarmOn[i] = !alarmOn[i];
}
}
public void setAlarm(int instncs,int aNmbr, int aHr, int aMnt) {
for (int i = 0; i < instncs; i++) {
aAlarm[i] = aNmbr;
aHour[i] = aHr;
aMinute[i] = aMnt;
instances = instncs;
}
}
public void setTime(int hr, int min, int sec) {
cHour = hr;
cMinute = min;
cSecond = sec;
}
public String displayClock() {
return String.format("%02d:%02d:%02d", cHour, cMinute, cSecond);
}
public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< Dead Code
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
}
}
When I entered the code for your class Backend in my Eclipse, it showed a build error for method displayAlarm(), namely...
This method must return a result of type String
Here is the code for method displayAlarm() (exactly as it appears in your question).
public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< Dead Code
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
}
It is possible that the for loop in the method will not be entered and in that case the method does not return anything. So I just added a line to get rid of the build error.
public String displayAlarm() {
for (int i = 0; i < instances; i++) { //<<< Dead Code
return String.format("%02d:%02d:%02d", aAlarm[i], aHour[i], aMinute[i]);
}
return "";
}
After adding the line, I got the dead code warning. I admit that it took me a while to discover the reason. Finally it dawned on me. The only thing in the for loop body is return. Hence there will only ever be precisely one loop iteration, so why increment i?
Guess the problem was the class had no 'static' in it.
Front end code:
public static void main(String args[]) {
int dec, dec2, amount = 0, deci0;
Backend nyet = new Backend();
Scanner scn = new Scanner(System.in);
System.out.print("Do you wish to set an alarm| 1 = Yes, 0 = No:");
int dec1 = scn.nextInt();
if (dec1 == 1) {
do {
System.out.print("Input alarm number(Stored = " + amount + "):");
int aNum = scn.nextInt();
System.out.print("Input Hour:");
int aHr = scn.nextInt();
if (aHr < 0 || aHr > 24) {
System.out.println("Sorry, there are only 24hr in one day.");
System.exit(0);
}
System.out.print("Input Minute:");
int aMin = scn.nextInt();
if (aMin < 0 || aMin > 60) {
System.out.println("Sorry, there are only 60mins in one hour.");
System.exit(0);
}
nyet.setAlarm(amount, aNum, aHr, aMin);
System.out.print("Do you wish to set another alarm(max 3)| 1 = Yes, 0 = No:");
dec2 = scn.nextInt();
if (dec2 == 1) {
amount++;
}else if (dec2 == 0) {
amount++;
nyet.setAlarm(amount, aNum, aHr, aMin);
}else {
System.out.println("The only choices are '1' and '0'.");
System.exit(0);
}
if (amount > 3) {
System.out.println("You have reached maximum storage.");
dec2 = 0;
}
}while (dec2 != 0);
}
Back end code:
public class Backend {
OffsetTime nyet = OffsetTime.now();
private int cHour, cMinute, cSecond;
private static int[] aAlarm, aHour, aMinute;
private boolean[] alarmOn;
private static int amnt;
public void setAlarm(int instncs,int aNmbr, int aHr, int aMnt) {
int i = instncs;
aAlarm[i] = aNmbr;
aHour[i] = aHr;
aMinute[i] = aMnt;
amnt = instncs;
}
public static void displayAlarm() {
for (int i = 0; i < amnt; i++) {
System.out.println("Alarm #" + aAlarm[i] + " - " + aHour[i] + ":" + aMinute[i]);
}
}

I need a Java Scanner 'reset' on invalid character fix

I am a new human learning to code!
I had a problem with my Scanner, which is that I need it to 'reset' on an invalid character.
My code:
public class Lemonade {
static int m = 150;
private static Scanner scan;
public static void main(String[] args) {
int day = 1;
for(int gameover = m; gameover > 0; day++) {
int Random = (int) (Math.random() * 100);
if(Random <= 25) {
System.out.println("Great Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 50) {
System.out.println("Good Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 75) {
System.out.println("Bad Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 100) {
System.out.println("Awful Chance!");
System.out.println("--------------------------------");
}
int count = 0;
int none = 0;
scan = new Scanner(System.in);
System.out.println("Enter a number between 0 and " + m + "!");
count = scan.nextInt();
if(count >= none && count <= m) {
System.out.println("You entered " + count + "!");
System.out.println("--------------------------------");
day = day + 1;
m = m - count;
System.out.println("Day " + day);
}
else {
System.out.println("Enter a number between 0 and " + m + ".");
count = scan.nextInt();
}
}
}
}
Now is my question how to get this to 'reset' on an invalid character like 'f', as Scanner only accepts numbers.
Thanks for the help!
If I understand you correctly then this is something you're looking for,
InputMismatchException will thrown if user enters invaild characters instead of int. You may use looping until the user enters an integer
import java.util.Scanner;
import java.util.InputMismatchException;
class Example
{
public static void main(String args[])
{
boolean isProcessed = false;
Scanner input = new Scanner(System.in);
int value = 0;
while(!isProcessed)
{
try
{
value = input.nextInt();
//example we will now check for the range 0 - 150
if(value < 0 || value > 150) {
System.out.println("The value entered is either greater than 150 or may be lesser than 0");
}
else isProcessed = true; // If everything is ok, Then stop the loop
}
catch(InputMismatchException e)
{
System.out.print(e);
input.next();
}
}
}
}
If this is not you're looking for please let me know!

Output multiple strings from scanner input

The purpose of my program is to accept ints, doubles, and strings from the user and when the program is terminated by inputing the word "quit", the program averages the ints and doubles, and outputs the submitted strings. Here is what i have so far:
import java.util.*;
public class Lab09 {
public static void main(String [] args) {
Scanner console = new Scanner(System.in);
double sumI = 0;
double sumD = 0;
String words = "";
int numInputInt = 0;
int numInputDoub = 0;
do {
System.out.print("Enter something: ");
if (console.hasNextInt()) {
int numI = console.nextInt();
if (numI >= -100 && numI <= 100) {
sumI += numI;
numInputInt++;
}
else {
System.out.println("Integer out of range!(-100 .. 100)");
}
}
else if (console.hasNextDouble()) {
double numD = console.nextDouble();
if (numD >= -10.0 && numD <= 10.0) {
sumD += numD;
numInputDoub++;
}
else {
System.out.println("Double out of range!(-10.0 .. 10.0)");
}
}
else {
words = console.next();
}
} while (!words.equalsIgnoreCase("quit"));
System.out.println("Program terminated...");
double avgInt = sumI / numInputInt;
double avgDoub = sumD / numInputDoub;
if (numInputInt > 0) {
System.out.println("\tAveragae of Integers: " + avgInt);
}
else {
System.out.println("\tNo intergers submitted");
}
if (numInputDoub > 0) {
System.out.println("\tAverage of Doubles: " + avgDoub);
}
else {
System.out.println("\tNo doubles submitted");
}
System.out.println(words);
}
}
The ints and doubles get processed well, but im stuck in the strings. Any ideas on how to go about doing so?
Thanks in advance!
While you could build a List<String> of words it looks like you just want to concatenate each new word to your String words like,
if (words.length() > 0) words += " "; // <-- add a space.
words += console.next(); // <-- += not =
Then change your loop test to something like,
while (!words.trim().toLowerCase().endsWith("quit"));
And it should work something like you would expect.
You could use
String input = "";
do {
//...
}
else {
input = console.next();
words += input + " ";
}
console.nextLine(); // Read the carriage return
} while (!input.equalsIgnoreCase("quit"));
//...
System.out.println(words.trim());
to concatenate the text, but this is rather inefficient when done within a loop.
A better solution would be to use a StringBuilder...
StringBuilder work = new StringBuilder(128);
String input = "";
do {
//...
}
else {
input = console.next();
words.append(" ").append(input);
}
console.nextLine(); // Read the carriage return
} while (!input.equalsIgnoreCase("quit"));
//...
System.out.println(words);
I just read your comment that you can't use any other class, try this:
import java.util.*;
public class Lab09 {
public static void main(String [] args) {
Scanner console = new Scanner(System.in);
double sumI = 0;
double sumD = 0;
String words = "";
int numInputInt = 0;
int numInputDoub = 0;
do {
System.out.print("Enter something: ");
if (console.hasNextInt()) {
int numI = console.nextInt();
if (numI >= -100 && numI <= 100) {
sumI += numI;
numInputInt++;
}
else {
System.out.println("Integer out of range!(-100 .. 100)");
}
}
else if (console.hasNextDouble()) {
double numD = console.nextDouble();
if (numD >= -10.0 && numD <= 10.0) {
sumD += numD;
numInputDoub++;
}
else {
System.out.println("Double out of range!(-10.0 .. 10.0)");
}
}
else {
words = words.concat(" ").concat(console.next());
}
} while (!words.contains("quit"));
System.out.println("Program terminated...");
double avgInt = sumI / numInputInt;
double avgDoub = sumD / numInputDoub;
if (numInputInt > 0) {
System.out.println("\tAveragae of Integers: " + avgInt);
}
else {
System.out.println("\tNo intergers submitted");
}
if (numInputDoub > 0) {
System.out.println("\tAverage of Doubles: " + avgDoub);
}
else {
System.out.println("\tNo doubles submitted");
}
System.out.println(words);
}
}

Java skipping a number in the sequence

This is very interesting, i notice. Before i can explain further its best i show the code and you will understand what i mean.
This is the code:
public class Qn3 {
static BigDecimal[] accbal = new BigDecimal[19];
private static Integer[] accnums = new Integer[19];
public static void main(String[] args) {
addaccount();
}
public static void addAccount() {
int i = 0, accno, input, j, check;
BigDecimal accbala;
DecimalFormat df = new DecimalFormat("0.00");
Scanner sc = new Scanner(System.in);
Scanner in = new Scanner(System.in);
accnums[1] = new Integer(1);
while (accnums.length >= count(accnums)) {
System.out.print("Enter the account number: ");
while (sc.hasNext("[0-9]{7}")) {
accno = sc.nextInt();
System.out.print("Enter account balance: ");
accbala = in.nextBigDecimal();
for (j = 0; j < accnums.length; j++) {
if (accnums[j] == null)
break;
else if (accnums[j].equals(accno)) {
break;
}
}
if (j == accnums.length) {
System.out.print("No more than 20 accounts can be added.");
} else if (accnums[j] != null) {
if ((accnums[j].equals(accno)))
System.out.println("Account already exists");
break;
} else {
accnums[j] = accno;
accbala = accbala.setScale(2, RoundingMode.HALF_UP);
accbal[j] = accbala;
check = j;
System.out.println("Current number of accounts in the system: "
+ (check + 1)
+ "\nNumber of accounts still can be added: "
+ (20 - (check + 1)));
}
}
while (!sc.hasNext("[0-9]{7}")) {
System.out.println("Wrong NRIC");
break;
}
while (accnums.length <= count(accnums)) {
System.out.println("20 accounts have already been created");
break;
}
break;
}
}
private static int count(Integer[] array) {
int count = 0;
// accnums = new Integer[] {1,2};
for (int index = 0; index < array.length; index++) {
if (array[index] != null) {
count++;
}
}
// System.out.println("You have used " + count + " slots");
return count;
}
}
So now that you have seen the code the problem that is hard to notice is this, take note of the line in the addaccount() method where
System.out.println("Current number of accounts in the system: "+(check+1)+"\nNumber of accounts still can be added: "+(20 - (check+1)));
this line the first check+1 will give me 1 then the next one gives me 3! and then the next time i run the method it gives me 4 and then again 5 and so on so forth, what is happening to 2?
You have that println in an else block, and when j == 1 you're hitting the else if case. Try removing this line
accnums[1] = new Integer (1);

Categories