在Java中填充数组时出现的问题。

huangapple go评论72阅读模式
英文:

Issues filling an array in Java

问题

以下是您要翻译的代码部分:

private static int read(String inFileName, int[] list) {
    int size = 0;
    int i = 0;

    Scanner inFile = null;
    int val = 0;

    int trash = 0;
    boolean ID = false;
    int Unique = 0;
    int dupe = 0;
    int lineCount = 0;

    try { 
        inFile = new Scanner(new File(inFileName));
        while (inFile.hasNext()){
            lineCount++;
            try{
                val = inFile.nextInt();
                if(val < 0) { 
                    trash++;
                    continue; 
                }

                for(i = 0; i< size ;i++){ 
                    if(list[i] == val){
                        ID = true;
                        dupe++;
           
                    }

                }
                if (ID == false && size<list.length){ 
                    list[size] = val;
                    size++;
                    Unique++;
                }

            }
            catch (InputMismatchException e){
                trash++;
                inFile.nextLine();
            }

        }
        System.out.println("lines: " + lineCount + ", unique: " + Unique +  ", dupes: " + dupe + ", trash: " + trash);
    }
    catch (FileNotFoundException e) {
        System.out.println("Error in opening " + inFileName);
    }


    return size;
}

希望这对您有所帮助。如果您需要进一步的信息或翻译,请随时提问。

英文:

I am trying to fill an array with the unique values from another file. This file has about 20 integers total, 10 are unique and 10 are copies. The user inputs the size of the array and name of the file within the main.

I think the trouble is coming from this while loop that is meant to fill the array, sorting out the duplicates.

At the moment, it only successfully sorts 11 of the 20 integers in the file.

private static int read(String inFileName, int[] list) {
int size = 0;
int i= 0;
Scanner inFile = null;
int val = 0;
int trash = 0;
boolean ID = false;
int Unique = 0;
int dupe = 0;
int lineCount = 0;
try { 
inFile = new Scanner(new File(inFileName));
while (inFile.hasNext()){
lineCount++;
try{
val = inFile.nextInt();
if(val < 0) { 
trash++;
continue; 
}
for(i = 0; i< size ;i++){ 
if(list[i] == val){
ID = true;
dupe++;
}
}
if (ID == false && size<list.length){ 
list[size] = val;
size++;
Unique++;
}
}
catch (InputMismatchException e){
trash++;
inFile.nextLine();
}
}
System.out.println("lines: " + lineCount + ", unique: " + Unique +  ", dupes: " + dupe + ", trash: " + trash);
}
catch (FileNotFoundException e) {
System.out.println("Error in opening " + inFileName);
}
return size;
}

So far, I have tried adding different if-statements to try and specify what is unique and what is a copy. This would typically help in filling the array with the correct number of integers, but not the unique ones. It seems to be skipping quite a few unique integers and filling the array with copies. I am pretty stuck, and any suggestions whatsoever would be appreciated.

答案1

得分: 0

我已经完成了代码的翻译,以下是翻译好的部分:

// 评论了你的代码后,你进行了更改,然后应该可以工作。

private static int read(String inFileName, int[] list) {
    int size = 0;
    int i = 0;

    Scanner inFile = null;
    int val = 0;

    int trash = 0;
    // boolean ID = false; 移除
    int Unique = 0;
    int dupe = 0;
    int lineCount = 0;

    try {
        inFile = new Scanner(new File(inFileName));
        while (inFile.hasNext()) {
            boolean ID = false; // 在这里添加
            lineCount++;
            try {
                val = inFile.nextInt();
                if (val < 0) {
                    trash++;
                    continue;
                }

                for (i = 0; i < size; i++) {
                    if (list[i] == val) {
                        ID = true;
                        dupe++;
                    }
                }
                if (ID == false && size < list.length) {
                    list[size] = val;
                    size++;
                    Unique++;
                }
            } catch (InputMismatchException e) {
                trash++;
                inFile.nextLine();
            }
        }
        System.out.println("lines: " + lineCount + ", unique: " + Unique + ", dupes: " + dupe + ", trash: " + trash);
    } catch (FileNotFoundException e) {
        System.out.println("Error in opening " + inFileName);
    }

    return size;
}
英文:

Commented on your code and you changed it, then should work.

> private static int read(String inFileName, int[] list) {
> int size = 0;
> int i= 0;
>
> Scanner inFile = null;
> int val = 0;
>
> int trash = 0;
> // boolean ID = false; remove
> int Unique = 0;
> int dupe = 0;
> int lineCount = 0;
>
> try {
> inFile = new Scanner(new File(inFileName));
> while (inFile.hasNext()){
> boolean ID = false; // Add here
> lineCount++;
> try{
> val = inFile.nextInt();
> if(val < 0) {
> trash++;
> continue;
> }
>
> for(i = 0; i< size ;i++){
> if(list[i] == val){
> ID = true;
> dupe++;
>
> }
>
> }
> if (ID == false && size<list.length){
> list[size] = val;
> size++;
> Unique++;
> }
>
> }
> catch (InputMismatchException e){
> trash++;
> inFile.nextLine();
> }
>
> }
> System.out.println("lines: " + lineCount + ", unique: " + Unique + ", dupes: " + dupe + ", trash: " + trash);
> }
> catch (FileNotFoundException e) {
> System.out.println("Error in opening " + inFileName);
> }
>
>
> return size;
> }

huangapple
  • 本文由 发表于 2023年2月26日 23:02:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定