-1

i'm stay crazy i just want use the loop while to read some ages on Java terminal

public static void main(String[] args){

    Scanner keyboard = new Scanner(System.in);

    int lenghtMax = 100;

    int[] age = new int[lenghtMax];
    int status = 1;
    String next; 
    int i = 0;

    while(status == 1){

        if (i >= 100){

            System.out.println("You cant register more ages!\n");

        } else {                

            System.out.println("What the age?");
            age[i] = keyboard.nextInt();
            i++;

            System.out.println("Do you want register other age? y/n");
            next = keyboard.next();

            if (next == "y"){

                status = 1;

            } else {

                status = 0;
                keyboard.close();

            }

        }

    }   

    System.out.println("Passing Here!");
}

This is my code, where is the error, because it print that message!

Output

What the age?
45
Do you want register other age? y/n
y
Passing Here!

Why it is printing that message?, the code should get back on the while loop, my while is a normal while!

Gabriel Gomes
  • 139
  • 1
  • 3
  • 20

1 Answers1

2

I guess you are a newbie to Java world. String comparison in Java should be done by using equals(Object o) or equalsIgnoreCase(String another) methods and not by ==. There are multiple simpler and better ways to achieve what you are trying to do. Note: Use java.util.Scanner to accept user inputs.

One: If you are using int array to store values -

int lengthMax = 100;
int[] ages = new int[lengthMax];
Scanner sc = new Scanner(System.in);
        /*
         * Just iterate through the length of the array
         */
        for(int i = 0; i < lengthMax; i++) {
            System.out.println("What is the age?");
           /*
            * Read the input and add the same to array
            * at position 'i'.
            */
            ages[i] = Integer.parseInt(sc.nextLine());

           /*
            * Read the choice form the user
            */
            System.out.println("Do you want to register more age? y / n");
            String choice = sc.nextLine();
           /*
            * If choice is a YES - y
            * simply continue with the loop
            */
            if(choice.equals("y")){
                continue;
            } else {
            // If choice is anything other than 'y', break out
                break;
            }
        }
//close the Scanner
sc.close();

Two: If you are using a Collection like ArrayList, to store the ages -

int lengthMax = 100;
List<Integer> agesList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
/*
 * Create an infinite loop
 */
while(true) {
          /*
           * At each run, check if the size of the ArrayList is
           * equal to the max length of ages defined.
           *
           * If yes, simply print your message and break out.
           */
            if(agesList.size() == lengthMax) {
                System.out.println("You can't register more ages!!");
                break;
            }

           /*
            * Start accepting the ages, and add each to the list.
            */
            System.out.println("What is the age?");
            agesList.add(Integer.parseInt(sc.nextLine()));

            /*
             * Ask if user is still interested to register ages.
             */
            System.out.println("Do you want to register more age? y / n");
            String choice = sc.nextLine();

            // If yes, continue with the loop, else simply break out.
            if(choice.equals("y")){
                continue;
            } else {
                break;
            }
        }
sc.close();

I hope this'll help you in your learning process.

Shyam Baitmangalkar
  • 1,075
  • 12
  • 18