0

Hi all, could anyone tell me how to read a unorder file into order output, I wrote a method that only read order file(begin with product). The following is my code.

My input file like this:

product Apples
location 10 Downing Street
London
England
quantity 382764


location Akihabara
Tokyo
Japan (East)
product Transistor Radios


quantity 10000 cases
location Woolies Supermarket
product Weetbix flakes
Marrackville
Sydney NSW Australia

Thanks in advance.

public static ArrayList getData(String fileName) {
ArrayList<Product> recordList = new ArrayList<Product>();
int index = -1;
try {
File file = new File(fileName);
Scanner reader = new Scanner(file);
String s;
Product p = null;
boolean locationActive = false;
while (reader.hasNext()) {
s = reader.nextLine();
Scanner line = new Scanner(s);
String cmd;
if (line.hasNext()) {
cmd = line.next();
if (cmd.equalsIgnoreCase("product")) {
index++;
p = new Product();
p.setName(line.nextLine());
recordList.add(index, p);
locationActive = false;
} else if (cmd.equalsIgnoreCase("location")) {
p.setLocation(line.nextLine());
recordList.set(index, p);
locationActive = true;
} else if (cmd.equalsIgnoreCase("quantity")) {
if (line.hasNextInt()) {
p.setQuantity(line.nextInt());
recordList.set(index, p);
}
locationActive = false;
} else if (locationActive) {
String location = p.getLocation() + " " + s;
p.setLocation(location);
recordList.set(index, p);
} else
System.out.println("Error: no command:" + s);
}
}
reader.close();
return recordList;
} catch (Exception e) {
System.out.println("Error:- " + e.getMessage());
return null;
}
}

Edited by newjavastudent: n/a

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by quuba
0
public static List getData(String fileName) {
        boolean DEBUG = false;
        List<Product> recordList = new ArrayList<Product>();
        try {
            File file = new File(fileName);
            Scanner reader = new Scanner(file);
            String s;
            while (reader.hasNext()) {
                s = reader.nextLine();
                if (DEBUG) {
                    System.out.println("(" + s + ")");
                }
                if (s.isEmpty()) {
                    System.out.println("-------- DETECTED EMPTY LINE!");
                }
                Scanner line = new Scanner(s);
                String cmd;
                if (line.hasNext()) {
                    cmd = line.next();
                    if (DEBUG) {
                        System.out.println("[" + cmd + "]");
                    }
                    if (cmd.equalsIgnoreCase("product")) {
                        System.out.println("DETECTED PRODUCT");
                    } else if (cmd.equalsIgnoreCase("location")) {
                        System.out.println("DETECTED LOCATION");
                    } else if (cmd.equalsIgnoreCase("quantity")) {
                        System.out.println("DETECTED QUANTITY");
                    } else {
                        System.out.println("DETECTED I DO NOT KNOW WHAT THIS IS?");
                    }
                }
            }
            reader.close();
            return recordList;
        } catch (Exception e) {
            System.out.println("Error:- " + e.getMessage());
            return null;
        }
    }

run:
DETECTED PRODUCT
DETECTED LOCATION
DETECTED I DO NOT KNOW WHAT THIS IS?
DETECTED I DO NOT KNOW WHAT THIS IS?
DETECTED QUANTITY
-------- DETECTED EMPTY LINE!
-------- DETECTED EMPTY LINE!
DETECTED LOCATION
DETECTED I DO NOT KNOW WHAT THIS IS?
DETECTED I DO NOT KNOW WHAT THIS IS?
DETECTED PRODUCT
-------- DETECTED EMPTY LINE!
-------- DETECTED EMPTY LINE!
DETECTED QUANTITY
DETECTED LOCATION
DETECTED PRODUCT
DETECTED I DO NOT KNOW WHAT THIS IS?
DETECTED I DO NOT KNOW WHAT THIS IS?
BUILD SUCCESSFUL (total time: 1 second)

class Product:
Temporarily make all fields in Product as String type.
Add field for unknown values and method set
Write method String toString()
method getData:
answer
-where and when create new instance of Product?
-when Product instance is ready to add to the List?
-when Product fields are not set?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.