2016-12-07 1 views
0

として保存するには、昇順または降順に応じてそれらを並べ替えますユーザーの選択に応じて、テキストファイルとしてエクスポートすることができます。最初のメソッドでjavaを使ってファイルを読み込もうとする前に、コードが配列の中にコードの中に含まれていると、コードは正常に機能しました。その後、ユーザーの入力と一致するものは何も印刷されませんでした。ここでのJava - .txtファイルを読むと、私は整数のリストがあるテキストファイルを読み込むことになって、次のコード -</p> <p>プログラムで支援が必要なアレイ

は、私が現在持っているものです。

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

public class BubbleSorting { 

public static int[] readFiles(String file) throws FileNotFoundException{ 
     File f= new File("input.txt"); 
     Scanner x= new Scanner(f); 

     int [] intArray= new int[100]; 

     Scanner s1= new Scanner(f); 

     for (int i=0; i< intArray.length; i++) 
      intArray[i] = s1.nextInt(); 

     return intArray; 
    } 

public static void main (String[] args) throws FileNotFoundException { 

int[] intArray= readFiles("input.txt"); 

System.out.println("Select from the following options:\n (A) Sort integers by ascending order \n (B) Sort by descending order " 
     + "\n (C) Both. \n Please input your selection below."); 

Scanner a = new Scanner(System.in); 
String choice = a.nextLine(); 

if (choice.equals("A") || choice.equals("a")) { 

    bubbleSortInASC(intArray); 

    System.out.println("You have chosen to sort in ascending order:"); 
    //print array after sorting using bubble sort algorithm 

    for(int i=0; i < intArray.length; i++){ 
      System.out.println(intArray[i]);}} 

else if (choice.equals("B") || choice.equals("b")) { 

//sort an array in descending order using bubble sort algorithm 
bubbleSortDSC(intArray); 

System.out.println("You have chosen to sort in descending order:"); 
//print array after sorting using bubble sort algorithm 

for(int i=0; i < intArray.length; i++){ 
     System.out.println(intArray[i] + " "); 
}} 

else if (choice.equals("C") || choice.equals("c")) { 

    System.out.println("You have chosen to sort in both ascending and descending order."); 

    bubbleSortInASC(intArray); 
    System.out.println("In ascending order: "); 
    for(int i=0; i < intArray.length; i++){ 
     System.out.println(intArray[i]+ " "); } 

    bubbleSortDSC(intArray); 
    System.out.println("\nIn descending order: "); 
    for(int i=0; i < intArray.length; i++){ 
     System.out.println(intArray[i]+ " "); } 
} 
else { 
    System.out.println("That was not one of the options provided. Please try again."); 
} 

    } 

public static void bubbleSortDSC(int intArray[]) { 

    int n = intArray.length; 
    int temp = 0; 

    for(int i=0; i < n; i++){ 
      for(int j=1; j < (n-i); j++){ 

        if(intArray[j-1] < intArray[j]){ 
          //swap the elements! 
          temp = intArray[j-1]; 
          intArray[j-1] = intArray[j]; 
          intArray[j] = temp;}  
      }} 
    } 

public static void bubbleSortInASC(int numbers[]) 
    { 
     int temp; 

     for(int i = 0; i < numbers.length; i++) 
     { 
      for(int j = 1; j < (numbers.length -i); j++) 
      { 
       //if numbers[j-1] > numbers[j], swap the elements 
       if(numbers[j-1] > numbers[j]) 
       { 
        temp = numbers[j-1]; 
        numbers[j-1]=numbers[j]; 
        numbers[j]=temp; 
       } 
      } 
     }} 
} 

すべてのヘルプははるかに高く評価されるだろう。

答えて

0

public static int[] readFiles(String file) throws FileNotFoundException{ 
    File f= new File("input.txt"); 
    //Scanner x= new Scanner(f); //you havent used it 

    int [] intArray= new int[10]; 
    Scanner s1= new Scanner(f); 
    for (int i=0; i< intArray.length; i++) 
     intArray[i] = s1.nextInt(); 
    s1.close();//should close inputstream after use it 
    return intArray; 
} 

readFiles methodを交換し、input.txtは、現在のディレクトリの下にあることを確認してください。

関連する問題