Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me with the below code, it only ask for int and float, but it doesn't ask for string:

package day1.example;

import java.util.Scanner;

public class ExampleInput {

    private static Scanner obj;

    public static void main(String[] args) {

        obj = new Scanner(System.in);

        int a;

        float b;

        String c;

        System.out.println("Enter a No : ");

        a = obj.nextInt();

        System.out.println("a = " + Math.pow(a,2));

        System.out.println("Enter a Float : ");

        b = obj.nextFloat();

        System.out.println("b = " + b);

        System.out.println("Enter a String :");

        c = obj.nextLine();

        System.out.println("Name = " + c);

    }

}

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

You can use the next() method which can able to read the input only till space.

 c = obj.next();

I hope this will help.

Want to know more about Java? Prefer this tutorial on Java Tutorial.

Want to become a Java Expert? Join Java Certification now!!

Related questions

0 votes
1 answer
asked Mar 9, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Mar 8, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 7, 2021 in Java by dante07 (13.1k points)

Browse Categories

...