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

读取逗号分隔ASCII值文件转对应字符的Java代码编译错误求助

修正Java代码:读取逗号分隔的ASCII值并转换为字符

需求:读取存储在逗号分隔.txt文件中的ASCII值,将其转换为对应的字符。以下是存在编译错误的代码,需修正以实现需求。

原代码(存在编译错误)

import java.io.File; 
import java.io.FileNotFoundException;  
import java.lang.reflect.Array; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.function.BinaryOperator;
import java.util.*;

public class Read_int_array_a  
{ 
    private final static String FILE_NAME = "D:/sumit/sample-project/final/file3.txt"; 
    private final static String DELIMITER = ", "; 
    public static void main(String[] args) 
    { 
        File inputFile = new File(FILE_NAME); 
        ArrayList<Integer> numbers = new ArrayList<Integer>(); 
        try  
        { 
            Scanner scanner = new Scanner(inputFile); 
            scanner.useDelimiter(DELIMITER); 
            while(scanner.hasNext()) 
            { 
                String value = scanner.next(); 
                Integer num = Integer.parseInt(value); 
                System.out.print(num);
                numbers.add(num);
            } 
            scanner.close();
                System.out.println(" ");
                int[] jcdIntArray = new int[numbers.size()];
                int myInt = 0;
                for(Integer jcdInt : numbers) {
                    jcdIntArray[myInt++] = jcdInt;
                                              }
                for(Integer myInteger : jcdIntArray) {
                    char ch = (char)myInteger;
                    System.out.print(ch);
                                                     }
        } 
           catch (FileNotFoundException e)  
        { 
            e.printStackTrace(); 
        } 
         
    } 
}

编译错误分析及修正方案

  1. 冗余导入:import java.lang.reflect.Array;、import java.util.function.BinaryOperator;未被使用,且import java.util.*;与前面的ArrayList、Scanner导入重复,需删除冗余导入语句。
  2. 增强for循环类型不匹配:遍历int[]数组时使用Integer类型变量,会导致编译错误,应改为int类型。
  3. 分隔符鲁棒性问题:原分隔符", "无法匹配文件中可能存在的换行或多余空格,建议改为",\\s*"(匹配逗号加任意数量的空白字符),同时对读取的字符串做trim()处理,避免空字符串解析报错。

修正后的代码

import java.io.File; 
import java.io.FileNotFoundException;  
import java.util.ArrayList; 
import java.util.Scanner; 

public class Read_int_array_a  
{ 
    private final static String FILE_NAME = "D:/sumit/sample-project/final/file3.txt"; 
    private final static String DELIMITER = ",\\s*"; // 匹配逗号加任意空白字符
    public static void main(String[] args) 
    { 
        File inputFile = new File(FILE_NAME); 
        ArrayList<Integer> numbers = new ArrayList<>(); // 使用菱形语法简化声明
        try  
        { 
            Scanner scanner = new Scanner(inputFile); 
            scanner.useDelimiter(DELIMITER); 
            while(scanner.hasNext()) 
            { 
                String value = scanner.next().trim(); // 去除字符串前后空白
                if (!value.isEmpty()) { // 跳过空字符串,避免解析报错
                    Integer num = Integer.parseInt(value); 
                    numbers.add(num);
                }
            } 
            scanner.close();
            
            // 转换为int数组并输出对应字符
            int[] jcdIntArray = new int[numbers.size()];
            int index = 0;
            for(Integer num : numbers) {
                jcdIntArray[index++] = num;
            }
            for(int ascii : jcdIntArray) {
                char ch = (char) ascii;
                System.out.print(ch);
            }
        } 
        catch (FileNotFoundException e)  
        { 
            e.printStackTrace(); 
        } 
        catch (NumberFormatException e) {
            // 新增异常处理:捕获非整数格式的内容
            System.err.println("文件中存在非整数格式的内容");
            e.printStackTrace();
        }
    } 
}

测试文件内容(file3.txt)

70, 73, 78, 65, 78, 67, 73, 65, 76, 32, 80, 82, 79, 68, 85, 67, 84, 83, 32, 68, 73, 83, 84, 82, 73, 66, 85, 84, 73, 79, 78, 32, 76, 73, 77, 73, 84, 69, 68

运行修正后的代码,会输出转换后的字符串:FINANCIAL PRODUCTS DISTRIBUTION LIMITED

内容的提问来源于stack exchange,提问作者user7616778

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 01:04:54