When I try to read or write to an external file in java. The program compiles to me but when the code is executed and I write the name of the file that I want that it reads, the program stays run but it does not show anything to me in screen.
import java.util.Scanner;
import javax.swing.*;
import java.io.*;
import java.io.FileReader;
import java.io.PrintWriter;
// Declared class ProjectPorStr
public class ProjectPorStr {
// Declared Object Scanner;
static Scanner input = new Scanner(System.in);
// Declaring the metohod menu
// And declaring menu items.
public static void menu (String productNamesList[]) {
String test = "";
do {
test = JOptionPane.showInputDialog(null, "a. Add items\n b. Show items\n c. Remove items\n x. Exit");
if (test.equals ("a")) {
JOptionPane.showMessageDialog(null, "Added items");
}
else if (test.equals ("b")) {
JOptionPane.showMessageDialog(null, "Showing items");
JOptionPane.showMessageDialog(null, productNamesList);
}
else if (test.equals ("c")) {
JOptionPane.showMessageDialog(null, "Delet the items");
}
else if (test.equals ("x")) {
JOptionPane.showMessageDialog(null, "Thank you for user own system. See it soon!");
System.exit(0);
}
else {
JOptionPane.showMessageDialog(null, "Please try agen.");
}
}while(!test.equals("d"));
}
// Declaring the metohd call name
// And declaring multiple variable
// Reader, fileInput, fileOutput and the array name productNamesList has 50 items
// Adding integer variable matchesCount, date, upcNumber, experDate, quantitySold, itemsPrices
// Collect the data when the user enters the product number
public static void name () {
Scanner keyboard = new Scanner(System.in);
FileReader reader;
Scanner fileInput;
PrintWriter fileOutput;
String productNamesList [] = new String [30],
product, strLine, namesOfInputFile, namesOfOuputFile;
int matchesCount = 0, date, upcNumber, experDate, quantitySold, itemsPrices;
JOptionPane.showInputDialog(null, "Name of the input file and add .txt: ");
namesOfInputFile = keyboard.next();
JOptionPane.showInputDialog(null, "Name of the output file and add .txt: ");
namesOfOuputFile = keyboard.next();
// Input try
try {
reader = new FileReader(namesOfInputFile);
fileInput = new Scanner (reader);
while (fileInput.hasNextLine()) {
strLine = fileInput.nextLine();
product = strLine;
upcNumber = Integer.valueOf(strLine);
if (productNamesList.equals ("b")) {
productNamesList[matchesCount] = product;
matchesCount += 1;
}
}
fileInput.close();
}catch(IOException error) {
System.out.print("File error " + error);
}
// Output try
try {
fileOutput = new PrintWriter(namesOfOuputFile);
for (int i = matchesCount - 1; i >= 0; i --) {
JOptionPane.showMessageDialog(null, "Name Product: " + productNamesList[i]);
fileOutput.print(productNamesList[i]);
}
fileOutput.close();
}catch(IOException error) {
System.out.print("File error " + error);
}
}
// The main function
// Calling the matohd
public static void main(String[] args) {
ProjectPorStr myMenu = new ProjectPorStr();
String[] menuList = {"Meet", "Fish", "Cake"};
name();
}
}
It would be a great help for me.