0

I'm working on a class that will take user input to assign values to an object created in a source class. Those objects will then be added to an array, which then prints the values on it. However, the "list" under print : list is telling me that I need to initialize the variable. Why is it not recognizing that this is an array even though it seems to work fine in my do loop?

import java.util.Scanner;
import name.Names;

 public class NameTester {

 public static void main(String[] args) {
    // TODO Auto-generated method stub
    String entry;
    Scanner firstscan = new Scanner(System.in);
    Scanner lastscan = new Scanner(System.in);
    Scanner codescan = new Scanner(System.in);
    Scanner entryscan = new Scanner(System.in);
    String first;
    String last;
    int code;

    System.out
            .println("This program will prompt you to input first name, +"
                    + "last name, and zip code for an individual. Hit \"x\" when finished\n");

    do {
        System.out.println("Enter first name:");
        first = firstscan.nextLine();
        System.out.println("Enter last name:");
        last = lastscan.nextLine();
        System.out.println("Enter zip code:");
        code = codescan.nextInt();

        Names nm = new Names(first, last, code);

        Names[] list = new Names[25];

        int count = 0;
        list[count] = nm;
        count++;

        System.out
                .println("To quit hit \"X\" or any other key followed by enter to continue:");
        entry = entryscan.nextLine();

    } while (!(entry.equalsIgnoreCase("x")));

    for (Names print : list)
        System.out.println(print + "");
}
}

2 Answers 2

2

For one, you are instantiating your array inside your loop, that means every time your loop runs through, it creates a new array instead of updating the old one. Then, once you leave your loop, you leave its "scope". That means everything you declare inside the loop is not available outside. The solution is to declare your array outside the loop.

Every block in java has its own scope (defined through brackets). While you can access variables that have been declared outside your block while inside it, it does not work the other way around; as you can see. Just google java scope, and you will understand more. Hope that helps ;)

Sign up to request clarification or add additional context in comments.

8 Comments

not "pretty much" ... every block (not limited to control blocks) has it's own scope... see JLS 6.3. Scope of a Declaration and 14.2. Blocks
Good call, thanks, I forgot to elaborate, but I fixed it now :)
This worked, thanks! The weird thing now is that while everything compiles and executes as it should, I now get this error at the end of the program: Exception in thread "main" java.lang.NullPointerException at name.NameTester.main(NameTester.java:53)
That means there is a NullPointerException in line 53, but what is at line 53? A NullPointerException occurs when you are trying to work with something that has no value (that is null). Also did you move your index declaration out of the loop, too? Otherwise you still have that scope issue, now with the index.
Yep, I moved the index out of the loop. Line 53 is System.out.println(print.toString()); If I take out "toString," it still prints the list but also prints "null" about 20 times.
|
1

You will need a method in the class Name that return the first, last name and the zip code because if you just use:

System.out.println(print + "")

You are printing the object Name and no the String that represents the attributes saved in the object.

For example you can have the method in the class Name:

String getFirst()
{
       return this.first;
}

And the last line in your class Nametester can be

System.out.println(print.getFirst() + "");

1 Comment

I actually have this in my Name class. I changed the print to use the method, which works great. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.