Results 1 to 11 of 11
  1. #1
    Chesh is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default Hi there, and i have little problem with readLine

    Hi there, I am currently doing my second year at uni doing Business Information Technology and seem to have forgotten everything over the christmas holidays!

    I get an error message saying
    "cannot find symbol - method readLine()"
    when i try to compile this code
    Java Code:
    import java.io.*;
    
    final public class Checking extends bankAccount
        {
            private String indata;
            
            public Checking(String accountNameIn, String custNameIn, float balanceIn)
               {
                   super (accountNameIn, custNameIn, balanceIn);
               }
    
             public void setAccountName(String accountNameIn, String args[]) throws IOException
               {
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                System.out.print ("Enter the name of your account: ");
                accountNameIn = indata.readLine();
               }
    
             public void setCustName(String custNameIn)
               {
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                System.out.print ("Enter the name of the holder of the account: ");
                custNameIn = indata.readLine();
               }
               
               public void deposit (float balanceIn)
               {
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                System.out.print ("Enter how much you would like to deposit in your checking acount: ");
                balanceIn = indata.readLine();
               }
               
               public void withdraw()
               {
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                System.out.print ("Enter how much you would like to withdraw from your checking account: ");
                balanceIn = indata.readLine();
               }
               
               public String getAccountType()
               {
                  return ("Checking Account");
               }
     }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are remembering more than you realize. Your main problem that I see is that you're calling readLine on the "indata" String not on the BufferedReader object, "in".

  3. #3
    Chesh is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default

    AH so if i just put
    "accountNameIn = in.readLine();"
    instead of
    "accountNameIn = indata.readLine();"
    should it work?
    The thing is i have just taken that part of code just to save writing it out from another program i have written and didnt have any problems with that, if possible i would like to be able to keep it as indata just to prevent me confusing myself?

    Is that possible or have i got the wrong end of the stick?

  4. #4
    Chesh is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default

    AAHHHHHHH thank you sooo much for pointing that out, just noticed that it was "BufferedReader in" instead of "BufferedReader indata"!

    Was tearing my hair out over that thank you fubarable lol!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I am a bit worried that you are creating multiple bufferedreaders but never closing them (that I can see at least). If you are not doing so, you will want to take care to manage your limited resources here.

  6. #6
    Chesh is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default

    I am sorry I do not understand what you mean by not closing them. Please explain and i will do my best to implement it.

    I havent been taught about closing the bufferedreaders?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Java is pretty good at preventing memory and similar leaks, but AFAIK, your system has a limited number of resources for I and O, and whatnot, and when you open a Reader object such as a BufferedReader, you are using one of these resources. When you are done with it, you close it so as to free the resource and allow it to be used by something else. I usually use my resource within a try block and close the resource (after checking that it's not null) in the finally section of the try/catch/finally block.

    This is even more important when you are writing to a file as if you don't close the resource that does the writing, it may not be flushed, and there may be data left in the resource that never gets written to the disk.

    Also note that if you create a nested reader such as you do here:
    Java Code:
    new BufferedReader(new InputStreamReader(System.in));
    it is only necessary to close the outer resource, here the BufferedReader, and not the inner resources, here the InputStreamReader.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default you have more than just readline errors

    Java Code:
    import java.io.*;
    
    public class test{
    	public static void main(String[] args) throws Exception
    	{
    		float balanceIn = 10.0f;
    		System.out.println("B4 deposit: " + balanceIn);
    		deposit(balanceIn);
    		System.out.println("After deposit: " + balanceIn);
    	}
    
    	public static void deposit (float balanceIn) throws Exception
    	{
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    		System.out.print ("Enter how much you would like to deposit in your checking acount: ");
    		balanceIn = Float.valueOf(in.readLine());
    		System.out.println("during deposit: " + balanceIn);
    		in.close();
    	}
    }
    Java Code:
    OUTPUT:
    S:\Temp>java test
    B4 deposit: 10.0
    Enter how much you would like to deposit in your checking acount: 2
    during deposit: 2.0
    After deposit: 10.0
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Chesh is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default

    Fubarable: ok so what I gathered from that, you can't use bufferedreader as many times as you like, there's only a certain number of bufferedreaders.
    If this is right, how can I either use just the one bufferedreader for the whole class or how do I close each instance of bufferedreader?

    Angryboy: afaik this shouldn't be a problem I won't actually be running re program from this class but I will tackle that when it becomes one!

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    in.close();
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Chesh is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

Similar Threads

  1. problem with console.readLine()
    By thatguy in forum New To Java
    Replies: 1
    Last Post: 12-14-2008, 08:40 PM
  2. [SOLVED] ReadLine(String fmt,Object... args) of Console class
    By Pooja Deshpande in forum New To Java
    Replies: 4
    Last Post: 04-25-2008, 06:51 AM
  3. DataInputStream readLine()
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 11:44 PM
  4. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 08:44 AM
  5. BufferedReader: readLine method problems
    By bbq in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2007, 03:27 AM

Posting Permissions

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