• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Scanners in Java  RSS feed

 
Brett Banich
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started working with java. While using a scanner, how can I take the users input and use the variable for the size? I will post my code below. I'm trying to use the users input as a int variable to scale the sizing off my mirror. If anyone can give me an idea of how to do this that would really help.

     
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, variables should begin with a lower case character, so it should be "console" and "size". Names in all caps should be used for constants only.

Then to get an int from a Scanner:
Never close() a Scanner constructed using System.in.
 
Brett Banich
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now that i've changed that what should I have under public static void main? Also, do I need to add something else to each of the sections to allow for the input to be used?
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to see this thread: Wrapper class for Scanner

If you include that class file along with other .java files in your project, then you could do this:
And it would work inside any method in any class.
 
Brett Banich
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting an error on line 12. How can I fix this?
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable size



 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You might want to see this thread: Wrapper class for Scanner

If you include that class file along with other .java files in your project, then you could do this:
And it would work inside any method in any class.

That's "Keyboard" with an upper case "K". It starts with an upper case because "Keyboard" is a class name and not a variable.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to go with the wrapper approach you don't need line 10 (in your last post) at all. If you want to stick with your own keyboard I suggest you make it a constant because you should only make a Scanner from System.in once and use it everywhere.
 
Brett Banich
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this compiles with no errors. After I run it, the only thing that displays is a #. Why doesn't the text System.out.println("Please enter Size: "); appear?



 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You see "#" as the first prompt because in main() you first call line(), and the first thing you do in line() is print a "#".
 
Brett Banich
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I figured that out. Now my program does as it is supposed to. If I wanted to add two methods where would I place them?

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question doesn't have a hard and fast answer, aside from "Inside the class" and "Not inside another method."  Usually "At the bottom of the class" works, or "Below all the methods that call it."  There are some methods that are traditionally at the end of the class: toString(), hashCode(), equals().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!