1

I can get and print the integer value in java but I am confuse how to get and print string. Can someone help

package hello;

import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        int integer;

        System.out.println("Please Enter Integer");

        Scanner sc = new Scanner(System.in);

        integer = sc.nextInt();

        sc.close();


        System.out.println("you entered : " +integer);


    }
}

Program output

Please Enter Integer
5
you entered : 5

I am stuck in this program. I don't understand how to get string and print on screen

import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        int name;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter your name");


        name = sc.nextInt(); 

         sc.close();

         System.out.println("Your name"+name);


    }
}
2
  • sc.nextLine() ? Commented Jul 29, 2018 at 11:10
  • Change type of name to String, and then name = sc.nextLine(); Commented Jul 29, 2018 at 11:11

4 Answers 4

2

You need to change your type value name from int to String. And replace sc.nextInt() by sc.nextLine() or sc.next().

Example

public static void main(String[] args) {

    String name;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter your name");


    name = sc.nextLine();

    sc.close();

    System.out.println("Your name " + name);

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

Comments

0

Use sc.nextLine() for reading string inputs or sc.next() (But this will read only a word before it encounters a space)

You can also use InputStreamReader for this purpose

eg.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in()));

String input = br.readLine();

Comments

0

name = sc.nextInt(); doesn't work for strings, only for integers, you should use sc.nextline instead. And also you have to change int name to String name, due to other type of variable.

Your code should look like this:

 import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        String name;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter your name");


        name = sc.nextLine(); 

         sc.close();

         System.out.println("Your name"+name);


    }
}

Comments

0

change int name to string name and use sc.nextLine()

import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        String name;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter your name");


        name = sc.nextLine(); 

         sc.close();

         System.out.println("Your name"+name);


    }
}

Comments

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.