How to take a scanner object and make it a String
50,204
Solution 1
String input = yourScannerObject.nextLine ();
where "yourScannerObject" is the name you give your scanner.
Solution 2
What method did you use to scan? is it {scanner object name}.next()
?
if so you have got a string and all that you have to do is create some string, and save the input to it, e.g.:
String str="";
str = {scanner object name}.next();
before using anything in java, I would advise you to read the API :
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#next()
Author by
user2558595
Updated on July 09, 2022Comments
-
user2558595 almost 2 years
I need help doing the following:
- receiving input using Scanner class (I got this)
- taking input from scanner and making it a String
- use
replaceAll
to remove numbers 0-9 from user input.
The below code is what I have so far but it is only returning user input and not removing numbers:
public static void main(String[] args) { Scanner firstname = new Scanner(System.in); System.out.println("Please enter your first name:"); String firstname1 = firstname.next(); firstname1.replaceAll("[^0-9]",""); System.out.println(firstname1);
Updated Code. Thank you Hovercraft. I am now investigating how to retrieve all alpha characters as with the code below, I am only getting back the letters prior to the numeric values entered by the user:
import java.util.Scanner; public class Assignment2_A { public static void main(String[] args) { Scanner firstname = new Scanner(System.in); System.out.println("Please enter your first name:"); String firstname1 = firstname.next(); firstname1 = firstname1.replaceAll("[^A-Z]",""); System.out.println(firstname1);