0

How to convert a user input word to string which is read using scanner class?

19th Jul 2016, 6:14 PM
KIRAN JOHNSON
KIRAN JOHNSON - avatar
3 Réponses
+ 2
the next method already returns it as a string
19th Jul 2016, 8:42 PM
Gershon Fosu
Gershon Fosu - avatar
+ 2
To extend Gershon Fos's answer with code: Scanner sc = new Scanner(System.in); String userInput = sc.next(); This code asks for user input, then saves the input as string in variable userInput.
19th Jul 2016, 9:02 PM
Thomas Alme
Thomas Alme - avatar
0
there various ways Java parses values, you can use these methods to cast from on type to another, when this is not possible, Java raises a classcastexception such as int hello_world = sc.next parseInt("hello world") would throw an exception but to answer your question all you need to do to statically cast objects, you can use Javas parse methods to cast types, look at parseString()
21st Jul 2016, 5:17 AM
Eric Gitangu
Eric Gitangu - avatar