我搜索过类似的问题,但没有人帮忙.
考虑一个文件:
你好你好吗?
当时你在哪里?
我希望在每一行结束后进行一些操作.如果我使用next()它,当我到达第一行的末尾时不会告诉我.
我也见过,hasNextLine()但它只告诉我是否存在另一条线.
根据Java API Scanner使用分隔符将整个输入分解为令牌.我试图理解令牌和分隔符.我正在做这个程序并且遇到了困惑
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = null;
try {
s = new Scanner(System.in);
s.useDelimiter("A");
System.out.println("1 " + s.next().length());
System.out.println("2 " + s.next().length());
System.out.println("3 " + s.next().length());
System.out.println("4 " + s.next().length());
} finally {
if (s != null) {
s.close();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用输入时,AAAAAasdf我得到以下输出.
1 0
2 0
3 0
4 0
Run Code Online (Sandbox Code Playgroud)
我可以理解这个输出,因为在分隔符之间令牌的长度为零,因此所有都是零但是当我使用默认分隔符并将输入作为
_____aaa\n- >用空格替换下划线,然后\n按下进入eclipse控制台.
为此,我得到输出为
1 3
Run Code Online (Sandbox Code Playgroud)
我无法理解.我给了5个空格,所以它们之间应该有4个长度为0的标记.为什么不?我在这里错过了什么?
我正在学习Java并且正在开展一些有趣的项目.我遇到的一个问题是,当我使用一个Scanner对象时,Eclipse警告我:
资源泄漏:'扫描'永远不会关闭.
所以,我scan.close();在代码的末尾添加了一个并处理警告.
问题来自因为我在同一个包中也有其他类也使用了扫描程序对象,并且Eclipse告诉我分别关闭这些类中的扫描程序.但是,当我这样做时,它似乎关闭所有扫描仪对象,并在运行时出现错误.
以下是导致错误的原因示例:
import java.util.Scanner;
public class test2 {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int test = 0;
do {
//Do stuff
test = scan.nextInt();
System.out.println(test);
scanTest scanTest = new scanTest();
scanTest.test();
} while (test != 0);
scan.close();
}
}
import java.util.Scanner;
public class scanTest {
public void test() {
Scanner scanner = new Scanner(System.in);
int blah = scanner.nextInt();
System.out.println(blah);
scanner.close();
}
}
Run Code Online (Sandbox Code Playgroud)
在scanTest类中关闭扫描仪并test2 …
这是我读取文本文件的代码.当我运行此代码时,输出会一直显示"找不到文件.",这是消息FileNotFoundException.我不确定这段代码中的问题是什么.
显然这是java的一部分.对于整个java文件,它要求用户输入内容并使用输入作为名称创建文本文件.之后,用户应该再次输入之前创建的文本文件的名称(假设用户输入正确),然后程序应该读取文本文件.我已正确完成程序的其他部分,但问题是当我再次输入名称时,它只是找不到文本文件,尽管它们位于同一文件夹中.
public static ArrayList<DogShop> readFile()
{
try
{ // The name of the file which we will read from
String filename = "a.txt";
// Prepare to read from the file, using a Scanner object
File file = new File(filename);
Scanner in = new Scanner(file);
ArrayList<DogShop> shops = new ArrayList<DogShop>();
// Read each line until end of file is reached
while (in.hasNextLine())
{
// Read an entire line, which contains all the details for 1 account
String line …Run Code Online (Sandbox Code Playgroud) 为什么此代码抛出InputMismatchException?
Scanner scanner = new Scanner("hello world");
System.out.println(scanner.next("hello\\s*world"));
Run Code Online (Sandbox Code Playgroud)
在http://regexpal.com/中使用相同的正则表达式匹配(使用\ s代替\\ s)
我正在使用Eclipse来编译和运行我的java代码.
这是我得到的错误.
Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at helloworld.main(helloworld.java:9)
Run Code Online (Sandbox Code Playgroud)
这是我的代码
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class helloworld {
public static void main(String[] args) throws IOException {
Scanner KB = new Scanner(new File("file.txt"));
while (KB.hasNext()) {
String line = KB.nextLine();
System.out.println(line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
File.txt
我在项目的同一文件夹中创建了file.txt.
我的Java分配有问题.我得到了一个意想不到的例外,特别是:
java.util.NoSuchElementException:找不到行
我正在使用Scanner(System.in),程序不断读取任何内容并重复"无效格式"异常文本.如果我输入正确值int,第一部分通过,然后该double部分立即进入此异常.如果我输入的值不正确int,则会开始循环异常.
这是我的代码:
import java.util.Scanner;
public class Program_4 {
public static void main(String[] args) {
getValidInt("Enter an integer from 5 to 50",5,50);
getValidDouble("Enter a double from 5.0 to 50.0",5.0,50.0);
getValidString("Enter a string with length from 5 to 8 characters",5,8);
}
public static int getInt(String prompt)
{
Scanner sc = new Scanner(System.in);
int i = 0;
boolean isValid;
do
{
try
{
System.out.print(prompt + ": ");
i = Integer.parseInt(sc.nextLine());
isValid = true; …Run Code Online (Sandbox Code Playgroud) 直接从这个 Scanner API:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
Run Code Online (Sandbox Code Playgroud) 我必须在while循环中显示Scanner输入:用户必须插入输入,直到他写"退出".所以,我必须验证每个输入以检查他是否写"退出".我怎样才能做到这一点?
while (!scanner.nextLine().equals("quit")) {
System.out.println("Insert question code:");
String question = scanner.nextLine();
System.out.println("Insert answer code:");
String answer = scanner.nextLine();
service.storeResults(question, answer); // This stores given inputs on db
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.如何验证每个用户输入?
Scanner input = new Scanner(System.in);
Run Code Online (Sandbox Code Playgroud)
您能否详细解释上面的代码是如何逐步完成的?我真的不明白它是如何工作的以及它之后如何能够做到这一点:
int i = input.nextInt()
Run Code Online (Sandbox Code Playgroud)