[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

How to use Scanner class

Hi

I am trying to Write a set of instructions to prompt the user for an int value and input it using the Scanner class into the variable x and prompt the user for a float value and input it using the Scanner class into the variable y.

IN JAVA Programming
0
Ansary100
Asked:
Ansary100
  • 4
1 Solution
 
InteractiveMindCommented:
This should do it:


    Scanner sc = new Scanner( System.in );
   
    int x;
    float y;
   
    try
    {
        x = sc.nextInt();
        y = sc.nextFloat();
    } catch ( Exception e )
    {
        e.printStackTrace( System.out );
    }

    // You now have two variables, 'x' (int), and 'y' (float).



Rob.
0
 
InteractiveMindCommented:
Here's the official Java documentation, for the Scanner class; you may find it useful:

   http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

0
 
InteractiveMindCommented:
..Thanks; I'm not quite sure why I only got a 'B' grade however?
0
 
InteractiveMindCommented:
I'm hoping that you can justify it, otherwise I'll request that the Admin/Moderator ups it to an 'A'.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now