英文:
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;
> }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论