Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 output.println doesn't seem to save data to file
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
username123




PostPosted: Thu Nov 12, 2015 9:15 am   Post subject: output.println doesn't seem to save data to file

What the title says. When I try to display the contents of itemList.txt, nothing shows up.
Java:

PrintWriter output = new PrintWriter(itemList);
    double price = 0;
    String itemNumber;
   
    System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
    answer = input.next();
   
    while(!answer.equals("N")||!answer.equals("n"))
    {   
      if(answer.equals("Monitors")||answer.equals("monitors"))
      {
        //Displays the list of monitors
        for(int i = 0; i < 3; i++)
        {
          System.out.println(lines[i]);
        }
       
        //Buying monitors
        System.out.println("Enter the item number of an item to see its specifications and add it to your shopping cart. Press N to stop browsing.");
        itemNumber = input.next();
       
        while(!itemNumber.equals("N")||!itemNumber.equals("n"))
        {
          if(itemNumber.equals("N82E16824009659"))
          {
            System.out.println("Acer B6 Series B286HK ymjdpprz ($499.99)\n- 28\"\n- Widescreen\n- 4k resolution(3840x2160)\n- LED backlight\n- DVI, HDMI, DP, miniDP, SPK, MHL\n- USB 3.0\n- LCD\n- 2ms response");
            System.out.println("Would you like to purchase this item?");
            answer = input.next();
            if(answer.equals("yes")||answer.equals("Yes"))
            {
              System.out.println("How many of this item do you want?");
              int numItem = input.nextInt();
              price = price + 499.99 * numItem;
              output.println(numItem + "x Acer B6 Series B286HK ymjdpprz, $" + 499.99 * numItem);
            }
            System.out.println("Enter the item number of an item to see its specifications and add it to your shopping cart. Press N to stop browsing.");
            itemNumber = input.next();
          }



inventory.txt
 Description:
Required to run the java file.

Download
 Filename:  inventory.txt
 Filesize:  1.05 KB
 Downloaded:  156 Time(s)


StoreInventory.java
 Description:
The entire java file, in case anyone wants to see the whole thing.

Download
 Filename:  StoreInventory.java
 Filesize:  35.25 KB
 Downloaded:  288 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Thu Nov 12, 2015 9:28 am   Post subject: RE:output.println doesn\'t seem to save data to file

write()/println() don't actually write to the file, they just keep it in memory until you've flushed the stream which will write all that's in memory to the file. You also need to close the file once you're done i/o operations on it.

Closing the stream automatically flushes the stream.

http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#close()
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: