Search code examples
javajava.util.scannerstring-concatenation

Unable to take multiple String input using scanner in java


I have been trying to do a program in which i have to concat three strings in java.I am taking input from user using Scanner.It compiles perfect but when i run it,it gives me this error:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Night.main(Night.java:10)

This is my code:

import java.lang.*;
import java.util.*;

class Demo {
	public static void main(String[] args) {
		String fn;
		String mn;
		String ln;
		String fmn, lmn;
		Scanner sc = new Scanner(System.in);
		fn = sc.nextLine();
		mn = sc.nextLine();
		ln = sc.nextLine();
		fmn = fn.concat(mn);
		lmn = fmn.concat(ln);
		System.out.println("The Full name of candidate is : " + lmn);
	}
}


Solution

  • I believe your current code in Demo class is working fine. Can you check again?! The exception is from Night.main(Night.java:10). If you still have the same error, can you check if there is any Night class in your working space?!

    Hope this help.