我正在尝试在 Java 中获取多行用户输入并将这些行拆分为一个array,我需要它来解决在线法官的问题。我正在使用 aScanner来接受输入。我无法确定输入的结尾。我总是遇到无限循环,因为我不知道输入的大小(即行数)
用空字符串终止输入(单击 Enter)仍然是一个无限循环。下面提供了代码。
public static void main(String[] args) {
ArrayList<String> in = new ArrayList<String>();
Scanner s = new Scanner(System.in);
while (s.hasNextLine() == true){
in.add(s.nextLine());
//infinite loop
}
}
Run Code Online (Sandbox Code Playgroud)
我什至不确定为什么循环第一次执行。我相信hasNextLine()第一次应该是假的,因为还没有输入任何内容。任何帮助或澄清表示赞赏。
我不断收到以下错误:
//change the package name to calculatorClass///////////////
import java.util.Scanner;
public class assignmentB {
public static String String(int y,int x, String name, String str) throws Exception {
if (y < 1)
throw new Exception("Value must be larger than 0.");
if (x < 1)
throw new Exception("Value must be larger than 0");
return name;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator c1 = new Calculator(); …Run Code Online (Sandbox Code Playgroud) java string compiler-errors switch-statement java.util.scanner
我决定在Java中创建一个货币转换器,并将其转换为从文本文件中提取转换值(以便于编辑,因为这些值会不断变化).我确实通过使用Scanner类并将所有值放入ArrayList来实现.
现在我想知道是否有办法在文本文件中添加注释以供用户阅读,扫描仪将忽略该注释."//"似乎不起作用.
谢谢
我有关于扫描仪的问题;我在一家小公司工作; 我们有一个软件; 它会生成一个大文本文件; 我们必须从中获得一些有用的信息; 我想用java编写一个简单的应用程序来节省时间; 你能指导我吗?
例如,我想要这个输出;
产量
RFID:25 BLUID:562 WifiID:2610 RFID:33
RFID数量:2
例如;这是我的文本文件,因为每个生成的文件与我们的软件有14000行:)
--------------------------
AAAAAAAAAAAA;RFID=25;
BBBB;BBBBBBBB;BBBBBBBBBB;
CCCCC;fffdsfdsfdfsd;BLUID=562;dfsdfsf;
fgfdgdf;terter;fdgfdgtryt;
trtretrre;WifiID=2610;trterytuytutyu;
zxzxzxzxz;popopopwwepp;RFID:33;aasasds…
gfdgfgfd;gfdgfdgfd;fdgfgfgfd;
Run Code Online (Sandbox Code Playgroud)
Scanner scanner = new Scanner("i:\1.txt");
scanner.findInLine("RFID=");
if (scanner.hasNext())
System.out.println(scanner.next());
else
System.out.println("Error!");
Run Code Online (Sandbox Code Playgroud)
请帮我 ;
非常感谢 ...
这可能是一个非常简单的修正,我看不到,但我很确定你们可以帮助我,这部分代码应该读取用户输入1-12(一年中的一个月)并添加一个到数组位置(即如果用户向数组输入3,那么它会将数组中的'space'2增加1来计算出现的数量.),这段代码只是通过而没有任何动作发生,无所事事后,通常的构建成功.
无论如何,我希望有人可以给我一些关于我哪里出错的指示.
import java.util.Scanner;
public class BirthMonth {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int months [] = new int [12];
}
public static int[] inputMonths(int[] months, Scanner input){
System.out.println("please enter the first month with a birthday:");
int month = input.nextInt();
months[month - 1] ++;
//arr[i] = Input.nextInt();
while (month != -1){
System.out.println("please enter the next month to be tallied");
month = input.nextInt();
months[month - 1] ++;
}
return months;
}
}
Run Code Online (Sandbox Code Playgroud) 当我使用这段代码时:
Scanner scan = new Scanner(System.in);
String codeEntered = "";
for ( ; ; ){
System.out.print("Enter Code or Press Q to Quit:\n");
codeEntered = scan.nextLine();
if (codeEntered == "Q"){
break;
}
//Do stuff here.
}
System.out.println("Goodbye!");
Run Code Online (Sandbox Code Playgroud)
即使我输入"Q",我也无法摆脱for循环.这段代码有什么问题?
这可能是一个简单的问题.我在扫描文本文件时遇到问题.我想扫描一个文本文件并在JOptionPane中显示该消息.它扫描但它只显示我的文本文件的第一行,然后停止忽略其他行.如果我能得到一点帮助.非常感谢你!这是我的代码:
File file = new File("src//mytxt.txt");
try{
Scanner input = new Scanner(file);
while(input.hasNext()){
String line = input.nextLine();
JOptionPane.showMessageDialog(null, line);
return;
}
input.close();
}
catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "File not found");
}
}
Run Code Online (Sandbox Code Playgroud) 我试图使用扫描仪从文件读入然后将这些附加到数组.但是当我这样做时,扫描仪似乎正在拾取空行并将这些空白行分配给数组中的索引.
我尝试了几种不同的工作方式无济于事,只是想知道是否有任何问题和新问题为什么会这样做?
该文件的格式如下:
F:\ DATA\SFW3 \文件夹
F:\ DATA\SFW3 \文件夹
F:\ DATA\SFW3 \文件夹
F:\ DATA\SFW3 \文件夹
任何想法都会受到欢迎.
public void scanFiles() throws NoSuchElementException {
Scanner sc = null;
System.out.println("Sage 2015 is Installed on this machine");
int i = 0;
try {
String line;
File companyFile = new File(sageFolders[8] + "\\COMPANY");
sc = new Scanner(new BufferedReader(new FileReader(companyFile)));
while(sc.hasNextLine()) {
line = sc.nextLine();
System.out.println(line);
System.out.println(i);
currentFolders.add(i,line);
System.out.println("At Index" + i + ": " + currentFolders.get(i));
i++;
}
sc.close();
}
catch(FileNotFoundException e) {
System.out.println("File not …Run Code Online (Sandbox Code Playgroud) 我有这个非常简单的代码:
public class ArrayIO {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number from 50 to 150: ");
int elements = sc.nextInt();
while(elements<50 && elements>150) {
System.out.println("Less than 50 or more than 150");
elements = sc.nextInt();
}
sc.close();
ArrayList<Integer> integers = new ArrayList<>();
for(int i=1; i<=elements; i++) {
integers.add(i);
}
integers.toArray();
System.out.printf("Array size is %d\n", integers.size());
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我的程序有多个条件时,我的程序会完全绕过while循环.它只适用于一种情况.我觉得我正在做一些非常愚蠢的错误,但我已经差不多一个小时了.
使用scanner.close()方法后,无法打开其他扫描仪。我已经在Google上搜索了我的问题,仅找到提供解决方案的人。我只想首先理解为什么这是一个问题。谁能解释为什么?
import java.util.Scanner;
public class Main {
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
scan.close();
Scanner scan = new Scanner(System.in);
System.out.println(scan.next()); //NoSuchElementException
}
}
Run Code Online (Sandbox Code Playgroud)
我知道一次打开多个扫描器会导致outOfMemoryError,所以我想为什么不将其作为可以从中调用方法的单例实用工具类?此类会在每次使用时打开和关闭扫描仪,因此流无需保持打开状态。
public class Main {
public static void main(String[] args) {
LinkedList<Scanner> list = new LinkedList<>();
while(true) {
list.add(new Scanner(System.in)); //OutOfMemoryError
}
}
}
Run Code Online (Sandbox Code Playgroud)
这回到了我的问题的根源,如果我们都能理解为什么扫描器类以这种方式工作,也许我们可以提出一个解决方案?