gpt4 book ai didi

Java读取.txt文件到数组

转载 作者:行者123 更新时间:2023-12-01 12:15:21 25 4
gpt4 key购买 nike

我一整天都在尝试进行这项练习,但没有任何运气。预先感谢您的帮助。

问题来了

The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 50, so we will be able to store integers up to 50 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the “front” of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we’re likely to add them like this:

38423

27

To simulate this right-shifting of values, we will store each value as a sequence of exactly 50 digits, but we’ll allow the number to have leading 0’s. For example, the problem above is converted into:

0000000000000000000038423

0000000000000000000000027

Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these.

The data for your program will be stored in a file called sum.txt. Each line of the input file will have a different addition problem for you to solve. Each line will have one or more integers to be added together. Take a look at the input file at the end of this write-up and the output you are supposed to produce. Notice that you produce a line of output for each input line showing the addition problem you are solving and its answer. Your output should also indicate at the end how many lines of input were processed. You must exactly reproduce this output.

You should use the techniques described in chapter 6 to open a file, to read it line by line, and to process the contents of each line. In reading these numbers, you won’t be able to read them as ints or longs because many of them are too large to be stored in an int or long. So you’ll have to read them as String values using calls on the method next(). Your first task, then, will be to convert a String of digits into an array of 50 digits. As described above, you’ll want to shift the number to the right and include leading 0’s in front. The String method charAt and the method Character.getNumericValue will be helpful for solving this part of the problem.

You are to add up each line of numbers, which means that you’ll have to write some code that allows you to add together two of these numbers or to add one of them to another. This is something you learned in Elementary School to add starting from the right, keeping track of whether there is a digit to carry from one column to the next. Your challenge here is to take a process that you are familiar with and to write code that performs the corresponding task.

Your program also must write out these numbers. In doing so, it should not print any leading 0’s. Even though it is convenient to store the number internally with leading 0’s, a person reading your output would rather see these numbers without any leading 0’s.

You can assume that the input file has numbers that have 50 or fewer digits and that the answer is always 50 digits or fewer. Notice, however, that you have to deal with the possibility that an individual number might be 0 or the answer might be 0. There will be no negative integers in the input file.

You should solve this problem using arrays that are exactly 50 digits long. Certain bugs can be solved by stretching the array to something like 51 digits, but it shouldn’t be necessary to do that and you would lose style points if your arrays require more than 50 digits.

The choice of 50 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits.

Consider the input file as an example of the kind of problems your program must solve. We might use a more complex input file for actual grading.

The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using BigInteger or BigDecimal. You must solve it using arrays of digits.

Your program should be stored in a file called Sum.java.

输入文件sum.txt

82384
204 435
22 31 12
999 483
28350 28345 39823 95689 234856 3482 55328 934803
7849323789 22398496 8940 32489 859320
729348690234239 542890432323 534322343298
3948692348692348693486235 5834938349234856234863423
999999999999999999999999 432432 58903 34
82934 49802390432 8554389 4789432789 0 48372934287
0
0 0 0
7482343 0 4879023 0 8943242
3333333333 4723 3333333333 6642 3333333333

应该产生的输出

82384 = 82384
204 + 435 = 639
22 + 31 + 12 = 65
999 + 483 = 1482
28350 + 28345 + 39823 + 95689 + 234856 + 3482 + 55328 + 934803 = 1420676
7849323789 + 22398496 + 8940 + 32489 + 859320 = 7872623034
729348690234239 + 542890432323 + 534322343298 = 730425903009860
3948692348692348693486235 + 5834938349234856234863423 = 9783630697927204928349658
999999999999999999999999 + 432432 + 58903 + 34 = 1000000000000000000491368
82934 + 49802390432 + 8554389 + 4789432789 + 0 + 48372934287 = 102973394831
0 = 0
0 + 0 + 0 = 0
7482343 + 0 + 4879023 + 0 + 8943242 = 21304608
3333333333 + 4723 + 3333333333 + 6642 + 3333333333 = 10000011364

总行数 = 14

到目前为止我的代码

public class Sum {

public static void main(String args[]) throws FileNotFoundException{
File file = new File("sum.txt");
Scanner scanner = new Scanner(file);
String[] myInts = new String[50];
int mySpot = 0;
while(scanner.hasNext()){
myInts[mySpot] = scanner.next();
mySpot++;
}
for(int i = 0; i < myInts.length; i++){
}
System.out.println(Character.getNumericValue(myInts[0]));
System.out.println(Arrays.toString(myInts));
}
}

最佳答案

当所有其他方法都失败时,请阅读说明:

“您要实现的方法是将每个整数存储在一个数字数组中,每个数组元素一位数字。我们将使用长度为 50 的数组,因此我们将能够存储最多 50 位数字的整数”。

告诉我这一行:

String[] myInts = new String[50];

有一些重大问题。

提示 1:当它是 String 对象数组时,不要将其称为 myInts。事情已经够难了。

提示 2:了解 new String[50] 不会为您提供大小为 50 个字符的字符串。它将为您提供空间来存储对 50 个字符串对象的引用。

提示 3:了解输入的每一行都可以单独求解,因此无需记住之前已求解的行中的任何内容。

提示 4:一次将一行读入 String line;

提示5:读完一行后,分两部分解决显示问题:=的左侧和右侧。

提示6:左侧:显示空格的行,替换为空格+空格。 line.replace("","+ ");

提示 7:右侧:使用 line.split("") 在空格上分割行,循环分割的字符串数组,每个字符串都是您将转换为 int 的内容数组。

提示 8:“将数字字符串转换为 50 位数字的数组”<- 如果您编写一个执行此操作的方法,生活会更容易。拿一根绳子。返回一个 int[]。 private int[] makeIntArray(String num) 请注意此处的“右移/前导零”问题。

提示 9:int 和 long 不够大,无法容纳较大的数字,因此在转换为 int[] 之前,请将数字字符串分解为数字字符串。

提示 10:阅读 Splitting words into letters in Java

提示 11:阅读 Split string into array of character strings

提示 12:一旦有了单个字符,您可以使用 Integer.parseInt(singleCharString[index--])(如果您将其分解为字符串数组)或 Character.digit( chr[index--], 10); 如果将其分解为字符数组。

提示 13:“编写一些代码,允许您将其中两个数字相加或将其中一个数字与另一个数字相加。”仔细阅读它,它告诉您确实需要声明两个变量。 int[] sum = new sum[SIZE];int[] next = new next[SIZE]; 其中大小为 private final static int SIZE = 50 ;

提示 14:将两个 int[] 数字相加来生成一个新的 int[] 将是创建方法的另一个好时机。 int[] sum(int[] op1, int[] op2)

提示 15:由于我们所有的 int[] 都已右移,并且长度始终为 50,因此以 i 为 49 开始循环并倒计时。 result[i-1] = (op1[i] + op2[i] + 进位) % 10;carry = (op1[i] + op2[i] + 进位)/10 会派上用场的。确保在 1 处停止循环,否则 [i-1] 将使索引超出范围。

提示 16:测试、测试、再测试。进行一些小的更改然后进行测试。小改动,测试。不要只是打字和祈祷。如果您愿意,可以使用调试器,但我个人更喜欢检查这样的值 System.out.println("line: "+ line);//TODO 删除调试代码

关于Java读取.txt文件到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27052681/

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