-2

I'am trying to write a program in Java that collects users favorite names within an array and then prints these names at the end.

NOTE: the length of the array should be defined by the number of names a user enters.

Please take a look at the code and tell me how to fix it. Here is what I have done so far

Scanner input = new Scanner(System.in);

System.out.println("What are your most favorite names?");
        
String[] favNames = new String[i];
int i = 1;
            
while (true){
    System.out.print("Please enter favorite name number" + i + ":  ");
    favNames[i-1] = input.next();
    System.out.print("Is that all?");
    String ans = input.next().toLowerCase();
    if(ans.startsWith("y")){
        System.out.print("Here is the completed list of your favorite names:\n" + Arrays.toString(favNames));
        break;
    }
    i++;
}

This is the error code I get:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable i
location: class JavaApplication13
at JavaApplication13.main(JavaApplication13.java:52)
Java Result: 1

I tried moving the first parts of the code inside the loop but it only prints one of the names the user enters.

//These parts:
String[] favNames = new String[i];
int i = 1;

If I swap the places between the first and second line. The array gets only 1 entry from the user.

//Only gets one entry
int i = 1;
String[] favNames = new String[i];

3 Answers 3

1

Variables should be declared before use. This is why your program is not working.

Change

String[] favNames = new String[i];
int i = 1;

To

int i = 1;
String[] favNames = new String[i];

But, keep in mind that in this case the used can only input 1 time(because arrays have fixed size). If you want to use an arbitrary number of input, you have to use ArrayList or similar types.

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

Comments

0
  1. declare variables before using them:
        int i = 1;
        String[] favNames = new String[i];

However, an array of size 1, will only work for 1 entry. If you would like the user to be able to enter more than that you need a bigger array.

I would suggest an arbitrarily large size (say 100), then check that the entry fits in the array; if it doesn't, game over.


String[] favNames = new String[100];

int i = 1;
while (true) {
    System.out.print("Please enter favorite name number" + i + ":  ");
    favNames[i - 1] = input.next();
    System.out.print("Is that all?");
    String ans = input.next().toLowerCase();
    if (ans.startsWith("y")) {
        System.out.print("Here is the completed list of your favorite names:\n" + Arrays.toString(favNames));
        break;
    }
    i++;
    if (i > favNames.length)
        System.out.print("Can't accept more than "+favNames.length+" entries, here is the completed list of your favorite names:\n" + Arrays.toString(favNames));
}

The issue then is that Arrays.toString will print null for the empty entries.

I would make a custom routine to print the array that ends on the first null:


static String printFavNames(String[] favNames) {
    String output = "[";
    for (String s : favNames) {
        if (s==null)
            break;
        if (output.length() > 1)
            output += ", ";
        output += s;
    }
    output += "]";
    return output;
}

related question: How to print all non null elements of an array using Array.toString()

full code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("What are your most favorite names?");

        String[] favNames = new String[100];

        int i = 1;
        while (true) {
            System.out.print("Please enter favorite name number" + i + ":  ");
            favNames[i - 1] = input.next();
            System.out.print("Is that all?");
            String ans = input.next().toLowerCase();
            if (ans.startsWith("y")) {
                System.out.print("Here is the completed list of your favorite names:\n" + printFavNames(favNames));
                break;
            }
            i++;
            if (i > favNames.length)
                System.out.print("Can't accept more than "+favNames.length+" entries, here is the completed list of your favorite names:\n" + printFavNames(favNames));
        }
        input.close();
    }
    static String printFavNames(String[] favNames) {
        String output = "[";
        for (String s : favNames) {
            if (s==null)
                break;
            if (output.length() > 1)
                output += ", ";
            output += s;
        }
        output += "]";
        return output;
    }
}

2 Comments

Thanks a lot!<br> The assignment question asked me to create an array which its length is defined by the user.<br>
That wasn't in the question. You were still creating an array of size 1, not a size defined by the user, rather than using 1, ask the user for the size.
0

Just move the i++ to inside the while loop and all be good!

1 Comment

thank u for the comment! but it doesn't still work.

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.