簡體   English   中英

將整數文件讀入數組和冒泡排序時遇到問題

[英]Running into issue with reading integer file into an array and bubble sort

我在編譯之前沒有收到任何錯誤,一切都在運行,但我的輸出應該是文本文件中的幾個整數,而不是一堆隨機字符和數字。 我不確定出了什么問題,但我的任務是將整數文件讀取到數組中,然后使用冒泡排序方法進行排序。 輸出結果為:Sorted Array order: [I@42a57993Sorted Array order: [I@42a57993Sorted Array order: [I@42a57993. 就我而言,任何幫助都是很好的幫助。 謝謝。

import java.io.File;
import java.io.FileNotFoundException;

public class Array {

    public static void main(String[] args) {
        
        int[] arr = new int[10];
        int i = 0;
    
        
    
        Scanner input = new Scanner(System.in);
        
        
        try {
        
        System.out.print("Enter the file name:");
        String fileName = input.next();
        Scanner readFile = new Scanner(new File(fileName));
        while (readFile.hasNext()) {
            try {
                arr[i] = readFile.nextInt();
                i++;
            } catch (InputMismatchException e) {
                readFile.next();
            }
            
        bubbleSort(arr);
        System.out.print("Sorted Array order: " + arr);
            input.close();
                
        }}catch(FileNotFoundException e) {
            System.out.println("Error occured");
            e.printStackTrace();
        }
        
        }

    

static void bubbleSort(int[] arr) {  
    int n = arr.length;  
    int temp = 0;  
     for(int i=0; i < n; i++){  
             for(int j=1; j < (n-i); j++){  
                      if(arr[j-1] > arr[j]){   
                             temp = arr[j-1];  
                             arr[j-1] = arr[j];  
                             arr[j] = temp;  
                     }  
                      
             }  
     }  

}}

稍微修改了一下,似乎得到了正確的輸出

package com.kanhaiyakumawat.stackoverflow;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Array {

    public static void main(String[] args) {

        int[] arr = new int[10];
        int i = 0;


        Scanner input = new Scanner(System.in);


        try {

            System.out.print("Enter the file name:");
            String fileName = input.next();
            Scanner readFile = new Scanner(new File(fileName));
            while(readFile.hasNext()) {
                try {
                    arr[i] = readFile.nextInt();
                    i++;
                } catch(InputMismatchException e) {
                    readFile.next();
                }

            }
            bubbleSort(arr);
            System.out.print("Sorted Array order: " + Arrays.toString(arr));
        } catch(FileNotFoundException e) {
            System.out.println("Error occured");
            e.printStackTrace();
        }finally {
            input.close();
        }

    }


    static void bubbleSort(int[] arr) {
        int n = arr.length;
        int temp = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 1; j < (n - i); j++) {
                if(arr[j - 1] > arr[j]) {
                    temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                }

            }
        }

    }
}

輸出是:

Enter the file name:/Users/kkumawat/githome/personal/practice/src/main/java/com/kanhaiyakumawat/stackoverflow/myfile.txt
Sorted Array order: [0, 0, 0, 0, 0, 0, 0, 0, 5, 10]
Process finished with exit code 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM