Example Run:
This program will create a shopping list:
Please enter your first item: Ham
Do you wish to enter More Items? Yes/No: Yes
Enter your next item: Cheese
Do you wish to enter More Items? Yes/No: No
Your shopping list contains:
Ham
Cheese
import java.io.*; // uses the java io library for the
import java.util.*; // BufferedReader class
public class week5_701
{
public static void main(String [] args)
{
PrintWriter outputStream=null;
try
{
outputStream= new PrintWriter(new FileOutputStream("shoppinglist.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file shoppinglist.txt");
System.exit(0);
}
int items;
String line=null;
System.out.println("This program will put items in your shopping list");
Scanner keyboard= new Scanner(System.in);
boolean numbersLeft= true;
int moreitems=1;
while (numbersLeft)
{
System.out.println("Enter the next item");
line=keyboard.nextLine();
keyboard.nextLine();
System.out.println("Would you like to enter more items, enter 1 for yes or 0 for no");
moreitems=keyboard.nextInt();
if (moreitems==0)
{
numbersLeft=false;
System.exit(0);
}
else
numbersLeft=true;
}
}
}

New Topic/Question
Reply




MultiQuote






|