wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Constructor error on BufferedReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor error on BufferedReader" Watch "Constructor error on BufferedReader" New topic
Author

Constructor error on BufferedReader

Katie Smith
Greenhorn

Joined: Mar 02, 2013
Posts: 11
I'm trying to compile this program using JCreator for class, and I keep getting a compiler error, but I can't tell what's wrong.

This is the error:
C:\Users\Megan\Documents\BankingProgram.java:60: error: constructor BufferedReader in class BufferedReader cannot be applied to given types;
BufferedReader br= new BufferedReader(new java.io.InputStreamReader(System.in));
^
required: no arguments
found: InputStreamReader
reason: actual and formal argument lists differ in length
C:\Users\Megan\Documents\BankingProgram.java:63: error: cannot find symbol
s4=br.readLine();
^
symbol: method readLine()
location: variable br of type BufferedReader
C:\Users\Megan\Documents\BankingProgram.java:68: error: cannot find symbol
s5=br.readLine();
^
symbol: method readLine()
location: variable br of type BufferedReader
3 errors

Process completed.

CODE BELOW:
//The purpose of this program is to read a record and make a bank statement using BufferedReader and String Tokenizer



Thanks,
Megan
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

Welcome to the Ranch.

Haven't used JCreator. The constructor for BufferedReader takes in a Reader class as argument which InputStreamReader is a sub-class.

However, the variable s in line "StringTokenizer str =new StringTokenizer(s);" is not initialized. If you assign null to s the program will compile but fail to run.

From the look of things, you want to get user inputs from the console, why not use Scanner instead of using both StringTokenizer and BufferedReader? StringTokenizer is in fact a legacy class and should not be used. Unless you are working with files BufferedReader again is not needed in this case.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Katie Smith
Greenhorn

Joined: Mar 02, 2013
Posts: 11
I initialized s to null, but the file still failed to compile, giving the same errors as before. This is for a class assignment and I have to use both StringTokenizer and BufferedReader. Any ideas?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Do you happen to have a class you made that is named BufferedReader? The errors you posted indicate the class BufferedReader only has a no-args constructor, and has no readLine() methods. That doesn't sound like java.io.BufferedReader, it sounds like something that you got from someplace else.


Steve
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

Then I suggest you not use JCreator and copy the code to notepad then use the Oracle Java SDK to test it.

You should put the BufferedReader line in the very beginning then read the input. Then string tokenize into the s1,s2,s3 variables.

Katie Smith
Greenhorn

Joined: Mar 02, 2013
Posts: 11
Thanks that worked to compile it! But now when I run it, I get most of the way through and then it gives me this error:
--------------------Configuration: <Default>--------------------
Enter the ID, age, and expenses:
4178 18 856.75
Enter the first name initial:
M
Exception in thread "main" java.lang.NumberFormatException: For input string: "M"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Double.parseDouble(Double.java:540)
at BankingProgram.main(BankingProgram.java:70)

Process completed.

Any ideas?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

Megan Michaels wrote:Thanks that worked to compile it! But now when I run it, I get most of the way through and then it gives me this error:
...
Exception in thread "main" java.lang.NumberFormatException: For input string: "M"
...
Any ideas?

Yes. Doesn't that first message line give you any?

Give it a good read: and see if you can work out what it's telling you.
Then check where it's being called from; and try and work out why it might be happening.

If you still have problems, come back to us with your analysis and tell us what you don't understand.

Winston
Katie Smith
Greenhorn

Joined: Mar 02, 2013
Posts: 11
Okay, I realized that its throwing a number format error, and that makes sense given that I am inputting a string not a number, but the only methods I am aware of are Double.parseDouble and Integer.parseInt. I know that the variable s4 is a string, and the input for s4 was "M" another string, but Double.parseDouble doesn't want a string, it wants a number. I'm confused because I don't know a similar parse method for strings or characters.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Megan Michaels wrote:Okay, I realized that its throwing a number format error, and that makes sense given that I am inputting a string not a number, but the only methods I am aware of are Double.parseDouble and Integer.parseInt. I know that the variable s4 is a string, and the input for s4 was "M" another string, but Double.parseDouble doesn't want a string, it wants a number. I'm confused because I don't know a similar parse method for strings or characters.


Do you understand what parseDouble() does and why you would use it? If so, then what do you hope to gain by using it when you know the input is not a Double (or an Integer, or a Number)? Why do you think you have to parse that value?
Katie Smith
Greenhorn

Joined: Mar 02, 2013
Posts: 11
Okay thanks so much! I finally understand and the program worked like I wanted it to. Since it was already a string, I didn't need to parse it. Thanks for all of your help, I really appreciate it!
Megan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Welcome to the Ranch
I have added code tags, which you should use yourself. See how much better it looks
Why are you using a tokenizer? It is legacy code, which you ought not to use. Have you not been taught about Scanner, which can indentify ints in your input?
 
 
subject: Constructor error on BufferedReader