Solved

Mulitple entries-Java

Posted on 2007-11-25
5
194 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:Talulia
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 20347228
>                         while( response.toUpperCase().equals("Yes") )

change to:

                        while( response.equalsIgnoreCase("yes") )
0
 

Author Comment

by:Talulia
ID: 20347263
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

0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 250 total points
ID: 20347319
your code is fine

if you open with a wordpad you can see as you expect.. if you want to see them in notepad also use System.getProperty("line.separator") instead of "\n"

two lines in you code should look like

outFile.write(keyboard.nextLine() + System.getProperty("line.separator"));
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 91
micro services vs rest web services 16 84
sites similar to codingbat to improve coding hanson skills 3 40
Java - Why doesn't this JFrame work 3 41
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now