在 JAVA 中将字符串作为输入时出现问题
Problem in taking a string as an Input in JAVA
我正在尝试创建一个程序,将广告 IP 地址作为输入,并给出 Class、netID 和 hostID 作为输出。
我在程序中将 IP 地址作为字符串输入时遇到问题。
This is the snap of the error i am getting
错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 0
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3756)
at java.base/java.lang.String.substring(String.java:1902)
at cnPrac6.classFromDecimal(cnPrac6.java:7)
at cnPrac6.main(cnPrac6.java:94)
下面是我遇到错误的部分代码:
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String ipClassD = "";
String ipClassB = "";
System.out.print("In which category you address is: \n 1. Decimal \n 2. Binary \n");
int choice = sc.nextInt();
System.out.print("Enter your address: ");
String str= sc.nextLine();
System.out.print("\n");
switch (choice) {
case 1:
ipClassD = classFromDecimal(str);
System.out.println("Given IP address belings to Class "+ipClassD);
seprate(str,ipClassD);
break;
下面是完整代码
import java.util.*;
public class cnPrac6 {
static String classFromDecimal(String str){
int index = str.indexOf('.');
String ipsub = str.substring(0,index);
int ip = Integer.parseInt(ipsub);
if (ip>=1 && ip<=126)
return "A";
else if (ip>=128 && ip<=191)
return "B";
else if (ip>=192 && ip<223)
return "C";
else if (ip >=224 && ip<=239)
return "D";
else
return "E";
}
static String classFromBinary(String str){
if (str.charAt(0)=='0')
return "A";
else if (str.charAt(0)=='1' && str.charAt(1)=='0')
return "B";
else if (str.charAt(0)=='1' && str.charAt(1)=='1' && str.charAt(2)=='0')
return "C";
else if (str.charAt(0)=='1' && str.charAt(1)=='1' && str.charAt(2)=='1' && str.charAt(3)=='0')
return "D";
else if (str.charAt(0)=='1' && str.charAt(1)=='1' && str.charAt(2)=='1' && str.charAt(3)=='1' && str.charAt(4)=='1')
return "E";
else return "Error wrong address";
}
static void seprate(String str, String ipClass){
String network = "", host = "";
if(ipClass == "A"){
int index = str.indexOf('.');
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "B"){
int index = -1;
int dot = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='.'){
dot +=1;
if(dot==2){
index = i;
break;
}
}
}
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "C"){
int index = -1;
int dot = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='.'){
dot +=1;
if(dot==3){
index = i;
break;
}
}
}
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "D" || ipClass == "E"){
System.out.println("No network or host ID");
return;
}
System.out.println("Network ID is "+network);
System.out.println("Host ID is "+host);
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String ipClassD = "";
String ipClassB = "";
System.out.print("In which category you address is: \n 1. Decimal \n 2. Binary \n");
int choice = sc.nextInt();
System.out.print("Enter your address: ");
String str= sc.nextLine();
System.out.print("\n");
switch (choice) {
case 1:
ipClassD = classFromDecimal(str);
System.out.println("Given IP address belings to Class "+ipClassD);
seprate(str,ipClassD);
break;
case 2:
ipClassB = classFromBinary(str);
System.out.println("Given IP address belings to Class "+ipClassB);
seprate(str,ipClassB);
break;
default:
System.out.println("Enter correct option");
}
}
}
问题在于获取 input.When 您正在使用 sc.nextInt()
,它需要一个整数输入,直到遇到 space.If 它遇到 space,它停止扫描输入。
扫描整数后,扫描仪仍然在同一行,直到我们读取整行才会转到下一行。
所以,得到整数后,使用sc.nextLine()
扫描整行输入.
在这里,您可以找到代码。
int choice = sc.nextInt(); // getting integer
sc.nextLine(); // passing this line
System.out.print("Enter your address: "); // scanner is in next Line
String str= sc.nextLine(); // reading the ip address
它失败的原因是代码 String str= sc.nextLine()
正在消耗来自先前输入的悬挂换行符(当您按 Enter 时)。对于之前的输入,您使用的是 sc.nextInt()
,它使用整数值但不使用换行符。
替换
int choice = sc.nextInt();
与
int choice = Integer.parseInt(sc.nextLine());
我正在尝试创建一个程序,将广告 IP 地址作为输入,并给出 Class、netID 和 hostID 作为输出。 我在程序中将 IP 地址作为字符串输入时遇到问题。
This is the snap of the error i am getting
错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 0
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3756)
at java.base/java.lang.String.substring(String.java:1902)
at cnPrac6.classFromDecimal(cnPrac6.java:7)
at cnPrac6.main(cnPrac6.java:94)
下面是我遇到错误的部分代码:
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String ipClassD = "";
String ipClassB = "";
System.out.print("In which category you address is: \n 1. Decimal \n 2. Binary \n");
int choice = sc.nextInt();
System.out.print("Enter your address: ");
String str= sc.nextLine();
System.out.print("\n");
switch (choice) {
case 1:
ipClassD = classFromDecimal(str);
System.out.println("Given IP address belings to Class "+ipClassD);
seprate(str,ipClassD);
break;
下面是完整代码
import java.util.*;
public class cnPrac6 {
static String classFromDecimal(String str){
int index = str.indexOf('.');
String ipsub = str.substring(0,index);
int ip = Integer.parseInt(ipsub);
if (ip>=1 && ip<=126)
return "A";
else if (ip>=128 && ip<=191)
return "B";
else if (ip>=192 && ip<223)
return "C";
else if (ip >=224 && ip<=239)
return "D";
else
return "E";
}
static String classFromBinary(String str){
if (str.charAt(0)=='0')
return "A";
else if (str.charAt(0)=='1' && str.charAt(1)=='0')
return "B";
else if (str.charAt(0)=='1' && str.charAt(1)=='1' && str.charAt(2)=='0')
return "C";
else if (str.charAt(0)=='1' && str.charAt(1)=='1' && str.charAt(2)=='1' && str.charAt(3)=='0')
return "D";
else if (str.charAt(0)=='1' && str.charAt(1)=='1' && str.charAt(2)=='1' && str.charAt(3)=='1' && str.charAt(4)=='1')
return "E";
else return "Error wrong address";
}
static void seprate(String str, String ipClass){
String network = "", host = "";
if(ipClass == "A"){
int index = str.indexOf('.');
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "B"){
int index = -1;
int dot = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='.'){
dot +=1;
if(dot==2){
index = i;
break;
}
}
}
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "C"){
int index = -1;
int dot = 0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='.'){
dot +=1;
if(dot==3){
index = i;
break;
}
}
}
network = str.substring(0,index);
host = str.substring(index+1,str.length());
}
else if(ipClass == "D" || ipClass == "E"){
System.out.println("No network or host ID");
return;
}
System.out.println("Network ID is "+network);
System.out.println("Host ID is "+host);
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String ipClassD = "";
String ipClassB = "";
System.out.print("In which category you address is: \n 1. Decimal \n 2. Binary \n");
int choice = sc.nextInt();
System.out.print("Enter your address: ");
String str= sc.nextLine();
System.out.print("\n");
switch (choice) {
case 1:
ipClassD = classFromDecimal(str);
System.out.println("Given IP address belings to Class "+ipClassD);
seprate(str,ipClassD);
break;
case 2:
ipClassB = classFromBinary(str);
System.out.println("Given IP address belings to Class "+ipClassB);
seprate(str,ipClassB);
break;
default:
System.out.println("Enter correct option");
}
}
}
问题在于获取 input.When 您正在使用 sc.nextInt()
,它需要一个整数输入,直到遇到 space.If 它遇到 space,它停止扫描输入。
扫描整数后,扫描仪仍然在同一行,直到我们读取整行才会转到下一行。
所以,得到整数后,使用sc.nextLine()
扫描整行输入.
在这里,您可以找到代码。
int choice = sc.nextInt(); // getting integer
sc.nextLine(); // passing this line
System.out.print("Enter your address: "); // scanner is in next Line
String str= sc.nextLine(); // reading the ip address
它失败的原因是代码 String str= sc.nextLine()
正在消耗来自先前输入的悬挂换行符(当您按 Enter 时)。对于之前的输入,您使用的是 sc.nextInt()
,它使用整数值但不使用换行符。
替换
int choice = sc.nextInt();
与
int choice = Integer.parseInt(sc.nextLine());