-3
import java.util.Scanner;

public class StrinExp {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        String a = null;
        System.out.println("Enter username");
        i=scanner.nextInt();
        a=a.valueOf(i);
        System.out.print(a);
    }
}

But m getting error when i am giving a alphanumeric value..

ex: Manish0818

and even when i am giving same value as string, still facing the same problem.

Help

beresfordt
  • 5,088
  • 10
  • 35
  • 43

1 Answers1

2

change your input statement.

String a = null;
System.out.println("Enter username");
a = scanner.next();
System.out.println(a);

If there's an alphanumeric, then take it in as a string, not as an integer. You will land in an InputMismatchException if you do so. It means that you're trying to take an integer but you are entering a string which differ in data types.

and if you wish to remove all junk characters except alphanumeric then you can try

a = a.replaceAll("[^a-zA-Z0-9]+", "");
sameera sy
  • 1,708
  • 13
  • 19