if(string.contains)在最终输出中混合变量放置

时间:2014-09-04 15:31:34

标签: java string contains

我正在编写一个程序来根据产品标题生成html元数据。所有程序要求(目前)是产品标题和其他2个细节。这是当前不正确的输出:

Please paste model title.

(我的输入:布加迪威龙1:18蓝色)

<METANAME="DESCRIPTION"CONTENT="Shop for Bugatti diecast model cars at [WEBSITE REMOVED]">
<METANAME="ABSTRACT"CONTENT=Shop for diecast model cars at [WEBSITE REMOVED].
<METANAME="KEYWORDS"CONTENT=“diecast cars, diecast quality scale cars,
diecast model cars, model cars, collectible cars, Veyron diecast model, quality diecast   cars, diecast 1:18">

我想要的是:

Please paste model title.

(我的输入:布加迪威龙1:18蓝色)

<METANAME="DESCRIPTION"CONTENT="Shop for Bugatti Veyron 1:18 Blue diecast model cars at [WEBSITE REMOVED]">
<METANAME="ABSTRACT"CONTENT=Shop for diecast model cars at [WEBSITE REMOVED].
<METANAME="KEYWORDS"CONTENT=“diecast cars, diecast 1:18 scale cars,
diecast model cars, model cars, collectible cars, diecast model, Bugatti diecast cars, diecast Veyron">

请注意,作为标题的差异不是标题,只是其中的一个词。比例放在最后而不是它应该的位置等等。

代码以截断的方式避免发布300多行:

import java.util.Scanner;
public class MetagenV3 {

public static void main (String[] args) {

    Scanner sc = new Scanner(System.in);
    String title;
    String abstr = "Shop for diecast model cars at [WEBSITE REMOVED]";
    String scale;
    String diecastManu;
    String metaModel;
    String metaMake;
    String defaultstring = "quality";

    String[] metaMakesList = {"Abarth", "Acura", "Ahrens", "Alfa Romeo"
            +"Alpine Renault", "AMC", "American LaFrance", "Aprilia", "Aston Martin" 
            +"Audi", "Austin", "Austin-Healey", "Bedford", "Benelli", "Bentley", "BMW"
            +"Buffalo", "Bugatti", "Buick","Cadillac","Caterham", "Chaparrel"
            + "Checker","Chevrolet","Chrysler","Citroen", "Cleveland", "Cord","DAF"
            + "Daimler","Dakota","Datsun","Dauer"}; // ETC ETC

    String[] scaleList = {"1:12","1:18","1:24","1:32","1:43","1:64"}; // ETC ETC

    System.out.println("Please paste model title.");
    title = sc.next();

    if (title.toLowerCase().contains(metaMakesList[0].toLowerCase()))
    { metaMake = metaMakesList[0];}
    if (title.toLowerCase().contains(metaMakesList[1].toLowerCase()))
    { metaMake = metaMakesList[1];}
    if (title.toLowerCase().contains(metaMakesList[2].toLowerCase()))
    { metaMake = metaMakesList[2];}
    if (title.toLowerCase().contains(metaMakesList[3].toLowerCase()))
    { metaMake = metaMakesList[3];}
    if (title.toLowerCase().contains(metaMakesList[4].toLowerCase()))
    { metaMake = metaMakesList[4];}
    if (title.toLowerCase().contains(metaMakesList[5].toLowerCase()))
    { metaMake = metaMakesList[5];}
    // ETC, ETC

    else {
        metaMake = defaultstring;
    }

    if (title.toLowerCase().contains(scaleList[0].toLowerCase()))
    { scale = scaleList[0]; }
    if (title.toLowerCase().contains(scaleList[1].toLowerCase()))
    { scale = scaleList[1]; }
    if (title.toLowerCase().contains(scaleList[2].toLowerCase()))
    { scale = scaleList[2]; }
    if (title.toLowerCase().contains(scaleList[3].toLowerCase()))
    { scale = scaleList[3]; }
    // ETC, ETC

    else { scale = defaultstring;}

    System.out.println("Please paste model manufacturer.");
    diecastManu = sc.next();
    System.out.println("Please paste car model (e.g Skyline");
    metaModel = sc.next();


String fullOutput = "<METANAME="  + "\"DESCRIPTION\"" + "CONTENT=\"" + "Shop for " + title + " "
        + "diecast model cars at [WEBSITE REMOVED]\"" +">" + "\n"
            + "<METANAME="  + "\"ABSTRACT\"" + "CONTENT=" + abstr + "\n"
         +  "<METANAME="  + "\"KEYWORDS\"" + "CONTENT=" + "“diecast cars, diecast " + scale + " "
                + "scale cars," + "\n" + "diecast model cars, modelcars, collectable cars, "
         + diecastManu + " diecast"
                        + " model, " + metaMake + " diecast cars, diecast " + metaModel +"\">";

System.out.println(fullOutput);

}

}

另外它也忽略了:

   System.out.println("Please paste model manufacturer.");
        diecastManu = sc.next();
        System.out.println("Please paste car model (e.g Skyline");
        metaModel = sc.next(); 

我知道这个问题很长而且不是很简单,但我想不出更好的方式来展示正在发生的事情。

1 个答案:

答案 0 :(得分:1)

看起来你的问题有多层,但你提到标题的第一个问题是直截了当。

此代码

System.out.println("Please paste model title.");
title = sc.next();

只会读到第一个单词。 Scanner.next()只会根据分隔符(默认为空格)读取第一个标记。

我建议你使用某种调试器(如果你有的话)来浏览代码。如果做不到这一点,你应该添加一些打印语句来帮助看看代码在做什么。

例如,如果添加

System.out.println("Please paste model title.");
title = sc.next();
System.out.println("Title: " + title);

您将立即看到问题是程序没有正确读取输入。

此外,这也解释了为什么它似乎忽略了制造商和汽车模型的提示。它正在从模型标题中读取剩余的标记。

更新(见评论) 最后一个总是触发

if (title.toLowerCase().contains(scaleList[2].toLowerCase()))
{ scale = scaleList[2]; }
if (title.toLowerCase().contains(scaleList[3].toLowerCase()))
{ scale = scaleList[3]; }
else { scale = defaultstring;}

假设scaleList [2]是正确的。它会将scale设置为scaleList [2],然后它将评估下一个if语句。它将是false,检测else块并运行它。构造这些相关if语句的正确方法是

if (title.toLowerCase().contains(scaleList[2].toLowerCase()))
{ scale = scaleList[2]; }
else if (title.toLowerCase().contains(scaleList[3].toLowerCase()))
{ scale = scaleList[3]; }
else { scale = defaultstring;}

这样一旦找到正确的比例,它就会停止尝试匹配比例(这也与你的比赛相关)。