简体   繁体   English

为什么我的琴弦没有变化?

[英]Why doesn't my string change?

public static void read(String a[], double b[], String c) throws IOException {
    Scanner in = new Scanner(new File("data.txt"));
    while (in.hasNext()) {
        int i = 0;
        String id = in.next();
        String name = in.next();
        String lastname = in.next();
        double grade = in.nextDouble();
        if (name.substring(0, 2).equalsIgnoreCase(c)) {
            a[i] = id + "\t" + name + "\t" + lastname + "\t" + grade;
            b[i] = grade;
        }
        i++;
    }
}

When I use this method with 当我使用这种方法

String men[] = new String[501];
double menGrade[] = new double[501];
read(men, menGrade, "MR");

My men[0] is assigned a String but men[1] to men [500] are all null ... 我的men [0]被分配了一个String,但是men [1]到men [500]都为null ...

You need to declare your variable i outside of the while loop to keep it incremented. 您需要在while循环之外声明变量i ,以使其保持递增状态。

Right now you are 现在你是

  • declaring it with value 0 用值0声明
  • assign the values to the 1st array position 将值分配给第一个数组位置
  • increment i, and then 递增i,然后
  • declare it again with value 0 at the next loop iteration. 在下一次循环迭代时再次声明为0。

SO, just change your lines: 所以,只需更改您的行:

while (in.hasNext()) {
    int i = 0;

to

int i = 0;
while (in.hasNext()) {

Your code has also other issues which you should adress in some way. 您的代码还有其他问题,您应该以某种方式解决。 I do not know why you initialize your array with a fixed size of 500 and also check some conditions before you add your men and grades to those Arrays. 我不知道为什么要使用固定大小500初始化数组,并在将人员和等级添加到这些数组之前检查一些条件。 This will however lead to a few problems if you are not careful. 但是,如果您不小心,将导致一些问题。

Right now you would have holes in your array whenever the if condition does not evaluate to true. 现在,只要if条件不为true,您的数组中就会有孔。

Also your program would crash if there is more than 500 entries in your file. 如果文件中有500个以上的条目,则程序也会崩溃。

A rather good solution when dealing with dynamic data structures (so, when you do not know beforehand how many records you will have exactly), is to use a dynamic data structure. 处理动态数据结构(因此,当您事先不知道确切有多少记录时)时,一个很好的解决方案是使用动态数据结构。

In java you can have a look at java.util.List interface and probably java.util.ArrayList as a good implementation. 在Java中,您可以查看java.util.List接口,并可以将java.util.ArrayList作为一个很好的实现。 Here is also the java doc of that class: Java Doc 这也是该类的Java文档Java Doc

Here you find more on the collections api which are a good thing for dynamic data structures: Collections - List tutorial 在这里,您可以找到有关collections api的更多信息,这对于动态数据结构而言是一件好事: Collections-List tutorial

while (in.hasNext()) {
  int i = 0;
  ...

This will RESET i each time you start the while loop and you always overwrite a[0] and b[0]. 每次启动while循环时,它将重置i,并且始终覆盖a [0]和b [0]。

swap these two lines! 交换这两行! (so the int i = 0; comes before the loop: (因此int i = 0;位于循环之前:

int i = 0;
while (in.hasNext()) {
  ...

You should increment i in your if statement and not always like you do now. 您应该在if语句中增加i ,但并不总是像现在这样。 You don't want holes in your men array. 您不希望在男士组合中出现漏洞。

This simply means, either your loop is executing only once. 这仅表示您的循环仅执行一次。 Or, if block in loop is exceuting only once. 或者,如果循环中的块仅执行一次。

Dependecy is on the content of file you are importing and your if condition. 取决于您要导入的文件的内容以及if条件。

I'm pretty sure that something is wrong with your "data.txt" that caused the while loop to execute only once. 我很确定您的“ data.txt”出了问题,导致while循环仅执行一次。 Otherwise, I don't see any mistake in the code. 否则,我不会在代码中看到任何错误。

Why don't you check the value of i during the execution of the program? 为什么在执行程序期间不检查i的值?

如果您data.txt文件中包含One单一的线,则对应的while将运行一次填充数组的第一个元素,即men在你的情况

The reason is this: 原因是这样的:

while (in.hasNext()) {
    int i = 0;
    ...
    i++;
}

you are destroying and creating i variable each time loop is executed effectively reseting it to 0 each time. 您在每次执行循环时都销毁并创建i变量,每次有效地将其重置为0。 Asides from notes from other answers you can simply move i outside the loop: 除了其他答案的注释之外,您还可以将i移出循环:

int i = 0;
while (in.hasNext()) {
    ...
    i++;
}

Now I can't run in myself , but I see 现在我无法自己奔跑 ,但我明白了

while (in.hasNext()) {
        int i = 0;
//other
  a[i] = id + "\t" + name + "\t" + lastname + "\t" + grade;
            b[i] = grade;
        }
        i++;

If you use a counter i over an array/Collection, generally you have to give a greater scope to counter. 如果在数组/集合上使用计数器 i ,通常必须给计数器更大的范围。 if the counter is inside the while, at every iteration you recreate the counter and you point always at the same element of array 如果计数器在while内,则在每次迭代时都会重新创建计数器,并且始终指向数组的同一元素

The/one solution can be: 一种解决方案可以是:

int i=0;
while (in.hasNext()) {
//etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM