import javax.swing.JOptionPane; import java.util.*; import java.io.*; import java.lang.Exception.*; public class FinalNaTlga { static Scanner input = new Scanner (System.in); static final int PASSWORD = 1234; static final int DONE = 143; static final int EOF = -1; public static void main (String [] args) throws FileNotFoundException,IOException, EOFException { DataOutputStream stream = new DataOutputStream(new FileOutputStream("Merchandise.dat")); DataInputStream stream1 = new DataInputStream ( new FileInputStream ("Merchandise.dat.tmp")); String s1 = "LBC"; String s2 = "JRS_Express"; String s3 = "MoneyGram"; String s4 = "Western_Union"; String choice, pcat = "", pname = "", mode, pay = "", pay2 = ""; int select, pass = 0,pass1, pcode = 0, q = 1, chc2 = 0, num = 0, x = 0, y = 0, z =0, xx = 0; char chc, md; double pprice; try { System.out.print(" \n++ WELCOME TO SUPER ELF MERCH ++ "); System.out.println(" \n==================================="); do { System.out.print("\nChoose user: \n\n[1] SHOP OWNER\n[2] BUYER\n>>> "); select = input.nextInt(); if ( select == 1) { System.out.print("\n ** SECURITY CHECK **" + "\nEnter security password: "); pass = input.nextInt(); if ( pass != PASSWORD) { System.out.print("\nInvalid password."); pass1 = 1; } else { System.out.print("\n=============================\nHi Shopkeeper! Good day ! ^^\n============================="); do { System.out.print("\n\nWhat would you like to do this time?" + "\n[a] ADD INVENTORY" + "\n[b] MANAGE SALES" + "\n[c] EXIT"); System.out.print("\n>>> "); input.nextLine(); choice = input.nextLine(); chc = choice.charAt(0); pass1 = 0; switch (chc) { case 'a':case'A': //in this part, data here is entered and written through the data streams System.out.print("\nINVENTORY"); do { System.out.print("\nPress Enter to continue :) "); input.nextLine(); System.out.println("\n++++++++++++++"); System.out.print("\nEnter product code or " + DONE + " to quit: "); while (!input.hasNextInt()) { System.out.println("\nInvalid data type!"); System.out.print("\nEnter product code or " + DONE + " to quit. >> "); input.next(); } pcode = input.nextInt(); while (pcode != DONE) { input.nextLine(); System.out.print("Enter category: "); pcat = input.nextLine(); System.out.print("Enter product name: "); pname = input.nextLine(); System.out.print("Enter product price: "); pprice = input.nextDouble(); stream.writeUTF(pcat); stream.writeInt(pcode); stream.writeUTF(pname); stream.writeDouble(pprice); System.out.print("\nEnter product code or " + DONE + " to quit: "); while (!input.hasNextInt()) { System.out.println("\nInvalid data type!"); } pcode = input.nextInt(); } stream.close(); q = JOptionPane.showConfirmDialog(null,"Do you want to continue?","SELECT",JOptionPane.YES_NO_OPTION); } while ( q != JOptionPane.NO_OPTION); break; case 'b':case'B': //through here the data entered in case a, should be shown here. but in my case, it only shows null. System.out.println("\nSALES\n\nCATEGORY\tPRODUCT NAME\t PRODUCT CODE\tPRICE"); System.out.println("======================================================================"); while ((pcode = stream1.readInt()) != EOF) { pcat = stream1.readUTF(); pname = stream1.readUTF(); pprice = stream1.readDouble(); stream.writeInt(pcode); stream.writeUTF(pcat); stream.writeUTF(pname); stream.writeDouble(pprice); System.out.println(pcode+ "\t\t" + pcat + "\t\t" + pname + "\t\t" + pprice); } stream1.close(); break; case 'c':case'C': break; default: System.out.print(""); } z = JOptionPane.showConfirmDialog(null,"Do you want to go back to the menu?","SELECT",JOptionPane.YES_NO_OPTION); } while ( z != JOptionPane.NO_OPTION); } File old = new File("c:\\java\\Mechandise.dat"); File temp = new File("c:\\java\\TempMerchandise.dat"); old.delete(); temp.renameTo(old); } pass1 = 0; } while (pass1 = 0; } catch (Exception e) { System.out.print(e.getMessage()); stream1.close(); stream.close(); } } }
[edited]
I dont really know what's wrong.
the code here is actually a part of a code only
The thing here is, I can actually write the data. But when it comes to reading it, it only shows null
please help me . i really dont know what to do anymore .