0

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);
	}
}

0

2 Answers 2

2

Your code works fine. Remove the [] near class declaration.

class VecDemo
{

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);
}
}

Refer the attachment.

Execution

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

It Worked guys.It was just a silly mistake ,I Saved the program by name "Night.java" though my class name was VecDemo. Thank you All members for your help..Highly appreciated
Haha. That's fine. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.