Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Writing to .Txt File Logic

    Point of this program is to ask the user for 10 items they would want on an island with them, then ask for the price of said item, and write item and price to a .txt file. Right now its doing this as soon as it gets to the second item and you can not enter a second item name only a price:

    Writing to .Txt File Logic-ibdescffz6ye1p.png

    PHP Code:
    //MakeIslandList.java by Tyler 11/18/2012
    
    import java.io.*;
    import java.util.*;
    
    public class MakeIslandList
    {
    	public static void main(String[] args)  throws IOException
    	{
    	DataOutputStream ostream;
    	Scanner input = new Scanner(System.in);
    	
    	int count = 1;
    	String item;
    	double itemCost;
    	ostream = new DataOutputStream(new FileOutputStream("MakeIslandList.txt"));
    	
    	try
    	{
    		while(count != 11){
    		System.out.print("Enter your number " + count + " item you would have on an island \n");
    		item = input.nextLine();
    		System.out.print("Enter the cost of this item \n");
    		itemCost = input.nextDouble();
    		ostream.writeUTF(item);
    		ostream.writeDouble(itemCost);
    		count++;}
    	}
    	catch(IOException e)
    	{
    	System.err.println("Error writing to file");
    	}
    	finally
    	{
    	ostream.close();
    	}
    	
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Writing to .Txt File Logic

    Scanner's nextDouble() method (and a few others) doesn't consume the newline, which remains in the Scanner until the next nextLine() call.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2012, 03:01 AM
  2. Replies: 14
    Last Post: 07-15-2011, 05:22 AM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Logic Error: Not Writing To File
    By JDCAce in forum Advanced Java
    Replies: 6
    Last Post: 10-21-2008, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •