标签: java.util.scanner

Java使用扫描仪输入密钥按下

我正在使用Java编程.
我正在尝试编写代码,可以识别用户是否在基于控制台的程序中按下回车键.

我怎么能用java做到这一点.有人告诉我,这可以使用扫描仪或缓冲输入阅读器完成.我不明白(或知道如何使用)缓冲输入阅读器.

我尝试使用扫描仪这样做,但按两次输入后程序终止,它不起作用

    Scanner readinput = new Scanner(System.in);

    String enterkey = "Hola";
    System.out.print(enterkey);


    enterkey = readinput.nextLine();
     System.out.print(enterkey);

    if(enterkey == ""){

        System.out.println("It works!");
Run Code Online (Sandbox Code Playgroud)

谢谢

- 编辑 - 以下代码使用equals字符串的方法而不是==

    Scanner readinput = new Scanner(System.in);

    String enterkey = "Hola";
    System.out.print(enterkey);


    enterkey = readinput.nextLine();
     System.out.print(enterkey);

    if(enterkey.equals("")){

        System.out.println("It works!");
Run Code Online (Sandbox Code Playgroud)

如何做到这一点,以及使用缓冲输入阅读器做到这一点的优点是什么?

java console input bufferedreader java.util.scanner

20
推荐指数
1
解决办法
10万
查看次数

在String中提取整数部分

提取字符串的整数部分的最佳方法是什么

Hello123
Run Code Online (Sandbox Code Playgroud)

你如何获得123部分.您可以使用Java的Scanner来破解它,有更好的方法吗?

java string parsing integer java.util.scanner

19
推荐指数
3
解决办法
8万
查看次数

如何确定何时到达文件结尾?

我试图从文本文件中读取文本.我需要帮助搞清楚文件结束的时间.如何在Java中确定这一点?

FileInputStream istream = new FileInputStream("\""+filename+"\"");      
Scanner input = new Scanner(istream);
while(EOF != true)
{
 ....
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

java file-io file java.util.scanner

19
推荐指数
2
解决办法
8万
查看次数

Java Scanner字符串输入

我正在编写一个使用Event类的程序,其中包含一个日历实例和一个String类型的描述.创建事件的方法使用扫描程序来获取月,日,年,小时,分钟和描述.我遇到的问题是Scanner.next()方法只返回空格前的第一个单词.因此,如果输入是"我的生日",则该事件实例的描述仅为"我的".

我做了一些研究,发现人们使用Scanner.nextLine()来解决这个问题,但是当我尝试这个时,它只是跳过输入应该去的地方.以下是我的代码的一部分:

System.out.print("Please enter the event description: ");
String input = scan.nextLine();
e.setDescription(input);
System.out.println("Event description" + e.description);
e.time.set(year, month-1, day, hour, min);
addEvent(e);
System.out.println("Event: "+ e.time.getTime());    
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

Please enter the event description: Event description
Event: Thu Mar 22 11:11:48 EDT 2012
Run Code Online (Sandbox Code Playgroud)

它跳过空格以输入描述字符串,因此,描述(最初设置为空格 - "")永远不会更改.

我怎样才能解决这个问题?

java string java.util.scanner

19
推荐指数
1
解决办法
26万
查看次数

Java扫描程序不通过整个文件

我正在用Java编写程序,我需要做的一件事是为最短路径问题创建一组每个有效位置.位置在.txt文件中定义,该文件遵循严格模式(每行一个条目,没有额外的空格),非常适合使用.nextLine来获取数据.我的问题是241行进入文件(432中)扫描仪停止工作3/4通过一个条目并且不识别任何新行.

我的代码:

    //initialize state space
private static Set<String> posible(String posLoc) throws FileNotFoundException {
    Scanner s = new Scanner(new File(posLoc));
    Set<String> result = new TreeSet<String>();
    String availalbe;
    while(s.hasNextLine()) {
        availalbe = s.nextLine();
        result.add(availalbe);
    }
    s.close();
    return result;
}
Run Code Online (Sandbox Code Playgroud)

数据

Shenlong Gundam
Altron Gundam
Tallgee[scanner stops reading here]se
Tallgeese II
Leo (Ground)
Leo (Space)
Run Code Online (Sandbox Code Playgroud)

当然,"扫描仪在此处停止读取"不在数据中,我只是标记扫描仪停止读取文件的位置.这是3068字节到文件中,但这不应该影响任何东西,因为在同一个程序中,几乎相同的代码,我正在读取编码路径的261行,14KB .txt文件.任何帮助,将不胜感激.

谢谢.

java file-io java.util.scanner

17
推荐指数
3
解决办法
2万
查看次数

逐行读取输入

如何在Java中逐行读取输入?我搜索过,到目前为止我有这个:

import java.util.Scanner;

public class MatrixReader {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            System.out.print(input.nextLine());
        }
    }
Run Code Online (Sandbox Code Playgroud)

这个问题是它没有读取最后一行.所以,如果我输入

 10 5 4 20
 11 6 55 3
 9 33 27 16
Run Code Online (Sandbox Code Playgroud)

它的输出只会是

10 5 4 20 11 6 55 3
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

16
推荐指数
2
解决办法
4万
查看次数

Java:使用Scanner in.hasNextInt()的无限循环

我使用以下代码:

while (invalidInput)
{
    // ask the user to specify a number to update the times by
    System.out.print("Specify an integer between 0 and 5: ");

    if (in.hasNextInt())
    {
        // get the update value
        updateValue = in.nextInt();

        // check to see if it was within range
        if (updateValue >= 0 && updateValue <= 5) 
        { 
            invalidInput = false; 
        } 
        else 
        {
            System.out.println("You have not entered a number between 0 and 5. Try again.");
        }
    } else
    {
        System.out.println("You have entered an …
Run Code Online (Sandbox Code Playgroud)

java loops infinite java.util.scanner

15
推荐指数
2
解决办法
4万
查看次数

Java - 在扫描程序中使用多个分隔符

我正在使用扫描仪来获取输入,并希望将其拆分为块.我希望它使用全字分隔符将其拆分.所以现在我有:

    Scanner scanner = new Scanner("1 imported bottle of perfume at 27.99");
    scanner.useDelimiter("\\sdelimitOne\\s");
Run Code Online (Sandbox Code Playgroud)

所以用输入"word word delimitOne word word delimitTwo word word"得到输出:

word word
word word delimitTwo word word
Run Code Online (Sandbox Code Playgroud)

我希望

    scanner.useDelimiter("\\sdelimitOne\\s\\sdelimitTwo\\s");
Run Code Online (Sandbox Code Playgroud)

可能会奏效,但不是.

我如何实现以下输出:

word word
word word
word word
Run Code Online (Sandbox Code Playgroud)

java delimiter java.util.scanner

15
推荐指数
1
解决办法
3万
查看次数

如何使用Java Scanner测试空白行?

我希望用扫描仪输入,直到没有任何东西(即当用户输入空行时).我该如何实现这一目标?

我试过了:

while (scanner.hasNext()) {
    // process input
}
Run Code Online (Sandbox Code Playgroud)

但那会让我陷入困境

java java.util.scanner

15
推荐指数
3
解决办法
8万
查看次数

Java.Util.Scanner的NoSuchElementException

我是Java的新手,但我正在阅读Java:How to program(第9版)这本书并且已经达到了一个例子,对于我的生活,我无法弄清楚问题是什么.

这是教科书中源代码示例的(略微)增强版本:

import java.util.Scanner;
public class Addition {
  public static void main(String[] args) {
    // creates a scanner to obtain input from a command window

    Scanner input = new Scanner(System.in);

    int number1; // first number to add
    int number2; // second number to add
    int sum; // sum of 1 & 2

    System.out.print("Enter First Integer: "); // prompt
    number1 = input.nextInt(); // reads first number inputted by user

    System.out.print("Enter Second Integer: "); // prompt 2 
    number2 = input.nextInt(); …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

15
推荐指数
1
解决办法
7万
查看次数