0
import java.io.*;
import java.util.*;
public class Test
{
    public static void main(String args[])
    {
        Scanner kbr = new Scanner(System.in);
        System.out.print("Enter Player's Name: ");
        double n = kbr.nextInt();
        System.out.print(n);
    }
}

Can I change this to

char n = kbr.nextInt()

or how can I get it to look for a character string instead of an int.

Krishna Chalise
  • 147
  • 1
  • 15
B. Moore
  • 23
  • 1
  • You can get a `String` with `kbr.next()`. [Javadocs](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) are your friend. – Ryan J Sep 18 '15 at 16:47
  • possible duplicate of [Scanner method to get a char](http://stackoverflow.com/questions/2597841/scanner-method-to-get-a-char) – Evan LaHurd Sep 18 '15 at 16:49

3 Answers3

1

you can do this as alternative:

import java.io.*;
import java.util.*;
public class Test
{
    public static void main(String args[])
    {
        try{
            Scanner kbr = new Scanner(System.in);
            System.out.print("Enter Player's Name: ");
            String n = kbr.readLine();
            //int z = Integer.parseInt();
            char z = n.charAt(0);
            System.out.print(n);
        }
        catch(IOException e){
            System.out.printl("IO ERROR !!!");
            System.exit(-1);
        }
    }
}
Krishna Chalise
  • 147
  • 1
  • 15
0

Something like this?

import java.io.*;
import java.util.*;


class Test
{
    public static void main(String args[])
    {
        Scanner kbr = new Scanner(System.in);
        System.out.print("Enter Player's Name: ");
        String n = kbr.nextLine();
        System.out.print(n);
    }
}
0

If you wants to get only first character of the typed string then

char c = kbr.next().charAt(0);

works fine.
However it allows to input more than single character but as soon as you hit enter,
it will store first character in char c.

For example

import java.io.*;
import java.util.*;

class Test
{
    public static void main(String args[])
    {
        Scanner kbr = new Scanner(System.in);
        System.out.print("Enter Character: ");
        char n = kbr.next().charAt(0);
        System.out.print(n);
    }
}

Input: Hello

Output: H

Digvijaysinh Gohil
  • 1,367
  • 2
  • 15
  • 30