Java字符串数组与char数组问题:初始化及作用域报错排查
Let's tackle each of your problems step by step, with clear explanations and a corrected version of your code:
1. String Array Initialization Type Mismatch
Your first error occurs because you’re trying to create a String[] array but using new int[numcities]—this is a direct type mismatch, since an int[] can’t be converted to a String[].
Fix: Initialize your array with the correct type:
String[] savedcities = new String[numcities];
2. Array Scope Issue with do-while Loop
When you declare savedcities inside the do-while loop, it’s only accessible within that loop block. To use the array outside the loop (for后续 operations), you need to declare the variable before the loop starts, and only initialize it inside the loop.
Fix: Move the array declaration outside the do-while loop:
String[] savedcities; // Declare outside loop for external access do { // ... other code ... savedcities = new String[numcities]; // Initialize inside loop // ... populate array ... } while (/* retry condition */); // Now savedcities is accessible here
3. Empty Line Issue After nextInt()
The nextInt() method only reads the numeric input, leaving the newline character (\n) in the input buffer. When you call nextLine() right after, it reads this leftover newline as an empty string, which breaks your city name input flow.
Fix: Add an extra nextLine() call immediately after nextInt() to consume the unused newline:
System.out.println("How many cities would you like to enter?"); int numcities = scanner.nextInt(); scanner.nextLine(); // Consume the remaining newline
Alternatively, you can read the entire line as a string and parse it to an int to avoid this issue entirely:
int numcities = Integer.parseInt(scanner.nextLine());
Corrected Full Code
Here's the complete fixed program with all the above fixes applied:
import java.util.Scanner; public class CityArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] savedcities; // Declare array outside loop for external access boolean isCorrect; do { // Program intro System.out.println("This program lets you create an array to store city names."); // Get number of cities, fix nextInt() newline issue System.out.println("How many cities would you like to enter?"); int numcities = scanner.nextInt(); scanner.nextLine(); // Consume leftover newline // Initialize array with correct String type savedcities = new String[numcities]; // Get city names from user for (int i = 0; i < numcities; i++) { System.out.println("Enter city name " + (i + 1) + ":"); savedcities[i] = scanner.nextLine(); } // Display entered cities and ask for confirmation System.out.println("\nYou entered the following cities:"); for (String city : savedcities) { System.out.println("- " + city); } System.out.println("\nAre these names correct? (yes/no)"); String confirmation = scanner.nextLine().toLowerCase(); isCorrect = confirmation.equals("yes"); } while (!isCorrect); // Use the savedcities array outside the loop System.out.println("\nFinal city list saved successfully!"); System.out.println("Your cities are:"); for (String city : savedcities) { System.out.println("- " + city); } scanner.close(); } }
内容的提问来源于stack exchange,提问作者MacKenzie Cutler

