标签: java.util.scanner

java.util.Scanner 读取剩余内容

使用java.util.Scanner实例,是否可以返回所有剩余内容?我的情况是,一旦我读取了 a 中的多个字段String,我只想获取剩下的内容并将其存储在其他地方。例如我的内容可能包括:

1,2,Some Garbage with \' special characters and so on
Run Code Online (Sandbox Code Playgroud)

,我将使用作为分隔符构建扫描仪,调用getInt()两次,然后想"Some Garbage with \' special characters and so on"在一次调用中返回。

java java.util.scanner

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

Java比较字符串与正则表达式 - while循环

我希望用户输入一个字符串,如果该字符串与我的正则表达式不匹配,那么我希望输出一条消息,并且用户再次输入一个值。

问题是,即使字符串与正则表达式匹配,它也会将其视为不匹配。

我的正则表达式:这应该等于 -Name, Name

[[A-Z][a-zA-Z]*,\s[A-Z][a-zA-Z]*]

我的while循环:

  System.out.println("Enter the student's name in the following format - surname, forename: ");  studentName = input.next();
  while (!studentName.equals("[[A-Z][a-zA-Z]*,\\s[A-Z][a-zA-Z]*]")) {
    System.out.println("try again");
    studentName = input.next();
  }
Run Code Online (Sandbox Code Playgroud)

java regex while-loop java.util.scanner

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

Java:在不知道输入行数的情况下读取输入

这可能是非常非常基本的,或者可能是我完全缺少的东西。我已经开始在在线频道上做一些竞争性的节目。我必须读取逗号分隔的字符串并对其进行一些操作,但问题是我不知道输入的行数。下面是输入示例

输入1

John,Jacob
Lesley,Lewis
Remo,Tina
Brute,Force
Run Code Online (Sandbox Code Playgroud)

输入2

Hello,World
Java,Coder
........
........
//more input lines
Alex,Raley
Michael,Ryan
Run Code Online (Sandbox Code Playgroud)

我正在尝试读取输入并在遇到行尾时中断,但没有运气。这就是我一直在尝试的

//1st method
Scanner in = new Scanner(System.in);

do{
    String relation = in.nextLine();
    //do some manipulation
    System.out.println(relation);

}while(in.nextLine().equals(""));   //reads only first line and breaks

//2nd method
Scanner in = new Scanner(System.in);
while(in.hasNext()){
    String relation = in.next();
    System.out.println(relation);
    if(relation.equals("")){
        break;
    }
}

