Results 1 to 2 of 2
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default Text and ASCII Code I/O

    I am writing a program for a "Magic Eight Ball" that will read in the answers from a text file. I can get single digit numbers' ASCII code to read in, but how would I read in a "10" for example?

    Thanks!

    AB

    Java Code:
    import java.io.*;
    
    class MagicBall
    {
    	public static void main(String[] args) throws IOException
    	{
    		FileReader inputStream = null;
    		FileWriter outputStream = null;
    
    		try
    		{
    			inputStream = new FileReader("answers.txt");
    			outputStream = new FileWriter("done.txt");
    
    			int c;
    			boolean done = false;
    
    			while(((c = inputStream.read()) != -1) && !done)
    			{
    				if(c == 55)
    				{
    					while((c = inputStream.read()) != -1)
    					{
    						outputStream.write(c);
    
    						if(c == 46)
    						{
    							break;
    						}
    					}
    
    					done = true;
    				}
    			}
    		}
    
    		finally
    		{
    			if (inputStream != null)
    			{
    				inputStream.close();
    			}
    
    			if (outputStream != null)
    			{
    				outputStream.close();
    			}
    		}
    	}
    }
    Attached Files Attached Files

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Look up methods for something like nextline, you may also want to chain on a buffered stream, I am not great with io and others can probably give you better advice but that is my solution. When in doubt search the API for possible methods, use the sun tutorials or look up other tutorials on google.

Similar Threads

  1. ASCII to binary code
    By Vagabond.drv in forum New To Java
    Replies: 12
    Last Post: 01-13-2011, 01:52 PM
  2. Changing text colour in code
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-29-2009, 03:32 PM
  3. Replies: 3
    Last Post: 12-12-2008, 12:12 PM
  4. Ascii code.........
    By Somitesh Chakraborty in forum New To Java
    Replies: 6
    Last Post: 11-04-2008, 05:32 PM
  5. How to obtain ASCII code of a character
    By karma in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 02:57 AM

Tags for this Thread

Posting Permissions

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