Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java – How to Create a class which can take input as Name Surname and ID

I don’t know why my code is not letting me input any text as it should… my output is 0
Process finished with exit code 0

My code is

import java.util.Scanner;

public class MyThirdHomework {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);

        System.out.println("Please enter your name:");
        String a=scan.nextLine();

        System.out.println("Please enter your surname");
        String b=scan.nextLine();

        System.out.println("Please enter your ID:");
        int c=scan.nextInt();

        System.out.println("Your name is:" + a);
        System.out.println("Your surname is:" + b);
        System.out.println("Your student ID is:" + c);
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code is fine. It works for me. I suspect you are running in an IDE that is hiding the output from you. How are you running this code?

From a command line I ran:

javac MyThirdHomework.java
java MyThirdHomework

Please enter your name:
Scott
Please enter your surname
TheGreat
Please enter your ID:
42
Your name is:Scott
Your surname is:TheGreat
Your student ID is:42

When it is done the process exits without an error, which may be why whatever you were using says "Process finshed with exit code 0"

If you are running on Windows make sure the code runs with "java.exe" and not "javaw.exe"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading