Reputation: 138
I'm writing a program to generate html metadata based on product title. All the program asks for (at the moment) is the product title and 2 other details. This is the current incorrect output:
Please paste model title.
(MY INPUT: Bugatti Veyron 1:18 Blue)
<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">
What I would want is:
Please paste model title.
(MY INPUT: Bugatti Veyron 1:18 Blue)
<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">
Note the differences being the title isn't the title, just one word out of it. The scale is put at the end instead of where it should be, etc.
The code, in a truncated fashion to avoid posting 300+ lines is:
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);
}
}
Additionally it also ignores:
System.out.println("Please paste model manufacturer.");
diecastManu = sc.next();
System.out.println("Please paste car model (e.g Skyline");
metaModel = sc.next();
I know this question is long and not very simple, but I can't think of a better way to show what's going on.
Upvotes: 0
Views: 78
Reputation: 6854
It looks like there are multiple layers of your question, but the first issue you mention of the title is straight forward.
This code
System.out.println("Please paste model title.");
title = sc.next();
will only read in the first word. Scanner.next() will only read the first token based on the delimiter (space by default).
I would recommend you walk through the code with a debugger of some sort (if you have one). Failing that, you should add some print statements to help see what the code is doing.
For example, if you add
System.out.println("Please paste model title.");
title = sc.next();
System.out.println("Title: " + title);
You will immediately see the issue is that the program is not reading the input correctly.
Additionally, this explains why it is appearing to ignore the manufacturer and car model prompts. It is reading the remaining tokens from the model title.
UPDATE (see comment) The final else is always triggering
if (title.toLowerCase().contains(scaleList[2].toLowerCase()))
{ scale = scaleList[2]; }
if (title.toLowerCase().contains(scaleList[3].toLowerCase()))
{ scale = scaleList[3]; }
else { scale = defaultstring;}
Suppose scaleList[2] is correct. It will set scale to scaleList[2], then it will evaluate the next if statement. It will be false, detect the else block and run that. The correct way to structure these related if statements is
if (title.toLowerCase().contains(scaleList[2].toLowerCase()))
{ scale = scaleList[2]; }
else if (title.toLowerCase().contains(scaleList[3].toLowerCase()))
{ scale = scaleList[3]; }
else { scale = defaultstring;}
This way as soon as it finds the correct scale, it will stop attempting to match scales (this is also relevant for your make).
Upvotes: 1