• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Scanners in Java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

     
 
Saloon Keeper
Posts: 10779
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10779
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10779
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10779
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?



 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
reply
    Bookmark Topic Watch Topic
  • New Topic