gpt4 book ai didi

java - 将字符串数组从文件转换为 int 数组,没有数字格式异常

转载 作者:行者123 更新时间:2023-11-30 06:33:01 25 4
gpt4 key购买 nike

我的任务是在java中执行合并排序功能。我已经用合并排序函数解决了这个问题。我在这个作业中遇到的问题是转换文本文件的每一行整数并对每个整数执行合并排序函数。例如:34 25 5 29 612 64 23 11 3294 12 42 23 55

第一行是文件中包含的数组数量,接下来的 3 行是我需要执行合并排序的数组。所以我需要对 4 25 5 29 6、12 64 23 11 32 和 94 12 42 23 55 等数组进行合并排序。

我试图做的是创建四个单独的字符串数组来存储每行整数,因此我使用了扫描仪中的 nextLine() 方法。我现在处理的问题是弄清楚如何将它们转换为 int 数组而不处理数字格式异常。

这是我的合并排序函数类

public class MergeSort 
{
private int[] array;
private int[] tempMergArr;
private int length;

public void sort(int inputArr[])
{
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);
}

public void doMergeSort(int lowerIndex, int higherIndex)
{

if (lowerIndex < higherIndex)
{
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the array
doMergeSort(lowerIndex, middle);
// Below step sorts the right side of the array
doMergeSort(middle + 1, higherIndex);
// Now merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}


public void mergeParts(int lowerIndex, int middle, int higherIndex)
{

for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}

}
}

这是Merge Sort的驱动类,这就是问题所在。

import java.io.*;

import java.util.Scanner;

public class MergeSortDriver
{
public static void main(String args[]) throws IOException
{
String[] array = new String[6];
String[] array1 = new String[6];
String[] array2 = new String[6];
String[] array3 = new String[6];


int i = 0;
File file = new File("input.txt");
Scanner fileReader = new Scanner(file);


array[i] = fileReader.nextLine();
array1[i] = fileReader.nextLine();
array2[i] = fileReader.nextLine();
array3[i] = fileReader.nextLine();
System.out.println(array1[i]);
System.out.println(array2[i]);
System.out.println(array3[i]);


fileReader.close();

MergeSortDriver driver = new MergeSortDriver();

for(int j = 0; j<array1.length; j++)
{
System.out.print(driver.convertToInt(array1)[i]+" ");
}



/*
int[] inputArr = {4,25,5,29,6}; // if I do like this, then the code will work, so the string arrays need to be like this in order to work.
MergeSort mms = new MergeSort();
mms.sort(inputArr);
for(int i:inputArr)
{
System.out.print(i);
System.out.print(" ");
}
*/
}
public static int[] convertToInt(String[] array)
{
int[] ints = new int[array.length];
for(int i = 0; i<array.length; i++)
{
ints[i] = Integer.parseInt(array[i]);
}
return ints;
}


}

我希望你们能找出我的驱动类源代码中的错误,如果您现在尝试运行该程序,您将得到数字格式异常。所以我的目标是将文件中的字符串数组转换为 int 数组而不生成异常。

最佳答案

对于您的基本问题来说代码太多。在 Java 8+ 中,您可以用一个或多个空格分割给定的行,然后使用StreammapToInt,例如

String line = "4 25 5 29 6";
int[] arr = Stream.of(line.split("\\s+")).mapToInt(Integer::parseInt).toArray();

在早期版本中,您可以执行类似的操作,但需要更多代码 - 例如

String line = "4 25 5 29 6";
String[] lineParts = line.split("\\s+");
int[] arr = new int[lineParts.length];
for (int i = 0; i < lineParts.length; i++) {
arr[i] = Integer.parseInt(lineParts[i]);
}

关于java - 将字符串数组从文件转换为 int 数组,没有数字格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45704617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com