Java String在Scanner中的Boolean.parseBoolean之前替换多个字符串?

时间:2017-12-12 07:35:13

标签: java string replace java.util.scanner

我正在构建一个跟踪用户肠道功能的java程序。我在循环中有一段代码,通过扫描器查询用户是否有抽筋。我有以下代码:

String cramps = userInput.nextLine();
String replacecramps = cramps.replace("Y","true");
boolean mycramps = Boolean.parseBoolean(replacecramps);

这实际上采用“Y”答案并用“true”替换它,然后将字符串“true”并将其更改为布尔值true(我认为 - 无论如何它似乎都是这样工作的)。但是,我想改变其他可能的常见输入,例如“y”,“yes”,“Yes”,“yes”,“Yeah”,“yep”,“Yep”等。

有没有办法做到这一点?我是Java的新手,所以事实证明这很困难。我在这里读过关于它的每一篇文章,我仍然对如何最好地解决这个问题感到失望。

以下是一些可能有用的代码段:

这个循环的分支是(如果需要,我可以粘贴完整的代码,但它很长):

} else if (myBristol == 7) {
   System.out.println("You appear to have severe diarrhea. ");
    //Integrate Diarrhea.java class
    System.out.println("\nDid you experience cramps or bloating? (Y/N)");
    String cramps = userInput.nextLine();
    String replacecramps = cramps.replace("Y","true");
    boolean mycramps = Boolean.parseBoolean(replacecramps);
    System.out.println("Did you experience flatulence? (Y/N)");
    String gas = userInput.nextLine();
    String replacegas = gas.replace("Y","true");
    boolean mygas = Boolean.parseBoolean(replacegas);
    Diarrhea b = new Diarrhea(poopColor, poopSize, mycramps, mygas);
    //Print data to log file
    pw.println(b.toString());

它正在使用的子类:

public class Poop{
    protected String color;
    protected String size;

    public Poop(String poopColor, String poopSize)
    {
    color=poopColor;
    size=poopSize; 
    }
    public void setcolor(String c)
    {
        color = c;
    }
    public String getcolor(){
        return color;
    }
    public void setsize(String s)
    {
        size = s;
    }
    public String getsize(){
        return size;
    }
    @Override
    public String toString() {
        String tmp = "This poop's color was: " + this.getcolor() + ". Poop was: " + this.getsize() + ".";
        return tmp;
    }

}

public class Diarrhea extends Poop{
protected boolean cramps;
protected boolean gas;

public Diarrhea(String poopColor, String poopSize, boolean cramps, boolean gas) {
super(poopColor,poopSize);
this.cramps=cramps;
this.gas=gas;
}
public void setcramps(boolean mycramps) {
    cramps=mycramps;
}
public boolean getcramps(){
    return cramps;
}
public void setgas(boolean gas) {
    gas=this.gas;
}
public boolean getgas(){
    return gas;
}
@Override
public String toString() {
    String tmp = "This diarrhea's color was: " + this.getcolor() + ". Was this a little or a lot of diarrhea: " + this.getsize() + ". Did you experience cramps or bloating? (true/false) " + this.cramps + ". Did you experience flatulence? (true/false) " + this.gas + ".";
    return tmp;
}

}

这是我一直在管理我的y / n问题的方法,直到这一点,它一直很好地工作,因为它只查找ay或n作为第一个字符,不关心什么情况,并且不关心关于其他任何事情(如拼写错误)。要弄清楚如何使它适用于布尔值有点困难,但我认为我走的是正确的道路 - 但也许我说这一切都错了。

char answer1 = userInput.next().charAt(0);
    answerString = Character.toString(answer1);
    userInput.nextLine(); 
     if (answerString.equalsIgnoreCase("Y")) {
        //Integrate Poop.java class
        System.out.println("Was this a large, medium, small, or average sized bowel movement (describe size): ");
        String poopSize = userInput.nextLine();
        System.out.println("How would you describe the color of this poop (brown, greenish, light, dark, etc): ");
        String poopColor = userInput.nextLine();
        Poop a = new Poop(poopColor, poopSize);
        //Print data to log file
        pw.println(a.toString());

2 个答案:

答案 0 :(得分:1)

你可以使用replaceALL这样的正则表达式:

String input = "you are saying : y yes Yes yeah Yeah yep Yep";
input = input.replaceAll("\\b(?i)y(es|eah|ep)?\\b", "true");

System.out.println(input); //you are saying : true true true true true true true

有关正则表达式的详细信息:

  • \b Word Boundaries
  • (?i)不区分大小写
  • y(es|eah|ep)? y后跟其中一个(eseahep)可选?

答案 1 :(得分:0)

尝试使用不区分大小写的替换所有使用您要定位的正面词汇列表:

String target = "Dogs Yes yes Yeah yeah Yep yep false";
target = target.replaceAll("(?i)\\b(y|yes|yeah|yep)\\b", "true");
System.out.println(target);

Dogs true true true true true true false

Demo