Java:无限循环中的扫描仪输入
我目前正在尝试制作一个简单的程序来测试字符串中的某些字符是否在它们应该在的位置。例如,正常的mm/dd/yy格式中的“/”。但由于某些原因,我甚至无法通过扫描仪输入,因为它只是不断要求输入。我四处搜索,可以找到任何人与我的确切问题,考虑到这是发生在一个独立的扫描仪。我对Java非常陌生,所以我认为这对我来说是一个简单的错误,但我似乎无法理解,我不想再浪费时间了Java:无限循环中的扫描仪输入,java,java.util.scanner,infinite-loop,Java,Java.util.scanner,Infinite Loop,我目前正在尝试制作一个简单的程序来测试字符串中的某些字符是否在它们应该在的位置。例如,正常的mm/dd/yy格式中的“/”。但由于某些原因,我甚至无法通过扫描仪输入,因为它只是不断要求输入。我四处搜索,可以找到任何人与我的确切问题,考虑到这是发生在一个独立的扫描仪。我对Java非常陌生,所以我认为这对我来说是一个简单的错误,但我似乎无法理解,我不想再浪费时间了 import java.util.Scanner; public class Main { public static Ch
import java.util.Scanner;
public class Main {
public static Character req = '/';
public static boolean datecheck(String d){
String hold = d;
String[] a = new String[1];
Character one = hold.charAt(2);
Character two = hold.charAt(5);
//check to make sure the "/" are where they need to be
if(one.equals(req)&&two.equals(req)){
return true;
}
else
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("welcome to the BrightSide Scheduler "
+ "\nplease input the day you wish to schedule:");
String date = input.next();
input.close();
try{
datecheck(date);
}catch (NullPointerException e){
System.out.println("didnt work");
}
if (datecheck(date)==true){
System.out.print("Success");
}
else{
System.out.println("fail");
}
}
}
在提出这个问题之前,我已经编辑掉了一些我在代码中对原始问题的注释,以节省一些麻烦。它位于下面的代码块中。一个LinkedList的导入和一大块注释,我决定在到达程序的那一部分之前暂时不使用它们。很明显,删除所有这些额外的内容可以让我的程序正常工作,但我不完全确定为什么 下面是重新创建的问题,我只是通过编辑我们的注释和导入修复了自己的问题
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
/*public class Date {
private String day;
private String month;
private String year;
public Date(String day, String month, String year){
this.day = day;
this.month = month;
this.year = year;
}
public String getDay() {
return day;
}
public String getMonth(){
return month;
}
public String getYear(){
return year;
}
//String[] options = new String[15];
}*/
public static Character req = '/';
public static boolean datecheck(String d){
String hold = d;
String[] a = new String[1];
Character one = hold.charAt(2);
Character two = hold.charAt(5);
//take user input an search through the string at to make sure it is
//formated correctly, then return true to continue the scheduler
for(int x = -1; x<5; x = x+ 3){
while(x != -1){
int n = 0;
a[n] = hold.substring(x,x+1);
n++;
}
}
//check to make sure the "/" are where they need to be
if(one.equals(req)&&two.equals(req)){
return true;
}
else
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("welcome to the BrightSide Scheduler "
+ "\nplease input the day you wish to schedule:");
String date = input.next();
input.close();
try{
datecheck(date);
}catch (NullPointerException e){
System.out.println("didnt work");
}
if (datecheck(date)==true){
System.out.print("Success");
}
else{
System.out.println("fail");
}
}
}
import java.util.LinkedList;
导入java.util.Scanner;
公共班机{
/*公课日期{
私人弦日;
私人串月;
私人弦年;
公共日期(字符串日、字符串月、字符串年){
this.day=天;
本月=月;
今年=年;
}
公共字符串getDay(){
回归日;
}
公共字符串getMonth(){
返回月份;
}
公共字符串getYear(){
回归年;
}
//字符串[]选项=新字符串[15];
}*/
公共静态字符req='/';
公共静态布尔日期检查(字符串d){
字符串保持=d;
字符串[]a=新字符串[1];
字符1=保持字符(2);
字符二=保持字符(5);
//使用用户输入搜索位于的字符串,以确保
//格式正确,然后返回true以继续调度程序
对于(int x=-1;x当我运行此代码时,没有无限循环需要澄清,在您键入内容并点击enter/return添加新行后,扫描仪只需等待更多输入,而不是打印“未工作”、“成功”或“失败”。无需关闭扫描仪
。您可以删除输入。close();
input.next()
正在等待一个令牌,即它正在等待的不是空格或换行符的内容。只需按enter键是不行的。如果这是您想要的,请将代码更改为input.nextLine()
请阅读并相应增强您的问题。