You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:完善Java中读取10个整数并通过parseInt解析输出的代码

Hey there! Let's fix up your Java code step by step—it has a few syntax issues and incomplete parts that are keeping it from working correctly. Here's what's wrong and how to get it running smoothly:

Key Issues in Your Original Code

  • Incorrect main method definition: The entry point of a Java program must follow the exact signature public static void main(String[] args)—your code uses a constructor-style method instead, which the JVM won't recognize as the program start.
  • Missing Scanner import: You're using Scanner but haven't imported it, so the compiler won't know where to find this class.
  • Unfinished for loop: The loop structure is cut off, and there's no logic to actually parse the split strings into integers.
  • No handling for invalid input: If the user enters non-integer values or extra spaces, Integer.parseInt() will crash the program.
  • Class name convention: Java uses PascalCase for class names (so variableGrowth should be VariableGrowth).

Corrected & Complete Code

import java.util.Scanner;

public class VariableGrowth { 
    // Standard main method signature for Java program entry
    public static void main(String[] args) { 
        System.out.print('\u000c'); // Clear console (works in most terminal/IDE environments)
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter 10 integers separated by spaces:");
        String inputInt = scan.nextLine(); 
        System.out.println("Raw input you entered: " + inputInt); 
        
        // Split on one or more spaces to handle multiple spaces, leading/trailing spaces
        String[] items = inputInt.split("\\s+"); 
        int[] results = new int[10]; // We need exactly 10 integers
        int validCount = 0;
        
        for (int i = 0; i < items.length; i++) {
            // Skip empty strings caused by extra spaces
            if (!items[i].isEmpty()) {
                try {
                    results[validCount] = Integer.parseInt(items[i]);
                    validCount++;
                    // Stop parsing once we have our 10 integers
                    if (validCount == 10) break;
                } catch (NumberFormatException e) {
                    System.out.println("Warning: '" + items[i] + "' isn't a valid integer—skipping it.");
                }
            }
        }
        
        // Check if we got enough valid integers
        if (validCount < 10) {
            System.out.println("Error: Only received " + validCount + " valid integers. We need 10.");
            scan.close();
            return;
        }
        
        // Output the parsed integers
        System.out.println("\nYour parsed 10 integers:");
        for (int num : results) {
            System.out.print(num + " ");
        }
        
        scan.close(); // Clean up the scanner resource
    }
}

What Changed & Why

  1. Fixed main method: Used the required public static void main(String[] args) so the JVM can launch your program.
  2. Added Scanner import: import java.util.Scanner tells the compiler where to locate the Scanner class.
  3. Improved input splitting: split("\\s+") handles multiple spaces, leading/trailing spaces, which avoids empty string entries in the array.
  4. Added error handling: The try-catch block catches invalid number inputs, so the program doesn't crash unexpectedly.
  5. Enforced 10 integers: We initialize the results array to size 10, track valid inputs, and alert the user if they don't provide enough valid numbers.
  6. Cleaned up resources: Closed the Scanner to avoid potential resource leaks.

内容的提问来源于stack exchange,提问作者kp-a

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:30:21