我正在为一个Z80模拟器编写一个调试器,我们正在学校项目中使用Java编写.调试器从用户读取命令,执行它,读取另一个命令等.
命令可以是参数较少,具有可选参数,也可以是无限量的参数.参数主要是整数,但偶尔它们是字符串.
目前,我们正在使用Scanner类来读取和解析输入.阅读方法看起来有点像这样(我写的是我的头脑,没有注意语法和正确性).
这是在项目开始时编写的一个kludge,随着我们向调试器添加越来越多的命令,这很快就失控了.
我对这段代码的主要问题是大量的重复,if/else-nestedness的高级别以及丑陋的一切.
我想建议如何使这个代码更美观和模块化,以及什么样的模式适合这种程序.
我还想了解更多有关代码风格的一般建议.
有人可以给我一个提示,为什么这个尝试和捕获不起作用?它会抛出扫描程序异常,而不是打印我期望的消息.
import java.util.*;
import java.io.*;
import java.math.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
Boolean test = true;
while (test == true) {
try {
double x, y;
String operator;
Scanner scan = new Scanner(System.in);
Scanner scan_2 = new Scanner(System.in);
Scanner ScanOperator = new Scanner(System.in);
System.out.println(" Enter a double value: ");
x = scan.nextDouble();
System.out.println(" Enter another double value: ");
y = scan_2.nextDouble();
System.out.println(" Enter a operator for the operation you want to execute, or …Run Code Online (Sandbox Code Playgroud) java exception-handling exception try-catch java.util.scanner
嘿伙计们,我正在尝试useDelimiter在它的Scanner类上使用Java的方法来做一些简单的解析.基本上每行都是由"|"分隔的记录,例如:
2 | John Doe
3 | Jane Doe
4 | Jackie Chan
Run Code Online (Sandbox Code Playgroud)
该方法将正则表达式作为参数,以便与之匹配.有人可以请我提供匹配的正则表达式|(两边用一个空格分隔的垂直条).
谢谢,我真的很感激!
我正在使用一个多线程环境,一个Thread通过反复调用不断地监听用户输入scanner.nextLine().要结束应用程序,此runloop会被另一个线程停止,但监听线程将不会停止,直到最后一个用户输入(由于阻塞性质nextLine()).
关闭流似乎不是一个选项,因为我正在读取System.in,它返回一个InputStream不可关闭的.
有没有办法打断扫描仪的阻塞,以便它会返回?
谢谢
这个程序实际上如何工作......?
import java.util.Scanner;
class string
{
public static void main(String a[]){
int a;
String s;
Scanner scan = new Scanner(System.in);
System.out.println("enter a no");
a = scan.nextInt();
System.out.println("no is ="+a);
System.out.println("enter a string");
s = scan.nextLine();
System.out.println("string is="+s);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
enter the no
1234
no is 1234
enter a string
string is= //why is it not allowing me to enter a string here?
Run Code Online (Sandbox Code Playgroud) 我试图使用扫描仪从键盘获取一个int,但我收到以下错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at TableReader.mainMenu(TableReader.java:122)
at TableReader.main(TableReader.java:76)
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的.它独立于我的其余程序,我不明白为什么这不起作用.如果有帮助的话,它会在while循环中调用的方法中声明.
// scan for selection
Scanner s = new Scanner(System.in);
int choice = s.nextInt(); // error occurs at this line
s.close();
Run Code Online (Sandbox Code Playgroud)
我介绍了调试器并将错误缩小到:
Java运行时环境检测到致命错误:在pc = 0xb6bdc8a8处为SIGSEGV(0xb),pid = 5587,tid = 1828186944
JRE版本:7.0_07-b30 Java VM:OpenJDK服务器VM(23.2-b09混合模式linux-x86)有问题的框架:V [libjvm.so + 0x4258a8] java_lang_String :: utf8_length(oopDesc*)+ 0x58
无法编写核心转储.核心转储已被禁用.要启用核心转储,请在再次启动Java之前尝试"ulimit -c unlimited"
我正在研究一个JAVA赋值应该处理多行输入.指令读作"输入从标准输入读取".
给出了一个示例输入示例:
one 1
two 2
three 3
Run Code Online (Sandbox Code Playgroud)
我不明白上面的示例输入"从stdin读取"是什么意思.
这是我写的一个测试程序,它隔离了我的困惑:
import java.io.*;
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner stdin = new Scanner(System.in);
while(stdin.hasNextLine())
{
String line = stdin.nextLine();
String[] tokens = line.split(" ");
System.out.println(Integer.parseInt(tokens[1]));
}
}
Run Code Online (Sandbox Code Playgroud)
当我在控制台中运行这个程序时,它会等待我的输入,每当我输入一行时,它就像我期望的那样回复它.所以我想也许上面的样本输入可以通过以这种方式输入3行中的每一行来实现.但是,似乎没有办法结束这一进程.输入3行后,如何终止输入?我尝试按两次输入,但这似乎只是一行只包含换行符,这会导致错误,因为该行不符合它所期望的2令牌格式.
这是控制台交互的样子:
javac Test.java
java Test
one 1
1
two 2
2
three 3
3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Test.main(Test.java:13)
Run Code Online (Sandbox Code Playgroud)
我非常感谢你指出我理解中的差距.
每当我运行它,该chooseCave()功能与in.nextInt().当我选择洞穴时,消息会以2秒的间隔弹出,然后一旦超过该部分,它就会给我错误:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Dragon.main(Dragon.java:81)
Run Code Online (Sandbox Code Playgroud)
我曾尝试hasNextLine()和hasNextInt(),当我使用while hasNextLine()的main方法,我得到一吨多的错误.当我while hasNextInt()在chooseCave()方法中使用时,它不接受我的输入.
当我if hasNextInt()在chooseCave()方法中使用时,它不接受我对playAgain字符串的输入,并直接进入另一个游戏,但随后hasNextInt()布尔返回false并且无限地发送"哪个洞穴......".
我已经完成了错误报告以及类似问题的Java-docs和Stack Overflow.请帮忙.
import java.util.Scanner;
public class Dragon {
public static void displayIntro() {
System.out.println("You are in a land full of dragons. In front of you, ");
System.out.println("You see two caves. In one cave, the dragon …Run Code Online (Sandbox Code Playgroud) 我一直在使用Java的BufferedWriter来写一个文件来解析一些输入.但是,当我打开文件后,似乎添加了空字符.我尝试将编码指定为"US-ASCII"和"UTF8",但我得到了相同的结果.这是我的代码片段:
Scanner fileScanner = new Scanner(original);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "US-ASCII"));
while(fileScanner.hasNextLine())
{
String next = fileScanner.nextLine();
next = next.replaceAll(".*\\x0C", ""); //remove up to ^L
out.write(next);
out.newLine();
}
out.flush();
out.close();
Run Code Online (Sandbox Code Playgroud)
也许问题甚至不是BufferedWriter?
我把它缩小到这个代码块,因为如果我将它注释掉,输出文件中就没有空字符.如果我在VIM中进行正则表达式替换,则该文件为null-character free(:%s /.*^ L // g).
如果您需要更多信息,请与我们联系.
谢谢!
编辑: 正常线的hexdump看起来像:0000000 5349 2a41 3030 202a
但是当运行此代码时,hexdump看起来像:0000000 5330 2a49 4130 202a
我不确定为什么事情变得混乱了.
编辑: 此外,即使文件与正则表达式不匹配并贯穿该代码块,它也会出现空字符.
编辑: 这是差异的前几行的十六进制:http: //pastie.org/pastes/8964701/text
命令是:diff -y testfile.hexdump expectedoutput.hexdump
其余的线条与最后两条线条不同.
我有这样的格式化文本:
x.i9j11k2d1" index="603" value="0"/>
x.i9j11k2d2" index="604" value="0"/>
x.i9j11k2d3" index="605" value="0"/>
x.i10j1k1d1" index="606" value="-0"/>
Run Code Online (Sandbox Code Playgroud)
而且,我对仅扫描数字感兴趣.例如:
int i,j,k,d,index,value;
Run Code Online (Sandbox Code Playgroud)
对于我想要的第一行:
i=9, j=11, k=2, d=1, index=603, value=0
Run Code Online (Sandbox Code Playgroud)
为此,我使用了以下代码:
Scanner file=new Scanner(new File("C:/sol.txt"));
while(file.hasNext())
{
System.out.println("");
int i=0;
int j=0;
int k=0;
int d=0;
file.useDelimiter("");
while(!(file.hasNextInt()))
{
System.out.print(file.next());
}
//System.out.print(file.next());
file.useDelimiter("j");
i=file.nextInt();
System.out.print(i);
file.useDelimiter("");
System.out.print(file.next()); //j
file.useDelimiter("k");
j=file.nextInt();
System.out.print(j);
file.useDelimiter("");
System.out.print(file.next()); //k
k=file.nextInt();
System.out.print(k);
System.out.print(file.next()); //d
d=file.nextInt();
System.out.print(d);
while(!(file.hasNextInt()))
{
System.out.print(file.next());
}
file.useDelimiter("\"");
int index=file.nextInt();
System.out.print(index);
file.useDelimiter("");
while(!(file.hasNextInt()))
{
System.out.print(file.next());
}
int value=file.nextInt();
System.out.print(value);
System.out.print(file.nextLine()); …Run Code Online (Sandbox Code Playgroud) java ×10
exception ×2
command-line ×1
delimiter ×1
interrupt ×1
parsing ×1
regex ×1
stdin ×1
try-catch ×1
user-input ×1