//3rd method
Scanner in = new Scanner(System.in);
while(true){   //infinite loop
    String relation = in.nextLine();
    System.out.println(relation);
    if(relation.equals("")){
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

PS:请不要评判。我是竞争性编程的新手,尽管我知道如何在 java 中获取用户输入以及 …

java input java.util.scanner

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

Java Scanner输入不等于自身?

我正在编写一个循环,当Scanner收到字符串值"end"时它将退出.但是,当使用"结束"值进行测试时,循环继续.逻辑上如果file =输入,那么if(file =="end")为false,即使我输入了!我的代码中是否有明显的错误?

String file = "";
    Scanner in = new Scanner(System.in);
    ArrayList<Integer> fileInput = new ArrayList<Integer>(); 

    while(file!="end") {
        // Scan for filename/end program
        System.out.println("Provide the name of a file in the \"bin/\" folder, i will assume it's .txt");
        file = in.nextLine();

        System.out.println("." + file + ".");
        if(file!="end") {
            file= "bin/" + file + ".txt";

            // start reading
            try {
                // If file found then carry on
                BufferedReader openFile = new BufferedReader(new FileReader(file));
                fileInput = readIn(openFile);
                int lowerBound = getLower(fileInput); …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

2
推荐指数
1
解决办法
1330
查看次数

Java Scanner换行符识别

我找不到默认情况下指定扫描程序如何处理换行模式的文档.我想逐行读取文件,让扫描仪能够处理\ r,\n或\ r \n行结尾,无论程序实际运行的系统如何.

如果我宣布这样的扫描仪:

Scanner scanner = new Scanner(reader);
Run Code Online (Sandbox Code Playgroud)

什么是默认行为?它会处理如上所述的所有三种类型,还是我必须明确告诉它这样做?

java newline java.util.scanner

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

在Java中将十六进制解析为short时的异常

在文件data.hex中,数据以十六进制形式给出,其中十六进制数的第一个数字始终小于8.

01FC 04BF 04C0 04C1 04C2 24C3 04C4 34C5 ...
Run Code Online (Sandbox Code Playgroud)

要解析此文件并将值存储在shrt []数组中,我已编写此代码

void read_hex_short(String filename, short[] shrt, int x, int y) throws Exception
{
    String str;
    Scanner s=new Scanner(new BufferedReader(new FileReader(filename)));
    for(int i=0;i<height*width;i++)
    {
        str= s.next();    // i have tried str="0x"+s.next() but it didn't work
        image[i]=(short)Integer.parseInt(str);
    }
    s.close();
}
Run Code Online (Sandbox Code Playgroud)

但我得到的是NumberFormatException,它在传递第一个字符串时出现,即仅01FC.我如何解析这些十六进制值并将它们存储在shrt []数组中?

java java.util.scanner

2
推荐指数
2
解决办法
1053
查看次数

为什么Double.parseDouble(scan.next())比scan.nextDouble()快得多?

所以我有一个文件可以读入,我知道如何设置数据.例如,我知道每个新行的第一个标记将是双重的.

我一直在使用扫描仪,只是使用scan.nextDouble()来读取double但是我被告知Double.parseDouble(scan.next())而不是加快从文件中读取数据的过程30秒降至约5秒.

scan.nextInt()与Integer.parseInt(scan.next())也是如此.

在我正在阅读的文件中,对于每行大约40,000行,它为int double int int.

那么是什么让它变得如此之快?

java double file-io java.util.scanner

2
推荐指数
2
解决办法
2073
查看次数

java.util.Scanner跳过输入请求

我正在尝试使用java.util.Scanner以下方式接收输入:

Scanner scanner = new Scanner(System.in);
int bla = scanner.nextInt();
String blubb = scanner.nextLine();
Run Code Online (Sandbox Code Playgroud)

但是nextLine()命令只是被跳过并返回一个空字符串.我怎么解决这个问题?

java java.util.scanner

2
推荐指数
1
解决办法
2460
查看次数

扫描仪方法打开和关闭两次

在单个类中,我有两种使用扫描类的方法.我为第一个方法创建了一个新的scanner对象实例,然后在第一个方法结束时将其关闭...然后在第二个方法中创建一个对象的新实例(具有不同的名称),最后在这个方法结束了.

除非我打开扫描仪一次并关闭它一旦它将无法工作并返回错误.扫描仪类的双重用途似乎不起作用.我究竟做错了什么?

这是我的两个返回错误的方法...

public void GameSetup(){
    //intro to the program and what the user should expect to do
    Scanner in = new Scanner(System.in);
    out.println("Let's play a little baseball game. Type down the scores for each team at the end of each inning to determine the winner!" +
            "\nFor each inning enter the score for the first team, hit space and enter the score for the second team.\nFirst, let's enter the team names:");

    //Console will ask for the team names.
    out.println("What …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

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

java.util.NoSuchElementException:找不到行 - 读取用户输入只能工作一次

我试图创建一个非常简单的程序(这不重要,但是一个扫雷游戏),我遇到了以下问题:当我尝试获取用户输入(带Scanner)时,它第一次工作(第一次转弯) ),但在第二个回合,按下后enter,它会引发以下异常:java.util.NoSuchElementException: No line found.

两个回合之间没有任何变化,我Scanner在每个回合都创建了一个新实例.

代码:

public String nextTurn() {
    Scanner scn = new Scanner(System.in);

    System.out.print("Please insert your action: ");
    StringTokenizer input = new StringTokenizer(scn.nextLine());
    scn.close();
    //...
}
Run Code Online (Sandbox Code Playgroud)

同样,它在我第一次调用此方法时有效,但在第二次调用时失败.你知道问题可能是什么吗?

java java.util.scanner

2
推荐指数
1
解决办法
967
查看次数

标签 统计

java ×10

java.util.scanner ×10

double ×1

file-io ×1

input ×1

newline ×1

regex ×1

while-loop ×1