Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't resolve method println

 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Learning Java, don't understand why I can't do this.



Is there really a need for two catches?


[Added code tags - see UseCodeTags]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Are you sure you have typed the name of the method correctly? It must be "println", so "print" followed by lower-case letter L and lower-case letter N. Note that it is not for example "printIn" with an upper-case letter I instead of a lower-case letter L (that's a mistake that people sometimes make).
 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch.

Are you sure you have typed the name of the method correctly? It must be "println", so "print" followed by lower-case letter L and lower-case letter N. Note that it is not for example "printIn" with an upper-case letter I instead of a lower-case letter L (that's a mistake that people sometimes make).

Thanks for the response, yes it is spelled correctly, rewrote it to make sure.

Still get can't resolve method println(?), Could it be something with the howmany variable?
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I think it is. You never told the compiler which type howmany is - try changing the statement where howmany is first used, like you did for number.
 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:Yup, I think it is. You never told the compiler which type howmany is - try changing the statement where howmany is first used, like you did for number.




Changed it, still have the error, I tried moving it down into the catch but then the while loop gave the error //can't resolve symbol.


edit:

Here is what I did now,

int howmany = 0;

try {
System.out.println("how many numbers do you want to enter?");
howmany = in.nextInt();

Removes the errors, but they don't get the value from howmany = in.nextInt(); , and only return 0.

Not sure what to do, since I can't make a new variable to store howmany outside of the try/catch.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about the scope that you've declared howmany in. It's not available on line 20. If you want a variable available in both the try and catch blocks it needs to be declared outside both of them.

There's another problem as well. What if line 8 throws the InputMismatchException?
 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Think about the scope that you've declared howmany in. It's not available on line 20. If you want a variable available in both the try and catch blocks it needs to be declared outside both of them.

There's another problem as well. What if line 8 throws the InputMismatchException?


I moved some stuff around and I get that InputMismatchException now, my catch is not grabbing it.
I tried moving just the catch and that gave me the error "catch without try", then I moved try up top to fix the error.

The code that is in the try is the only stuff that will be caught, if I move the howmany variable inside the try I will get errors because the availability ends after the try bracket.

I am not sure what to do about this.

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could declare and initiliaze howmany outside of the try block (with 0, for example), and then assign it in the try block. The scope will be larger, and everything you want in the try block is in it.

You're doing the exact same thing with total and divide.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted is not a complete program. Can you post your complete source code, and the exact error message? Copy and paste the whole error message. The more information you give us, the better we can help.
 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The code you posted is not a complete program. Can you post your complete source code, and the exact error message? Copy and paste the whole error message. The more information you give us, the better we can help.




Error


You could declare and initiliaze howmany outside of the try block (with 0, for example), and then assign it in the try block. The scope will be larger, and everything you want in the try block is in it.
You're doing the exact same thing with total and divide.


I tried that, but the catch didn't read the value inside of the try, it only read the value outside and returned 0.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you've fixed the "can't resolve method" error? Because the above output shows that you've successfully compiled the program and you can run it.

You enter "2.4" as an answer to the question "how many numbers do you want to enter?". Well, that doesn't work, because you can't enter 2.4 numbers. It's expecting you to enter an integer, not a floating-point number. If you enter "2.4", then line 12 is going to throw an InputMismatchException because 2.4 is not an integer.

Try entering an integer instead of "2.4".
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avery key wrote:
You could declare and initiliaze howmany outside of the try block (with 0, for example), and then assign it in the try block. The scope will be larger, and everything you want in the try block is in it.
You're doing the exact same thing with total and divide.


I tried that, but the catch didn't read the value inside of the try, it only read the value outside and returned 0.

That could be because of the other problem I mentioned. If the exception is thrown on the first nextInt() call, then the value never gets set anyway. Since you don't print that stack trace you've hidden that information.

This is the general approach you need to use. But I would suggest - you need to stop just moving things around to see if they work, and try to understand why it works as it does. Understand the scope of variables, and the order your code is executed in. Then you can write the code knowing what it's going to do.
 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:So, you've fixed the "can't resolve method" error? Because the above output shows that you've successfully compiled the program and you can run it.

You enter "2.4" as an answer to the question "how many numbers do you want to enter?". Well, that doesn't work, because you can't enter 2.4 numbers. It's expecting you to enter an integer, not a floating-point number. If you enter "2.4", then line 12 is going to throw an InputMismatchException because 2.4 is not an integer.

Try entering an integer instead of "2.4".


This works for integers, what I want it to do is catch the exception from entering a double and print the number they typed.
I can catch the double but I can't print the number they typed, that's my problem.


That could be because of the other problem I mentioned. If the exception is thrown on the first nextInt() call, then the value never gets set anyway. Since you don't print that stack trace you've hidden that information.

This is the general approach you need to use. But I would suggest - you need to stop just moving things around to see if they work, and try to understand why it works as it does. Understand the scope of variables, and the order your code is executed in. Then you can write the code knowing what it's going to do.


I understand how and why it works, but I don't understand how to read the value of howmany in the catch, if I don't put the variable howmany into the try statement then I can't catch the exception it's going throw.
I just want to be able to catch the exception and print back the number that they entered in the catch.
I don't know how to initialize the value outside of the try so it's available for the catch and catch the exception at the same time.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you have to wrap line 12 in a try-catch block. But it's probably not going to work because in.nextInt() isn't going to return what the user typed, in case an exception is thrown. What you'd have to do is read the input as a string, using the nextLine() method of class Scanner, and then interpreting that as a number, using Integer.parseInt(). If that last method throws an exception, you'll still have the string with the text that the user typed, and you can show that in an error message.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avery key wrote:This works for integers, what I want it to do is catch the exception from entering a double and print the number they typed.
I can catch the double but I can't print the number they typed, that's my problem.

In that case I'd suggest getting the string they typed first. Once you've got that in a variable you can refer to it in error messages. Then parse it to an integer. As things stand you're trying to use an int variable to print out something that can't be stored in an int variable. If it was possible, you wouldn't have the error in the first place! So you need to keep the original input in a form that can be stored, and a String is your only reliable option there.
 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure this out.


import java.util.InputMismatchException;
import java.util.Scanner;



I changed counter to nextLine and tried to print it, then I converted it to an int so I could use it for the while loop, and I am still getting errors; If someone can show me the right way to do this, It would help me out a lot.

} catch (InputMismatchException exception) {
System.out.println("You can't enter " + counter + " numbers");



edit:

I've decided to settle with the code below, because I don't know what to do.



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Matthew and I told you: you cannot use in.nextInt(); to solve this. Because if you use that, and what the user entered cannot be parsed, then an exception will be thrown and you won't have what the user entered, so you can't print what the user entered.

So, instead of in.nextInt(); use in.nextLine(); to get the result as a String. Then parse that String into an int. Example:
 
avery key
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the response, I understand now.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic