英文:
I'm stuck on this java code using strings and characters. I cant get past the first input
问题
// Standard import for the Scanner class
import java.util.*;
public class postalCode {
public static void main (String [] args) {
// Create a Scanner object attached to the keyboard
Scanner input = new Scanner (System.in);
System.out.print("Enter a postal code: ");
String postalCode = input.nextLine();
while (!postalCode.contains("exit")) {
if (postalCode.length() > 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() < 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() == 6) {
for (int i = 0; i < postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(2))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(4))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isLetter(postalCode.charAt(1))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(3))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(5))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
else {
System.out.println("Postal code is Valid!");
break;
}
}
}
}
}
}
英文:
QUESTION: Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of L9L9L9 where:
L is a letter
9 is a digit
Your program should continue accepting postal codes until the user enters the word “exit”.
Sample run (user input is shown in bold underline):
Enter a postal code: T2T-3X7
Postal code wrong length, format L9L9L9
Enter a postal code: T2T3AA
Postal code has letters where there are supposed to be digits
Enter a postal code: T2T358
Postal code has digits where there are supposed to be letters
Enter a postal code: T2T3A8
Postal code is valid!
Enter a postal code: exit
MY CODE:
// Standard import for the Scanner class
import java.util.*;
public class postalCode {
public static void main (String [] args) {
// Create a Scanner object attached to the keyboard
Scanner input = new Scanner (System.in);
System.out.print("Enter a postal code: ");
String postalCode = input.nextLine();
while (!postalCode.contains("exit")) {
if (postalCode.length() > 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() < 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() == 6) {
for (int i = 0; i < postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(2))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(4))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isLetter(postalCode.charAt(1))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(3))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(5))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
else {
System.out.println("Postal code is Valid!");
break;
}
}
}
}
}
}
答案1
得分: 1
在第三个 if{ }
代码块之后,您需要添加 postalCode = input.nextLine();
以便下次可以从用户那里获取输入。因为缺少这部分,您无法在第一次输入后继续执行。
while (!postalCode.contains("exit")) {
if (postalCode.length() > 6) {
System.out.println("邮政编码长度错误,格式应为 L9L9L9");
}
if (postalCode.length() < 6) {
System.out.println("邮政编码长度错误,格式应为 L9L9L9");
}
if (postalCode.length() == 6) {
for (int i = 0; i < postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))) {
System.out.println("邮政编码有数字,但应为字母");
break;
}
if (Character.isDigit(postalCode.charAt(2))) {
System.out.println("邮政编码有数字,但应为字母");
break;
}
if (Character.isDigit(postalCode.charAt(4))) {
System.out.println("邮政编码有数字,但应为字母");
break;
}
if (Character.isLetter(postalCode.charAt(1))) {
System.out.println("邮政编码有字母,但应为数字");
break;
}
if (Character.isLetter(postalCode.charAt(3))) {
System.out.println("邮政编码有字母,但应为数字");
break;
}
if (Character.isLetter(postalCode.charAt(5))) {
System.out.println("邮政编码有字母,但应为数字");
break;
} else {
System.out.println("邮政编码有效!");
break;
}
}
postalCode = input.nextLine();
}
}
英文:
After your third if{ }
block you need to add postalCode = input.nextLine();
so that you can take the input from user next time. Because of this you are not able to proceed after first input.
while (!postalCode.contains("exit")) {
if (postalCode.length() > 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() < 6){
System.out.println("Postal code wrong length, format L9L9L9");
}
if (postalCode.length() == 6) {
for (int i = 0; i < postalCode.length(); i++) {
if (Character.isDigit(postalCode.charAt(0))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(2))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isDigit(postalCode.charAt(4))){
System.out.println("Postal code has digits where there are supposed to be letters");
break;
}
if (Character.isLetter(postalCode.charAt(1))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(3))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
if (Character.isLetter(postalCode.charAt(5))){
System.out.println("Postal code has letters where there are supposed to be digits");
break;
}
else {
System.out.println("Postal code is Valid!");
break;
}
}
postalCode = input.nextLine();
}
}
答案2
得分: 0
你必须在循环内更新postalCode
。
然后,最好将所有不同的检查提取到单独的方法中,而不是多次打印相同的结果。
public class Foo {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Enter a postal code: ");
String postalCode = scan.nextLine();
if ("exit".equalsIgnoreCase(postalCode))
break;
if (postalCode.length() != 6)
System.err.println("Postal code wrong length, format L9L9L9");
else if (!hasDigits(postalCode))
System.err.println("Postal code has digits where there are supposed to be letters");
else if (!hasLetters(postalCode))
System.err.println("Postal code has letters where there are supposed to be digits");
else
System.out.println("Postal code is Valid!");
}
}
private static boolean hasDigits(String postalCode) {
for (int i = 1; i < postalCode.length(); i += 2)
if (!Character.isDigit(postalCode.charAt(i)))
return false;
return true;
}
private static boolean hasLetters(String postalCode) {
for (int i = 0; i < postalCode.length(); i += 2)
if (!Character.isLetter(postalCode.charAt(i)))
return false;
return true;
}
}
英文:
You have to update postalCode
inside a loop.
Then it's better to extract all different checks to the separate methods instead of printing same result multiple times.
public class Foo {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Enter a postal code: ");
String postalCode = scan.nextLine();
if ("exit".equalsIgnoreCase(postalCode))
break;
if (postalCode.length() != 6)
System.err.println("Postal code wrong length, format L9L9L9");
else if (!hasDigits(postalCode))
System.err.println("Postal code has digits where there are supposed to be letters");
else if (!hasLetters(postalCode))
System.err.println("Postal code has letters where there are supposed to be digits");
else
System.out.println("Postal code is Valid!");
}
}
private static boolean hasDigits(String postalCode) {
for (int i = 1; i < postalCode.length(); i += 2)
if (!Character.isDigit(postalCode.charAt(i)))
return false;
return true;
}
private static boolean hasLetters(String postalCode) {
for (int i = 0; i < postalCode.length(); i += 2)
if (!Character.isLetter(postalCode.charAt(i)))
return false;
return true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论