0

I am working on an array that stores a string of name and an integer of age, I want the array to be a max of 4 and print out these results, unless the user enters the word 'done', if so the program terminates and the array outputs what it has stored.

The array works fine but can't seem to get the 'done' section, that I am currently using an if statement with .equals

any suggestions?

Thanks

import java.util.Scanner;
import java.util.ArrayList;

public class NameAge {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        final int MAX_VALUE = 4;
        //boolean name = false;

        ArrayList<String> nameList = new ArrayList<String>();
        ArrayList<Integer> ageList = new ArrayList<Integer>();

        Integer[] ages = new Integer[4];

        for (int i = 0; i < MAX_VALUE; i++) {

            System.out.print("Enter a name: ");
            String currentLine = input.next();

            if (currentLine.equals("done")) {
            break;
            }

            nameList.add(currentLine);

            System.out.print("Now enter an age for " + currentLine + ": ");
            ageList.add(input.nextInt());

          }

        System.out.print("\n");
        for(int i = 0; i < MAX_VALUE; i++) {

            System.out.println("Name: " + nameList.get(i) + "   Age: " + ageList.get(i));


        }


    // display youngest and oldest member of array
    int smallest = ageList.get(0);
        int largest = ageList.get(0);

        String oldest = nameList.get(0);
        String youngest = nameList.get(0);

        for (int i = 0; i < ages.length; i++) {
            if(ageList.get(i) > largest) {
                largest = ageList.get(i);
                oldest = nameList.get(i);

            } else if(ageList.get(i) < smallest) {
                smallest = ageList.get(i);
                youngest = nameList.get(i);
            }
        }
                        System.out.println("\nThe youngest person is " + youngest + " who is " + smallest + " years old");
            System.out.println("The oldest person is " + oldest + " who is " + largest + " years old");


        }

    }
  • Possible duplicate of [How to terminate Scanner when input is complete?](http://stackoverflow.com/questions/16206813/how-to-terminate-scanner-when-input-is-complete) – DimaSan Nov 22 '16 at 13:10
  • 2
    your `nameList` is a `ArrayList` and though wont equal the `String` `"DONE"`. Also your variable `name` seems to be useless, as it´s only use is inside an `if` where it´s getting set to true. You might want to read the input before the `if` statments into a `String input;` and work up what your current input should do from there. – SomeJavaGuy Nov 22 '16 at 13:10

4 Answers4

0

Ok, there are a couple of things to review in that code.

Variable name serves no purpose there, as it is only getting set to true each iteration. (You are maybe confusion = with == inside if statement?

Then, nameList is an ArrayList, so you can't compare it to a String, you need to get the value i of the ArrayList an then compare, such as:

if (nameList.get(i).equals("DONE")) {    
    break; 
}
Manuel S.
  • 411
  • 8
  • 21
0
  • Remove the name boolean variable , it seems not useful.

  • Why using two scanners. One is enough.

         Scanner input2 = new Scanner(System.in);
    

    is not needed.

  • Store first input.nextLine() in a temporary variable and not in the final list in order to decide what do : going on or stopping :

    String currentLine = input.nextLine()

  • ages array is not needed. You have already a List to store age values :

    ArrayList<Integer> ageList

  • Scanner.nextLine() reads the whole line. It may have side effects. In your case, you need only line current input : use only next() or nextInt().

It should do the job :

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    final int MAX_VALUE = 4;

    ArrayList<String> nameList = new ArrayList<String>();
    ArrayList<Integer> ageList = new ArrayList<Integer>();

    for (int i = 0; i < MAX_VALUE; i++) {

        System.out.print("Enter a name: ");
        String currentLine = input.next();

        if (currentLine.equals("DONE")) {
        break;
        }

        nameList.add(currentLine);

        System.out.print("Now enter an age for " + currentLine + ": ");
        ageList.add(input.nextInt());

      }

    }    
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thanks! @davidxxx. I have other code below this now seen within my original post that displays the highest and lowest name and age (contained within array) Although applying your solution produces the following error - Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at NameAge.main(NameAge.java:35) Any suggestions? –  Nov 22 '16 at 13:28
  • Your exception refers to a get() call on an ArrayList instance and in my code, I don't use get() method on an ArrayList instance – davidxxx Nov 22 '16 at 13:41
0

First, why do you need: if(name = true) {} You don't need that, and if you want to test if name is true you use double ==.

Second, with this line: nameList.equals("DONE") you are checking if ArrayList nameList object is equal to "DONE". You are not checking the elements of the ArrayList.

If you want to stop your program when someone enters word "DONE" then this is the code:

Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);

final int MAX_VALUE = 4;
boolean name = false;
String chk = "";

ArrayList<String> nameList = new ArrayList<>();
ArrayList<Integer> ageList = new ArrayList<>();

Integer[] ages = new Integer[4];

for (int i = 0; i < MAX_VALUE; i++) {

    System.out.print("Enter a name: ");

    chk = input.nextLine();

    if (chk.equals("DONE"))
        break;

    nameList.add(chk);

    System.out.print("Now enter an age for " + nameList.get(i) + ": ");
    ageList.add(input2.nextInt());

}

If you want to enter word "DONE" to array and then check if array contains "DONE" then code is like this:

Scanner input = new Scanner(System.in);
          Scanner input2 = new Scanner(System.in);

          final int MAX_VALUE = 4;
          boolean name = false;
          String chk = "";

          ArrayList<String> nameList = new ArrayList<>();
          ArrayList<Integer> ageList = new ArrayList<>();

          Integer[] ages = new Integer[4];

          for(int i = 0; i < MAX_VALUE; i++ ) {

                  System.out.print("Enter a name: ");
                  nameList.add(input.nextLine());
                  if (nameList.contains("DONE"))    //use contains
                      break; 



              System.out.print("Now enter an age for " + nameList.get(i) + ": ");
              ageList.add(input2.nextInt());              

          }  
user1598696
  • 550
  • 1
  • 4
  • 22
0

The reason you get a index out of bounds is because you have a max value set to 4, that youre trying to loop even if the array is smaller. Since you have a person connected to an age, you can use HashMap to set name as key, and age as value and then use a lambda expression to print the hashmap when done adding new entries like this:

    Scanner input = new Scanner(System.in);
    final int MAX_VALUE = 4;
    HashMap<String, Integer> people = new HashMap<String, Integer>();

    for (int i = 0; i < MAX_VALUE; i++) {
        System.out.print("Enter a name: ");
        String name = input.next();

        if (name.equals("done")) {break;}

        System.out.print("Now enter an age for " + name + ": ");
        int age = input.nextInt();

        people.put(name, age);
    }
    input.close();
    System.out.print("\n");
    people.forEach((k,v)->System.out.println("Name : " + k + "  Age : " + v));
Alex Karlsson
  • 296
  • 1
  • 11