我使用scanner.delimiter 来分割我的csv,分隔符是“,”。但是,我有一些数据在数据中包含逗号,例如“Monsters, Inc.”。
但是,如果我将分隔符设置为 "\",\"",那么它会在其他所有内容上崩溃。
不需要我编写自己的scanner.delimiter 方法的想法?
我有一台扫描仪,询问一些偏好。它创建一个介于 0 和 3 之间的int变量choice。然后我执行以下操作:
String location;
switch(choice){
case 0:
System.out.println("Please type the zip codes you would like to search in a comma separated list");
location = input.nextLine();
break;
case 1:
System.out.println("Please type the street names you would like to search in a comma separated list");
location = input.nextLine().toUpperCase();
break;
case 2:
System.out.println("Please type the boroughs you would like to search in a comma separated list");
location = input.nextLine().toUpperCase();
break;
default:
location = "none";
break;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个称为文件的文件加载SPY.txt到数组中,但是我什至无法使这个小片段正常工作。
我不明白 如果f.exists是true,扫描仪如何抛出文件找不到异常?
import java.io.*;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
File f = new File (new File("SPY.txt").getAbsolutePath());
System.out.println(f.exists());
Scanner s = new Scanner(f);
}
}
Run Code Online (Sandbox Code Playgroud)
输出: True
线程“主”中的异常java.lang.RuntimeException:无法编译的源代码-未报告的异常java.io.FileNotFoundException; 必须在scanstest.ScannerTest.main(ScannerTest.java:13)处被捕获或声明被抛出
第13行是
Scanner s = new Scanner(f);
我正在通过输入询问电话号码Scanner class而我只是强制执行号码,但我可能会犯这个错误.我需要有一串int,而不是一个int的东西,并且比int的大小容量大.最终,它需要7个整数或10个整数,而不是介于两者之间或更多或更少,但当然所有数字而不是字母.
System.out.println("What is the phone number (digits only)?: ");
while (!Main.scan.hasNextInt())
{
Main.scan.next();
System.out.println("What is the phone number (digits only)?: ");
}
int phone_number = Main.scan.nextInt();
Run Code Online (Sandbox Code Playgroud)
清除整数字符串 - 不是意味着整数数组而是一系列独立的整数但当然如果你指定为一个整数,它就无法区分,所以你必须使用长整数.这就是我所引用的,事实上我的电话号码是一系列的int而不是一个int这么原始的类型对我来说是正确的,一半是错的.
解决方案:感谢Braj !!
String numbers = Main.scan.next();
long phone_number = 0 ;
while (!numbers.matches("(\\d{7}|\\d{10})$"))
{
System.out.println("What is the patient's phone number (digits only)?: ");
numbers = Main.scan.next();
}
phone_number = Long.valueOf(numbers);
Run Code Online (Sandbox Code Playgroud) 这个问题仅用于教育目的。我从一本关于 Java 的教科书中获取了以下代码,并且很好奇为什么在 catch 块中使用 input.nextLine()。
我尝试使用 input.nextInt() 来编写程序。程序将不再正确捕获异常。当我传递一个不是整数的值时,控制台会显示熟悉的“线程中的异常...”错误消息。
当我完全删除那行代码时,控制台将无休止地运行 catch 块的 System.out.println() 表达式。
Scanner.nextLine() 的目的是什么?为什么这些场景中的每一个都有不同的表现?
import java.util.*;
public class InputMismatchExceptionDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueInput = true;
do {
try{
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println(
"The number entered is " + number);
continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Try again. (" +
"Incorrect input: an integer is required)");
input.nextLine();
}
}
while (continueInput);
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行一个简单的输入程序,但在System.out.println命令上出现错误,我不知道为什么它不接受该命令并且在我修复它之前无法继续工作。
错误说:
Multiple markers at this line
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "Identifier (" to complete
MethodHeaderName
- Syntax error on token ".", @ expected after this token
- Syntax error, insert "SimpleName" to complete QualifiedName
Run Code Online (Sandbox Code Playgroud)
我的代码如下。
package classPack;
import java.util.Scanner;
public class Main {
Scanner s = new Scanner(System.in);
int numone = s.nextInt();
System.out.println("please input the number of numbers you want to analyze");
Scanner r = new Scanner(System.in);
int numtwo …Run Code Online (Sandbox Code Playgroud) 我想编写一个接受用户输入的程序。到目前为止我很好。但是用户输入的数量会失控,并且在调试时手动完成是不切实际的。有没有可能让 eclipse 为我做用户输入?如果是,它看起来如何?用户输入是一堆以不同方式分隔的整数。有没有办法继续使用扫描仪,因为我也想测试扫描仪?
我希望在 java 中的一行中读取多个输入。
前任:
System.out.print("Input name, age, address, city: ");
Run Code Online (Sandbox Code Playgroud)
用户将输入这些用空格分隔的详细信息
控制台中的预期内容:
输入姓名、年龄、地址、城市:Tom, 10, USA, NY
使用 Scanner 类知道如何做到这一点。谢谢。
我正在为一个静态方法做扫描仪,并发生这个异常:
Exception in thread "main" java.util.NoSuchElementException: No line found
Run Code Online (Sandbox Code Playgroud)
我的修改方法倾向于从控制台获取 2 个字符串作为输入,但它不起作用。
注意:我没有使用scanner.close();
static ArrayList<Book> modBook(){
Book tempbook = Book.searchTitle();
if(tempbook !=null){
Scanner sc = new Scanner(System.in);
int i = BookList.indexOf(tempbook);
System.out.println("Please enter title:");
String booktitle = sc.nextLine();
System.out.println("Please enter author:");
String bookauthor = sc.nextLine();
tempbook.setTitle(booktitle);
tempbook.setAuthor(bookauthor);
BookList.set(i, tempbook);
}
return BookList;
}
Run Code Online (Sandbox Code Playgroud)
我的搜索方法:
static Book searchTitle(){
try (Scanner input = new Scanner(System.in)) {
String booktitle;
System.out.println("Please enter title:");
booktitle = input.nextLine();
for(Book b : BookList){
if(b.getTitle() != …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
import java.util.Scanner;
public class Maggiore3Valori {
public static void main(String[] args) {
Scanner scanner = new Scanner("System.in");
int num1, num2, num3;
int max;
System.out.println("Inserisci il primo numero: ");
num1 = scanner.nextInt();
System.out.println("Inserisci il secondo numero: ");
num2 = scanner.nextInt();
System.out.println("Inserisci il terzo numero: ");
num3 = scanner.nextInt();
if (num1 > num2 && num1 > num3) {
max = num1;
} else if (num2 > num1 && num2 > num3) {
max = num2;
} else {
max = num3; …Run Code Online (Sandbox Code Playgroud)