2017-12-11 13 views
-3

私は書籍情報を持つファイルを持っています。私はファイルを配列に読み込んでメニューを提供する必要があります。コードを実行してオプション1を選択すると、テキストファイルのデータが削除されます。なぜこうなった?オプション1を選択したとき、私は、書籍の数を本を選んだことができるように、ユーザーが必要とし、それらに価格を表示しテキストファイルを配列に読み込んで別のタスクを実行する

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.util.*; 

public class BookProgram { 

    static Scanner input = new Scanner(System.in); 

    public static void main(String[] args) throws FileNotFoundException { 
     Scanner fileInput = new Scanner(new File("src/BookData.txt")); 
     PrintWriter output = new PrintWriter("src/BookData.txt"); 
     String newID = ""; 
     String title = ""; 
     String newPrice = ""; 

     ArrayList<String> prices = new ArrayList<>(); 
     ArrayList<String> IDs = new ArrayList<>(); 

     while (fileInput.hasNextLine()) { 
      prices.add(fileInput.next()); 
      String line = fileInput.nextLine(); 
      String[] price = line.split(" "); 
      prices.add(price[0]); 
      IDs.add(price[1]); 
     } 

     // display menu 
     display(); 
     int userChoice = input.nextInt(); 
     while (fileInput.hasNextLine()) { 
      System.out.println(fileInput.nextLine()); 
     } 

     if (userChoice == 1) { 
      System.out.println("Enter the Book ID that you wish to buy: "); 
      int bookID = input.nextInt(); 
      System.out.println("How many of those books do you want to buy?: "); 
      int numOfBooks = input.nextInt(); 
      System.out.println("Price: " + numOfBooks); 
     } 

     if (userChoice == 2) { 
      addBook(); 
      output.println(newID + " " + title + " " + newPrice); // save to the 
                    // file 
     } 
    } 

    // method to display menu 
    public static void display() { 
     System.out.println("1) Purchase Menu"); 
     System.out.println("2) Add Book to Inventory"); 
     System.out.println("3) Remove Book from Inventory"); 
     System.out.println("4) Quit"); 
     System.out.println("Enter your choice: "); 
    } 

    // method to display all the info 
    public static void purchaseMenu() { 

    } 

    // method to add to the text file 
    public static void addBook() { 
     System.out.println("Enter the ID: "); 
     String newID = input.nextLine(); 
     System.out.println("Enter the title: "); 
     String title = input.nextLine(); 
     System.out.println("Enter the price: "); 
     String newPrice = input.nextLine(); 
    } 
} 
+1

何が起こっているかを見つけるために、デバッガを使用してください。ここで答えを待っているよりはるかに早い。 –

+0

どうすればいいですか?私はデバッガを一度も使用していません – Boundz

+0

eclipseでJava用デバッガを使用する方法://www.vogella.com/tutorials/EclipseDebugging/article.html –

答えて

0

//あなたは、配列の中のテキストを読むためにこのコードを使用することができます。次に配列要素の上にあなたが望むように操作を行います。

BufferedReader abc = new BufferedReader(new FileReader(yourfilehere)); 
List<String> lines = new ArrayList<String>(); 

while((String line = abc.readLine()) != null) { 
    lines.add(line); 
    System.out.println(data); 
} 
abc.close(); 

//文字列に変換する

String[] data = lines.toArray(new String[]{}); 
+0

私はスキャナに精通しています。バッファリングされたリーダーの代わりにスキャナーを使用できますか? – Boundz

+0

はいこれも使用できます...正しい答えが正しいことを確認してください。 – SelakaN

関連する問題