Link to home
Start Free TrialLog in
Avatar of Talulia
Talulia

asked on

Mulitple entries-Java

I can't get this code to allow the user to enter more than one entry after hitting yes.

How do I do this?
// Week5.java
 
import java.util.*;
import java.io.*;
 
// class Week5 definition
public class Week5
{
	// method main
	public static void main(String args[])
	{
		System.out.println("This program will create a shopping list:");
 
		// call the inputItems method
		inputItems();
		// call the outputItems method
		outputItems();
 
		System.exit(0);
	}
 
	// method inputItems
	public static void inputItems()
	{
		Scanner keyboard = new Scanner(System.in);	// create a keyboard Scanner
		PrintWriter outFile = null;					// declare a PrintWriter
		String response;
 
 
		try
		{
			outFile = new PrintWriter(new FileOutputStream("shoppinglist.txt"));
			// get first item from user
			System.out.print("Please enter your first item: ");
			outFile.write(keyboard.nextLine() + "\n");
 
 
 
			System.out.print("Do you wish to enter more Items? Yes/No: ");
			response = keyboard.nextLine();
 
			while( response.toUpperCase().equals("Yes") )
			{
				// get next item from user
				System.out.print("Enter your next item: ");
				outFile.write( keyboard.nextLine() + "\n" );
				System.out.print("Do you wish to enter more Items? Yes/No: ");
				response = keyboard.nextLine();
			}
			outFile.close();					// close the file
		}
		catch(FileNotFoundException e)		// catch exception
		{
			// display the error message and exit program
			System.err.println("File write error occurred.");
			System.exit(0);
		}
 
 
 
	}
 
	// method outputItems
	public static void outputItems()
	{
		try
		{
			// create a Scanner for the input file
			Scanner inFile = new Scanner( new File("shoppinglist.txt"));
 
			System.out.println("Your shopping list contains: ");
 
			while( inFile.hasNextLine() )
 
				System.out.println( inFile.nextLine() );
 
			inFile.close();			// close the file
		}
		catch(IOException io)		// catch exception
		{
			// display the error message and exit program
			System.err.println("File read error occurred.");
			System.exit(0);
		}
 
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Talulia
Talulia

ASKER

I've corrected that part.

How do I get the textfile to read the items inputted like this:

Ham
Cheese
Sausage

instead of like this:

HamCheeseSausage

I know I need to use the StringTokenizer class but not sure how to use it. "I think", anyways.
// Week5.java
 
import java.util.*;
import java.io.*;
 
// class Week5 definition
public class Week5
{
	// method main
	public static void main(String args[])
	{
		System.out.println("This program will create a shopping list:\n");
 
		// call the inputItems method
		inputItems();
		// call the outputItems method
		outputItems();
 
		System.exit(0);
	}
 
	// method inputItems
	public static void inputItems()
	{
		Scanner keyboard = new Scanner(System.in);	// create a keyboard Scanner
		PrintWriter outFile = null;					// declare a PrintWriter
		String response;
		String newentry;
 
 
		try
		{
			outFile = new PrintWriter(new FileOutputStream("shoppinglist.txt"));
			// get first item from user
			System.out.print("Please enter your first item: ");
			outFile.write(keyboard.nextLine() + "\n");
 
 
 
			System.out.print("Do you wish to enter more Items? Yes/No: ");
			response = keyboard.nextLine();
 
			while( response.equalsIgnoreCase("yes"))
			{
				// get next item from user
				System.out.print("Enter your next item: ");
				outFile.write( keyboard.nextLine() + "\n" );
 
				System.out.print("Do you wish to enter more Items? Yes/No: ");
				response = keyboard.nextLine();
			}
			outFile.close();					// close the file
		}
		catch(FileNotFoundException e)		// catch exception
		{
			// display the error message and exit program
			System.err.println("File write error occurred.");
			System.exit(0);
		}
 
 
 
	}
 
	// method outputItems
	public static void outputItems()
	{
		try
		{
		// Scanner for the input file
			Scanner inFile = new Scanner( new File("shoppinglist.txt"));
 
			System.out.println("\nYour shopping list contains:\n ");
 
			while( inFile.hasNextLine() )
 
				System.out.println( inFile.nextLine() );
 
			inFile.close();			// close the file
		}
		catch(IOException io)		// catch exception
		{
			// display the error message and exit program
			System.err.println("File read error occurred.");
			System.exit(0);
		}
 
	}
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial