Results 1 to 2 of 2
  1. #1
    cmb64 is offline Member
    Join Date
    Feb 2017
    Posts
    1
    Rep Power
    0

    Default New to this forum...need help please with input utility class

    hi guys,

    new here...and fairly new to java...about 3 months under my belt

    i am trying to write a utility class to handle all my user input for my program im writing

    here is the code,then i will explain the problem

    Java Code:
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class InputUtility {
    
        static Scanner userInput = new Scanner(System.in);
    
        private InputUtility() {
        }
    
        public static int getNumber() {
    
            System.out.println("Enter a number:");
    
            while (!userInput.hasNextInt()) {
                userInput.next();  //this line important to ask for input again so that 18 doesnt just do an infinite loop
                System.out.println("Please enter a number");
            }
    
            int userNumber = userInput.nextInt();
            //System.out.println(userNumber);
            return userNumber;
        }
    
        public static String getString() {
    
            System.out.println("Enter characters:");
    
            String userCharacters = userInput.nextLine();
    
            return userCharacters;
        }
    }
    my main will just call the methods etc,there is no code in my main

    the problem is that i cant print out the variables userNumber and userCharacters in my main method...i cant even see them...
    i want to be able to capture user input for various areas in my program...how can i accomplish this?

    thanks for reading

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: New to this forum...need help please with input utility class

    cant print out the variables userNumber and userCharacters in my main method.
    Those variables are defined locally to the methods they are in which means they are not available to other methods.

    However their values are returned by the methods that they are defined in. The code that calls those methods should save the value that is returned.

    See also: https://coderanch.com/t/676089/java/print-returned
    Last edited by Norm; 02-15-2017 at 01:50 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 06-15-2014, 06:25 PM
  2. Replies: 6
    Last Post: 02-04-2013, 12:14 PM
  3. class diagram for creating forum
    By 10e5x in forum New To Java
    Replies: 7
    Last Post: 11-28-2010, 03:00 PM
  4. Calling a method in a utility class
    By TMobotron in forum New To Java
    Replies: 2
    Last Post: 09-17-2010, 07:39 AM
  5. Replies: 0
    Last Post: 04-16-2008, 11:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •