Java:无限循环中的扫描仪输入
Java: Scanner input in an infinite loop
我目前正在尝试制作一个简单的程序来测试字符串中的某些字符是否在它们应该在的位置。例如,正常 mm/dd/yy 格式中的 '/'。但出于某种原因,我什至无法通过扫描仪输入,因为它一直在请求输入。考虑到这只是一个独立的扫描仪,我已经四处搜索,可以找到任何人遇到我遇到的确切问题。我是 Java 的新手,所以我认为这是我的一个简单错误,但我似乎无法弄清楚,不想再浪费时间了。
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");
}
}
}
这是我的控制台的样子:
console img
我目前正在尝试制作一个简单的程序来测试字符串中的某些字符是否在它们应该在的位置。例如,正常 mm/dd/yy 格式中的 '/'。但出于某种原因,我什至无法通过扫描仪输入,因为它一直在请求输入。考虑到这只是一个独立的扫描仪,我已经四处搜索,可以找到任何人遇到我遇到的确切问题。我是 Java 的新手,所以我认为这是我的一个简单错误,但我似乎无法弄清楚,不想再浪费时间了。
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");
}
}
}
这是我的控制台的样子: console img