-1

I am making a game for my com sci class and it uses console to display user inputs. I want to use the Java Scanner class to receive a text of line from the user however when I try to use the scanner inputs, it doesn't work.

This is the import I use:

import java.util.Scanner; 

this is essentially what I'm trying to do

Scanner keyboard = new Scanner(System.in); // not system.in but something compatible to console

maybe scanner isn't compatible with console and perhaps I need to use another class? Or maybe i'm doing something wrong? Thanks :)

glitch
  • 1
  • 1
  • Define 'does not work'. What, exactly, did you do? What happened? Generally, writing interactive console I/O is not very satisfying in java, but you might do better to read lines and then parse them with Scanner. – bmargulies Jun 04 '17 at 22:05
  • You should look at this post - https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java – Denis Jun 04 '17 at 22:08
  • It says the import "java.util.Scanner" is not valid, since it does not name a type in a package. Haha yeah this is my first time using console and its very annoying. Thanks for the suggestions! – glitch Jun 04 '17 at 22:09

1 Answers1

0

From my understanding of your question, what you need to do is:

Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.NextLine();

This will read the console input from the user.

The import statement is also wrong. It should be:

import java.util.Scanner;

Also as mentioned in the comments above, look at this post: How can I read input from the console using the Scanner class in Java?

maattss
  • 13
  • 4
  • the import was a typo, I actually used what you just stated and it wouldn't work. In the forum that is attached they actually use that same import ? – glitch Jun 04 '17 at 22:26