Text File Transfer and Two Methods within One Class

Output Final Results to DOS and text file

Page 1 of 1

9 Replies - 3249 Views - Last Post: 26 November 2007 - 04:27 AM Rate Topic: -----

#1 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Text File Transfer and Two Methods within One Class

Post icon  Posted 23 November 2007 - 12:17 AM

I need this code to read to a text file and its not doing it. I also need this code to display the results like this:


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;
}
 
}
}
 



Is This A Good Question/Topic? 0
  • +

Replies To: Text File Transfer and Two Methods within One Class

#2 Todilo  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 13-November 07

Re: Text File Transfer and Two Methods within One Class

Posted 23 November 2007 - 01:04 AM

 
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 creating 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.next();
outputStream.println(line);

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;
break;
}
}
outputStream.close();
}
}




That is a working code but it is NOT very good. I removed the else statement. You forgot to use outputStream.println(line) to actually print the words into the file. Do not forget that you need to close the file(outputStream.close()); If nothing else this ensures a flush = your text goes into your file(println has a "built-in" flush if I am not mistaken).

One more thing. in the IF statement do not use System.exit(0); use break instead to exit the whileloop so you can clean up after yourself(close files and so on).

If you need more help or better explination do not hessitate to ask. Gtg in a hurry.
Was This Post Helpful? 0
  • +
  • -

#3 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Text File Transfer and Two Methods within One Class

Posted 23 November 2007 - 02:09 AM

My program must contain:

2 methods (inputItems and outputItems)
Both methods MUST be inside the same class file.

How do I incorporate that into this file?

Also......How do I show my output to the screen like this:

Your shopping list contains:
Ham
Cheese
whatever the user inputted basically

New Updated code:

import java.io.*; 
import java.util.*; 

public class week5

