Мне нужно было создать базовую программу для ввода текста в документ .txt, поэтому я создал ее, но не понимаю, почему первый вопрос пропускается при первом запуске программы.
Этого не произойдет, если первый вопрос, который задает цикл, отсутствует. Также, как мне остановить перезапись того, что уже есть в текстовом документе, когда все, что я хочу, это добавить к нему.
Пока что последний метод, похоже, работает далеко, но я подумал, что все равно включу его.
package productfile;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Mp
*/
public class Products {
public void inputDetails(){
int i=0;
int count=0;
String name;
String description;
String price;
Scanner sc = new Scanner(System.in);
System.out.println("How many products would you like to enter?");
count = sc.nextInt();
do{
try{
FileWriter fw = new FileWriter("c:/Users/Mp/test.txt");
PrintWriter pw = new PrintWriter (fw);
System.out.println("Please enter the product name.");
name = sc.nextLine();
pw.println("Product name: " + name );
System.out.println("Please enter the product description.");
description = sc.nextLine();
pw.println("Product description: " + description );
System.out.println("Please enter the product price.");
price = sc.nextLine();
pw.println("Product price: " + price );
pw.flush();
pw.close();
i++;
}catch (IOException e){
System.err.println("We have had an input/output error:");
System.err.println(e.getMessage());
}
} while (i<count);
}
public void display(){
String textLine;
try{
FileReader fr = new FileReader("c:/Users/Mp/test.txt");
BufferedReader br = new BufferedReader(fr);
do{
textLine = br.readLine();
if (textLine == null){
return;
} else {
System.out.println(textLine);
}
} while (textLine != null);
}catch(IOException e){
System.err.println("We have had an input/output error:");
System.err.println(e.getMessage());
}
}
}