Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Reading the data from DataInputStream . help :(

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Reading the data from DataInputStream . help :(

    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 .
    Last edited by cuihanluan; October 7th, 2012 at 05:57 AM. Reason: not direct to the point


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading the data from DataInputStream . help :(

    it only shows null
    What shows null?
    If something shows null and you think it should not be null, go to the place where you thought you assigned a non-null value and add a println (or ten) and see what is going on.
    If that does not help you figure it out try to be more descriptive in stating your problem in terms of what values you are getting where in the code, and what values you expected at that point in the code. Use comments in the code to mark areas of interest you think are related to the problem.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading the data from DataInputStream . help :(

    the data inputted through the dataoutputstream, wont show. it only shows null.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Reading the data from DataInputStream . help :(

    Is there a NullPointerException being thrown? If so, what line throws it? More information would help us quite a bit.

Similar Threads

  1. reading unformatted data from socket
    By amjhanwar in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 7th, 2012, 02:03 PM
  2. Reading File and Outputting Data
    By ChrisMessersmith in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 13th, 2011, 01:40 PM
  3. Help reading in data
    By toolman87 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 30th, 2011, 06:41 PM
  4. Error by Using DataInputStream
    By pokuri in forum Collections and Generics
    Replies: 2
    Last Post: January 13th, 2011, 12:03 PM
  5. Reading Chunks of Data
    By icu222much in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 22nd, 2010, 08:39 PM