Results 1 to 2 of 2
- 02-15-2017, 12:19 AM #1
Member
- Join Date
- Feb 2017
- Posts
- 1
- Rep Power
- 0
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; } }
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
- 02-15-2017, 12:30 AM #2
Re: New to this forum...need help please with input utility class
cant print out the variables userNumber and userCharacters in my main method.
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-returnedLast 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
-
multi module project that uses 1 instance of utility class
By yossijava in forum Apache MavenReplies: 0Last Post: 06-15-2014, 06:25 PM -
Difference between jar file created with the jar utility and the zip utility
By JunkerMartin in forum New To JavaReplies: 6Last Post: 02-04-2013, 12:14 PM -
class diagram for creating forum
By 10e5x in forum New To JavaReplies: 7Last Post: 11-28-2010, 03:00 PM -
Calling a method in a utility class
By TMobotron in forum New To JavaReplies: 2Last Post: 09-17-2010, 07:39 AM -
A utility class that parses a Comma Separated Values (CSV) file
By Java Tip in forum java.ioReplies: 0Last Post: 04-16-2008, 11:58 PM
Bookmarks