I am new at this can you help me? I am using java

+2 votes
asked Jan 30, 2018 by Daniel Stuckey (420 points)
I want to ask what your name is then have it print it out this is my code

import java.util.Scanner;

class Main
{
    public static void main (String[] args) {
        
        Scanner input = new Scanner(System.in);
        int name;
        
        System.out.print("Enter Name");
        name = input.Scanner;
        
        if (name == 2)
        
        System.out.println("Hello");
        
        System.out.println(name);
    }
}

1 Answer

0 votes
answered Jan 30, 2018 by sbs0034 (180 points)

import java.util.Scanner;

class Main
{
    public static void main (String[] args) {
        
        Scanner input = new Scanner(System.in);
        int name;
        
        System.out.print("Enter Name: ");
        name = input.nextInt();  //gets the next integer entered from terminal
        
        if (name == 2)
        
        System.out.println("Hello");
        
        System.out.println(name);
    }
}

This code should work. To get input from keyboard use input.next(); (this gets string values) or for specific types like ints, use input.nextInt(); to get the next int entered. 

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...