• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm this close to tearing out every strand of hair I have.  RSS feed

 
Leeana Lee
Greenhorn
Posts: 5
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize in advance for the coloring, it's very hard to read or type in all black text for me, plus it looks pretty.


Tried to make a very, very simple text based game (think homestuck), except wow, look at all these errors!
at first i tried using the scanner to input the name, but it wasn't working w/ me, and so i tried creating a new method for the name, resulting in the mess down below. the most prominnent error i get is Error: Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration, and also Warning: Resource leak: 'scanner' is never closed. I'm very, very new to java, and yes i'm following the big and bad java tutorial but i wanted to step away and try something new for once.




 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By convention, method names should start with a lowercase letter so that they don't get confused with constructor names and type names. So, "Username" on line 36 should be "username".

On line 36, you define the method to take a String as its argument. That means when you call the username method, you need to pass it a String. Neither of the calls you try to make on line 11 and 21 do that.

Think about your design though. Why would you need to pass a String to username()? What is the string for? In the method, you're just asking the user to enter their name, then returning whatever they entered. You have no use for a parameter since the method has everything it needs to do its job already.  If you want to define a method that doesn't take any arguments, then just don't put anything inside the parentheses in the method declaration.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing: You really should format and indent your code properly. The code you posted looks like it's randomly indented. That makes your code very difficult to read because the structure and organization of the code is not apparent. 
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and Welcome to the Ranch!
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are few things you need to understand in Java to correct your code:

How to define and call a method?

You can refer to following tutorial:

https://www.tutorialspoint.com/java/java_methods.htm

How to user Scanner to read input from console ?



Output:

OSHETKAR-M-W22C:Introduction oshetkar$ java ScannerTest
Omkar
name: Omkar
25
number: 25


Please go through the above tutorial and example carefully, and let us know your specific confusion/doubts.

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite how most IDEs try to "help" by telling you that you forgot to close a Scanner, you really shouldn't be closing a Scanner that's tied to System.in -- those warnings can be safely ignored or suppressed with an @SuppressWarnings("resource")

So, there's really no need to wrap that code in a try-finally block.
 
Leeana Lee
Greenhorn
Posts: 5
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:One more thing: You really should format and indent your code properly. The code you posted looks like it's randomly indented. That makes your code very difficult to read because the structure and organization of the code is not apparent. 


sorry, but is there a guide that helps me do this?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find style guides by searching for How to properly indent Java code. Choose one and follow it consistently.

If you're using an IDE like Eclipse, you can have it automatically reformat your code to a common indentation style by pressing Ctrl+Shift+F.
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Despite how most IDEs try to "help" by telling you that you forgot to close a Scanner, you really shouldn't be closing a Scanner that's tied to System.in -- those warnings can be safely ignored or suppressed with an @SuppressWarnings("resource")

So, there's really no need to wrap that code in a try-finally block.


Completely agree. After some quick googling, I see that, System.in is opened by JVM. Hence, it's the responsibility of JVM to close the same. Java program need not worry about closing System.in resource.
Thanks.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omkar, have a cow for actually going out and googling for a reason to not close System.in -- In fact, since this a fairly common thing that comes up in this forum, I'll add it to the Beginner FAQ.

Edit: Added this to the Beginner FAQ. See this.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!