Scanner Method to Get a Char

Take a char input from the Scanner

You could take the first character from Scanner.next:

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

To consume exactly one character you could use:

char c = reader.findInLine(".").charAt(0);

To consume strictly one character you could use:

char c = reader.next(".").charAt(0);

Scanner method to get a char

To get a char from a Scanner, you can use the findInLine method.

    Scanner sc = new Scanner("abc");
char ch = sc.findInLine(".").charAt(0);
System.out.println(ch); // prints "a"
System.out.println(sc.next()); // prints "bc"

If you need a bunch of char from a Scanner, then it may be more convenient to (perhaps temporarily) change the delimiter to the empty string. This will make next() returns a length-1 string every time.

    Scanner sc = new Scanner("abc");
sc.useDelimiter("");
while (sc.hasNext()) {
System.out.println(sc.next());
} // prints "a", "b", "c"

Getting a character as input from user using Scanner(System.in)

You can use Scanner and the findInLine method:

Scanner inputTaker = new Scanner(System.in);
String input = inputTaker.nextLine();
Scanner scanner = new Scanner(input);
char someCharacter = scanner.findInLine(".").charAt(0);
// Do something with character

More here: Scanner method to get a char

What is the proper way to get a char input(using Scanner) inside a loop in Java?

The NullPointerException is being thrown as Scanner.findInLine() can return null if it does not find the requested String/Pattern. The code invokes the method charAt(0) on the null String. Changing the code to the following will resolve this:

Scanner myInput=new Scanner(System.in);
char ans;

System.out.print("Do you have numbers to compute?");
ans=myInput.findInLine(".").charAt(0);

while ((ans=='Y')||(ans=='y'))
{
// Code omitted

System.out.print("Compute another?");
myInput.nextLine();
String s = myInput.findInLine(".");
ans = (null == s) ? 'n' : s.charAt(0);
}

How do you check if the user input is a single Char

You could use the nextLine() method to read the user input, regardless of being one or more characters. Then, once you've acquired the text, check whether its length is equals to 1 or not.

public static void main(String[] args) {
try (Scanner kbd = new Scanner(System.in)) {
System.out.println("Enter an item");
String input = kbd.nextLine();
if (input.length() == 1) {
System.out.println("True");
} else {
System.out.println("False");
}
}
}

EDIT: For some reason, it does not incorporate the link properly in the text. I'll leave the link to the Scanner documentation here:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

java.util.scanner with chars

It's always nice to have sample file data to see what's in being read because at this point...I would only be guessing. If the file content looks like this:

xx xxxxxxx
xx xxxxxx
xxx xxxxxx
xxxx xx
xxxxx x xx
xxxx x x
xxxx xxx x
xxxxxx x
xxxxx xxx
xxxxx xxxx

then you don't need a char[] array, you can just store each line into the ArrayList which would then be an ArrayList of String (ArrayList<String>). Now it's just a matter of printing each element within the array list to the console window. I just don't know, so...we'll just assume it's like the above but we'll take each line and convert it to a char[] array because you seem to want to do this with Characters. With this in mind...

What needs to be done:

  1. Read the file line by line.
  2. Take each line read in and convert it to a char array.
  3. Take this array an add it to the mazeArray ArrayList.
  4. Print the contents of the ArrayList (mazeArray) to the console
    window.
// ArrayList to hold the Maze from file.
ArrayList<char[]> mazeArray = new ArrayList<>();

// `Try With Resources` used here to auto-close reader and free resources.
try (Scanner fileReader = new Scanner(new File("maze.txt"))) {

// String variable to hold each line read in
String line;
// Loop through the whole file line by line...
while (fileReader.hasNextLine()) {

// Task 1: Apply current line read to the `line` variable.
line = fileReader.nextLine().trim(); // Remove any leading/trailing spaces as well.

// Make sure the line isn't blank (if any). You don't want those.
if (line.isEmpty()) {
continue; // Go to the top of loop and read in next line.
}

// Task #2: Convert the String in the variable `line` to a char[] array.
char[] chars = line.toCharArray();

// Task #3: Add the `chars` character array to the `mazeArray` ArrayList.
mazeArray.add(chars);
}
}
catch (FileNotFoundException ex) {
Logger.getLogger(yourClassName.class.getName()).log(Level.SEVERE, null, ex);
}

/* Task #4: Print the maze contained within the `mazeArray` ArrayList
to the Console Window. */
// Outer loop to get each char[] array within the ArrayList.
for (char[] characters : mazeArray) {
// Inner loop to get each character within the current char[] array.
for (char c : characters) {
System.out.print(c); //Print each character on the same console row.
}
System.out.println(); // Start a new line in console.
}

Reading char with scanner from txt file and adding to a 2d array

Scanner.next() always returns a String. Please check https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html for more information. Before performing any operation on scan.next() e.g. scan.next().charAt(0), you should check if (scan.hasNext()) condition.



Related Topics



Leave a reply



Submit