简体   繁体   English

初级编码器 (Java) - 验证字符串输入并在输入正确输入后结束循环

[英]Beginning coder (Java) - validating String input and ending a loop once correct input has been entered

The idea with this question is that the user knows to input the sentence "Robin came to Montreal, Canada in 2009.".这个问题的想法是用户知道输入句子“Robin came to Montreal, Canada in 2009.”。 From there, the code should spit back "Robin stays in Montreal, for 11 years. Montreal is in Canada. Please enter the input sentence (press q to exit):".从那里,代码应该吐回“罗宾留在蒙特利尔,11 年。蒙特利尔在加拿大。请输入输入语句(按 q 退出):”。

I've managed to get a loop going so that as long as the user is inputting the sentence correctly the program will also spit back the correct phrase.我已经设法让一个循环继续下去,这样只要用户输入的句子正确,程序也会吐出正确的短语。 The part that I'm struggling with is ending the loop - getting "q" to terminate the program.我正在努力的部分是结束循环——让“q”终止程序。 Instead I get an error.相反,我得到一个错误。 Please let me know what I can do.请让我知道我能做什么。

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MiniTranslator {

public static void main(String[] args) {
    
    Scanner scan = new Scanner(System.in);
    String sentence;
    
    do {
        System.out.println("Please enter the input sentence (press q to exit)");
        sentence = scan.nextLine();
        int i = sentence.indexOf(' ');
        String word = sentence.substring(0, i);
        String sentence2 = sentence.substring(i + 9, sentence.length());
        int j = sentence2.indexOf(' ');
        String word2 = sentence2.substring(0, j);
        Matcher matcher = Pattern.compile("\\d+").matcher(sentence);
        matcher.find();
        int k = Integer.valueOf(matcher.group());
        String sentence3 = sentence2.substring(j + 1, sentence2.length());
        int l = sentence3.indexOf(' ');
        String word3 = sentence3.substring(0, l);
        System.out.println(word + " stays in " + word2 + "  for " + (2020 - k) + " years. " + word2 + " is in " + word3 + ".");
        System.out.println(" ");
        
    } while (!"q".equals(sentence));
    
    do {
        System.out.println("Thanks for using the translator program.");
        System.out.println(" ");
        System.out.println("The program is now terminated");
    } while ("q".equals(sentence));

First of all you should probably share you error message so community members can better understand your problem.首先,您可能应该分享您的错误消息,以便社区成员可以更好地理解您的问题。 But n.netheless your code should be while(.sentence.equals("q")) , since you basically want to call the.equals() method, and you need to create a type String Object for that.但是 n.netheless 你的代码应该是while(.sentence.equals("q")) ,因为你基本上想调用 the.equals() 方法,你需要为此创建一个类型 String Object。 "q" is just a String expression that can be passed into the.equals() method. “q”只是一个可以传递给.equals() 方法的字符串表达式。

The part that I'm struggling with is ending the loop - getting "q" to terminate the program.我正在努力的部分是结束循环——让“q”终止程序。 Instead I get an error.相反,我得到一个错误。 Please let me know what I can do.请让我知道我能做什么。

I have solved the part where you were struggling, but your program logic is incorrect and you need to work on that.我已经解决了您遇到的问题,但是您的程序逻辑不正确,您需要继续努力。 For that learn aboutString class and its functions.为此,请了解String class及其函数。

Anyway, here is your program press q to exit code无论如何,这是您的程序按q退出代码

    Scanner scan = new Scanner(System.in);
String sentence;
boolean doAgain = true;
do 
{
    System.out.println("Please enter the input sentence (press q to exit)");
    sentence = scan.nextLine();
    if("q".equals(sentence))
    {
        doAgain = false;
    }
    else
    {
        //your program logic in here
        
        int i = sentence.indexOf(' ');
        String word = sentence.substring(0, i);
        String sentence2 = sentence.substring(i + 9, sentence.length());
        int j = sentence2.indexOf(' ');
        String word2 = sentence2.substring(0, j);
        Matcher matcher = Pattern.compile("\\d+").matcher(sentence);
        matcher.find();
        int k = Integer.valueOf(matcher.group());
        String sentence3 = sentence2.substring(j + 1, sentence2.length());
        int l = sentence3.indexOf(' ');
        String word3 = sentence3.substring(0, l);
        System.out.println(word + " stays in " + word2 + "  for " + (2020 - k) + " years. " + word2 + " is in " + word3 + ".");
        System.out.println(" ");
        
    }
} while (doAgain);

System.out.println("Thanks for using the translator program.");
System.out.println(" ");
System.out.println("The program is now terminated");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM