【问题标题】:Running into issue with reading integer file into an array and bubble sort将整数文件读入数组和冒泡排序时遇到问题
【发布时间】:2020-09-10 00:09:28
【问题描述】:

在编译之前我没有收到任何错误,一切都运行了,但我的输出应该是文本文件中的几个整数,而不是一堆随机字符和数字。我不确定出了什么问题,但我的任务是将整数文件读取到数组中,然后使用bubblesort方法进行排序。输出结果如下: 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;  
                     }  
                      
             }  
     }  

}}

【问题讨论】:

    标签: java arrays file bubble-sort


    【解决方案1】:

    稍作修改,似乎得到了正确的输出

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 2013-09-28
      • 2013-10-05
      相关资源
      最近更新 更多