{

public static void main(String [] args)
  {
	PrintWriter outputStream=null;
	try
	{
		outputStream= new PrintWriter(new FileOutputStream("shoppinglist.txt"), true);
		}
		catch(FileNotFoundException e)
		{
			System.err.println("Error opening the file shoppinglist.txt");
			System.exit(0);
			}
			int items;
			String line=null;
			System.out.println("This program will create a shopping list:\n");
			Scanner keyboard= new Scanner(System.in);
			boolean numbersLeft= true;
			int moreitems=1;
			System.out.print("Please enter your first item: ");
			while (moreitems > 0  && keyboard.hasNext())
			{
				line=keyboard.next();
				outputStream.println("Please enter next item:");
				System.out.print("Do you wish 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;
					System.out.print("Enter the next item: ");
					}

					outputStream.close();
					}
					}




Can someone help me when you get time? Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Text File Transfer and Two Methods within One Class

Posted 24 November 2007 - 04:51 PM

please do not double post, i have removed the duplicate and merged the new text.


We are not here to do your homework for you. If you want help with something or have errors we will be glad to help you with that.
Describe the problems you are having, please.
For more clarity, refer to the rules (links available in my signature)
Was This Post Helpful? 0
  • +
  • -

#5 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Text File Transfer and Two Methods within One Class

Post icon  Posted 24 November 2007 - 06:49 PM

Okay: I am having problems getting my code to list all the items a
user enters.
System.out.print("Your shopping list contains:\n" + line + "\n");
When I do it this way it only shows the last entry the person
typed as a shopping list item.
There is something I am doing wrong, but can't figure out how to
re-word it. The moreitems==0 which throws everything off.
I'm doing it to where moreitems > 0 but these are not numbers I'm dealing with. Trying to figure out to allow my output to come out on the screen.

Any help, thanks.



import java.io.*;
import java.util.*;


public class week5

{
public static void main(String [] args)
  {
	PrintWriter outputStream=null;
	try
	{
		outputStream= new PrintWriter(new FileOutputStream("shoppinglist.txt"), true);
		}
		catch(FileNotFoundException e)
		{
			System.err.println("Error opening the file shoppinglist.txt");
			System.exit(0);
			}
			int items;

			String line=null;
			System.out.println("This program will create a shopping list:\n");
			Scanner keyboard= new Scanner(System.in);
			boolean numbersLeft= true;
			int moreitems=1;
			System.out.print("Please enter your first item: ");
			while (moreitems > 0  && keyboard.hasNext())
			{
				line=keyboard.next();
				outputStream.println("Please enter next item:");

				System.out.print("Do you wish to enter more items? 1 = yes, 0 = no. ");
				moreitems=keyboard.nextInt();
				if (moreitems>0)
				{
					numbersLeft=false;
					System.out.print("Your shopping list contains:\n" + line + "\n");
					System.exit(0);
					}
					else
					numbersLeft=true;
					System.out.print("Enter the next item: ");



}
					outputStream.close();
					}
					}
[color=#CC33CC]


Was This Post Helpful? 0
  • +
  • -

#6 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Text File Transfer and Two Methods within One Class

Posted 25 November 2007 - 01:12 AM

1. Why doesn't this shoppinglist.txt code not output to a textfile? That was my goal to get it to do that.
2. How do I incorporate two methods named inputItems and outputItems with this already working code?
3. How do I output to the DOS screen the final results of the shopping list?



import java.io.*;
import java.util.*;


public class week5

{

public static void main(String [] args)

  {
	PrintWriter outputStream=null;


	try

	{
		outputStream= new PrintWriter(new FileOutputStream("shoppinglist.txt"));

		}

		catch(FileNotFoundException e)

		{
			System.err.println("Error opening the file shoppinglist.txt");

			System.exit(0);

			}


			boolean done=false;
			String input = null;
			String ans = null;

			System.out.println("This program will create a shopping list:\n");

			Scanner keyboard = new Scanner(System.in);

			System.out.print("Enter your item: ");

			input = keyboard.nextLine();

			System.out.print("Do you wish to enter more items? y/n ");

			ans = keyboard.nextLine();


			while(!done)
			{
				if (ans.equalsIgnoreCase("y"))
				{
					System.out.println("Thank you, please enter another item.");
					input = keyboard.nextLine();
					System.out.println("Do you wish to enter other items? y/n:");
					ans = keyboard.nextLine();
					ans.equalsIgnoreCase("y");
					done=false;
				}
				if (ans.equalsIgnoreCase("n"))
				{
					System.out.println("Thank you for your entries. You can find the file shoppinglist.txt located in your textfile. ");
					done=true;
				}
			}




			outputStream.close();

			}

			}



Was This Post Helpful? 0
  • +
  • -

#7 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Text File Transfer and Two Methods within One Class

Posted 25 November 2007 - 09:41 AM

View Postxosunkist, on 25 Nov, 2007 - 09:12 AM, said:

1. Why doesn't this shoppinglist.txt code not output to a textfile? That was my goal to get it to do that.


Because you only output anything with the System.out.println, and it writes to the standard output, which is the console by default.
You can write to your file with the outputStream PrintWriter object, that you create right in the beginning. Something like outputStream.println(Some string) should do.

View Postxosunkist, on 25 Nov, 2007 - 09:12 AM, said:

2. How do I incorporate two methods named inputItems and outputItems with this already working code?

You create another function just like the main function, and you can call it from the main function.
public static void main(String [] args)
 {
	  /* 
		 some code 
		  ...
	  */
	  inputItems();  // this calls the inputItems function
  }


public static void  inputItems()
 {
		/* 
			put the logic of the function here 
			....
		 */
  }



[rl=http://java.about.com/library/weekly/aa_methods1.htm] Read more about methods here[/url]

View Postxosunkist, on 25 Nov, 2007 - 09:12 AM, said:

3. How do I output to the DOS screen the final results of the shopping list?

You can write to the DOS screen with System.out.println() . To output everything you should probably store the content of the list in a variable, and output it at the end. (Alternatively you could open your file, and output it's content, but that would not make much sense in this case, I think)
Was This Post Helpful? 0
  • +
  • -

#8 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Text File Transfer and Two Methods within One Class

Posted 25 November 2007 - 10:07 AM

xosunkist, please stop starting multiple topics for the same question! I've just merged three of them. This is a waste of our time, because the same question is answered multiple times, and I think even you'll have problem finding the answers. Thank you!
Was This Post Helpful? 0
  • +
  • -

#9 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Text File Transfer and Two Methods within One Class

Posted 25 November 2007 - 04:05 PM

This code compiles successfully.

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.
In other words, vertically instead of horizontally in the shoppinglist.txt file.

// 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);
		}
 
	}
 


Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Text File Transfer and Two Methods within One Class

Posted 26 November 2007 - 04:27 AM

Quote

I know I need to use the StringTokenizer class but not sure how to use it. "I think", anyways.
In other words, vertically instead of horizontally in the shoppinglist.txt file.


If all the values are on different lines, then you won't need the StringTokenizer, because nextLine will return a line, that contains the value, so it is already "tokenized".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1