I am trying to create a JAVA program that reads some tags from a txt file and creates objects that have these tags as fields. I am posting the part of the code that I have the issue :
try{
line = reader.readLine();
while(line != null){
if (!line.trim().equals(" ") && line.trim().equals("ITEM_LIST")) {
line = reader.readLine();
if (line != null && line.trim().equals("{"))) {
line = reader.readLine();
if (line != null) {
if (line.trim().equals("ITEM")) {
line = reader.readLine();
if (line != null) {
if (line.trim().equals("{")) {
if (line != null) {
line = reader.readLine();
while (!((line.trim().startsWith("ITEM_TYPE ")) {
line = reader.readLine();
if((line.trim().startsWith("ITEM_TYPE ")){
String devType = line.trim().substring(10);
}
)
if(devType.toLowerCase() == "tv"){
dev = new TV();
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("CODE ")) {
dev.setCode(line.substring(5).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("MODEL ")) {
dev.setModelName(line.substring(6).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("MODEL_YEAR ")) {
dev.setYear(line.substring(11).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("MANUFACTURER ")) {
dev.setConstructor(line.substring(13).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("PRICE ")) {
dev.setPrice(Double.parseDouble(line.substring(6).trim()));
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("PANEL_TYPE ")) {
dev.setType(line.substring(11).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("DIMENSIONS ")) {
dev.setSize(line.substring(11).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("RESOLUTION ")) {
dev.setResolution(line.substring(10).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("INTERFACES ")) {
dev.setPorts(line.substring(10).trim());
}
}
line = reader.readLine();
if (line != null){
if (line.trim().toUpperCase().startsWith("PIECES ")) {
dev.setPieces(line.substring(7).trim());
}
}
My question is how can I modify the code in order to read the tag ITEM_TYPE
first of all then create the specific item (eg tv) and then read all the other tags and complete the object. The order of the tags in the txt file is not specific and if a tag is missing the field will take a default value. Here is the txt file :
ITEM_LIST
{
ITEM
{
CODE ADLS443
ITEM_TYPE tv
MODEL IDL32KJX4300
MODEL_YEAR 2016
MANUFACTURER Samsung
PRICE 350
PANEL_TYPE LCD
DIMENSIONS 32
RESOLUTION "Full HD"
INTERFACES "HDMI USB SCART"
PIECES 43
}
ITEM
{
CODE KD444211
ITEM_TYPE tv
MODEL KDL49DDR4600
MODEL_YEAR 2014
MANUFACTURER Sony
PRICE 600
PANEL_TYPE LED
DIMENSIONS 49
RESOLUTION "Ultra HD"
INTERFACES "HDMI USB SCART Ethernet WiFi"
PIECES 8
}
ITEM
{
CODE 2445DBS41
ITEM_TYPE blueray
MODEL JVX44R32
MODEL_YEAR 2012
MANUFACTURER LG
PRICE 250
RESOLUTION 1080p
FORMAT BR-W
PIECES 12
}
I'm sorry if any information about the code is missing.
Short answer - you can't.
Better answer - read everything under ITEM
, extract ITEM_TYPE
, create the object you want and fill it with the rest of the data.
Also, you may want to only do line.trim()
once, right after you performed the reader.readLine()
, and generally rewrite the code as a state machine so you won't have that high level of nesting if
s.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.