我正在用Java做一些事情,要求输入与模式^ [1-5] $匹配.我应该有一个while循环遍历每行输入,根据模式检查它,如果没有,则输出错误消息.
Sudo代码:
while (regex_match(/^[^1-5]$/,inputLine)) {
print ("Please enter a number between 1 and 5! ");
getNextInputLine();
}
Run Code Online (Sandbox Code Playgroud)
我可以使用java.util.Scanner.hasMatch("^[^1-5]$"),但这只会匹配一个令牌,而不是整行.关于如何使hasMatch与整条线匹配的任何想法?(将分隔符设置为"\n"或"\ 0"不起作用.)
编辑:如果这不可能,还有其他方法吗?
我在本实验中的任务是接受多个输入文件,并且所有这些文件的格式都相似,只是有些文件有注释,我想跳过注释行.例如:
输入文件:
Input file 1
#comment: next 5 lines are are for to be placed in an array
blah 1
blah 2
blah 3
blah 4
blah 5
#comment: next 2 line are to be placed in a different array
blah 1
blah 2
#end of input file 1
Run Code Online (Sandbox Code Playgroud)
我尝试做的是我使用2循环(如果需要,我可以发布我的代码).我做了以下
while(s.hasNext()) {
while(!(s.nextLine().startWith("#")) {
//for loop used to put in array
array[i] = s.nextLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得这应该有效,但事实并非如此.我做错了什么 请帮忙.先感谢您.
出于某种原因,当我尝试使用带有gwt的扫描仪时,我收到以下错误:
No source code is available for type java.util.Scanner; did you forget to inherit a required module?
Run Code Online (Sandbox Code Playgroud)
我环顾四周,似乎"没有源代码可用于类型xxxx"错误是由于没有Java类型的Javascript等效类型.扫描仪无法与GWT一起使用吗?
这是我的代码片段:
import java.util.Scanner;
...
public void submit(){
String text = editor.getEditor().getText();
Scanner input = new Scanner(text);
while(input.hasNextLine()){
String line = input.nextLine();
if(line.contains("//")){
cInfo.setDone(false);
cInfo.setCode(text);
return;
}
cInfo.setDone(true);
cInfo.setCode(text);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个文本文件,内容如下:
Description|SKU|Retail Price|Discount
Tassimo T46 Home Brewing System|43-0439-6|17999|0.30
Moto Precise Fit Rear Wiper Blade|0210919|799|0.0
Run Code Online (Sandbox Code Playgroud)
我已经得到它以便我阅读所有内容,并且它完美地工作,除了它读取第一行的事实,这是.txt文件的一种传说,必须被忽略.
public static List<Item> read(File file) throws ApplicationException {
Scanner scanner = null;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
throw new ApplicationException(e);
}
List<Item> items = new ArrayList<Item>();
try {
while (scanner.hasNext()) {
String row = scanner.nextLine();
String[] elements = row.split("\\|");
if (elements.length != 4) {
throw new ApplicationException(String.format(
"Expected 4 elements but got %d", elements.length));
}
try {
items.add(new Item(elements[0], …Run Code Online (Sandbox Code Playgroud) 我有一个文件:
12345678;ABC 123456A12345678;45678945
Run Code Online (Sandbox Code Playgroud)
这就是我做的:
Scanner s = new Scanner(new File(testCase.getFileName()));
while (s.hasNext()) {
String[] lineItems = s.next().split(";");
}
Run Code Online (Sandbox Code Playgroud)
输出:
12345678;ABC
123456A12345678;
45678945
Run Code Online (Sandbox Code Playgroud)
期望的输出:
12345678
ABC 123456A12345678
45678945
Run Code Online (Sandbox Code Playgroud)
我希望它考虑"ABC 123456A12345678"作为一个单一的标记,而不是在遇到空格时中断.
我该怎么办?
我试图将一个字符串拆分成一个字符串数组,但是当它拆分字符串时,只有第一部分,在拆分之前,在[0]插槽中的数组中,但是[1]或更晚的内容中没有任何内容.这也会在尝试输出拼接时返回java异常错误[1]
import java.util.Scanner;
public class splittingString
{
static String s;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the length and units with a space between them");
s = input.next();
String[] spliced = s.split("\\s+");
System.out.println("You have entered " + spliced[0] + " in the units of" + spliced[1]);
}
}
Run Code Online (Sandbox Code Playgroud) 我观察到,Scanner当用于文件读取工作时,两个参数:File和FileInputStream.
Scanner scan = new Scanner(new File("myfile.txt"));
Run Code Online (Sandbox Code Playgroud)
和
Scanner scan = new Scanner(new FileInputStream("myfile.txt"));
Run Code Online (Sandbox Code Playgroud)
但是,我不知道这两个定义之间的区别.是否有任何与性能相关的差异?哪一个更喜欢?
有人请解释一下.谢谢.
所以,我正在开发一个从命令行开始的测验计划,测验有时间限制.我想做的是,即使用户正在回答问题,也要在用户的时间到了之后立即停止测验.我正在使用Java的Scanner来获取用户的输入,所以我想基本上告诉Scanner对象即使它正在接受输入的中间也要终止.
现在,我知道我可以追溯惩罚用户在事后经历一段时间,但我只是希望测验在超过时限后终止.有没有办法用多线程来做到这一点?
我有一个像这样的csv文件
Mike,Smith
Scuba,Steve
John,Doe
Run Code Online (Sandbox Code Playgroud)
和java代码一样:
Scanner file=new Scanner(new File("input.txt"));
file.useDelimiter(",");
while (file.hasNext()){
String s1=file.next();
String s2=file.next();
System.out.println(s1+" "+s2);
}
file.close();
Run Code Online (Sandbox Code Playgroud)
我得到输出:
Mike Smith
Scuba
Steve
John Doe
Run Code Online (Sandbox Code Playgroud)
我不明白什么可能使这个工作在前两个名称而不是中间名称
我正在阅读的文件String后跟int一行,然后继续,直到它到达这个字符:*
例如:
A 1300
B 1200
C 1100
D 1000
*
Run Code Online (Sandbox Code Playgroud)
我需要它来停止读取文件,并在它到达星星时停止循环.当我执行if语句时,它没有读入*,它将按预期跳到下一行,但这会导致与读入的下一个标记的对齐问题.
这是我的代码:
static GradingScale reportCard = new GradingScale();
public static void main(String[] args) {
readInputFile();
javax.swing.JOptionPane.showMessageDialog(null,reportCard.toString());
}
public static void readInputFile(){
try{
Scanner file = new Scanner(new File("grades.txt"));
int maxPoints = file.nextInt();
reportCard.setMaxPoints(maxPoints);
while(!file.hasNext("*")){
String grade = file.next();
int points = file.nextInt();
double percent = points/(maxPoints*1.0);
reportCard.addGradeLevel(grade, points, percent);
}
}
catch(java.io.FileNotFoundException fnfe){
javax.swing.JOptionPane.showMessageDialog(null, "File not found");
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用该hasNext(Pattern)方法,但我不断收到此错误: …