File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor error on BufferedReader

 
Katie Smith
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Katie Smith
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9472
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic