Question

Doing a java assignment in CSE 205 at ASU, and I'm having a hard time understanding parsing. I've looked through our online textbook, and parsing rarely comes up and it's never given a full explanation. I've looked through the java api documentation a few times and I never understand what it's saying, so I hope someone isn't too frustrated as to explain how to do it.

The class is:

BankParser

The BankParser class is a utility class that will be used to create bank objects from a string. The BankParser class cannot be instantiated. It has the following method:

public static Bank bankParser(String lineToParse)

The bankParser method's argument will be a string in the following format:

bankName/bankID/city,state

A real example of this string would be:

Bank Arizona/10001/Phoenix,AZ

The bankParser method will parse this string, pull out the information, create a new bank object, set the attributes of the object, and return it.

So far this is my setup:

public class BankParser {

public static Bank bankParser(String lineToParse) {

}

}

Also, in my Bank class I have this toString method:

    public String toString() {
    String printInfo = ("\nBank name:\t" + bankName + "\nBank ID:\t" + bankID +     "\nBank address:\t" + bankAddress + "\n");
    return printInfo;

It gives me 2 markers in eclipse: that this overrides java.lang.Object.toString, and that the return type is missing. What does this all mean?? The return type is String, I don't see what the problem is with that, but the override I'm clueless

EDIT; This is what I've come up with for bankParser

    public static Bank bankParser(String lineToParse) {
    String[] returnValue = lineToParse.split("/");
    Bank temp = new Bank();
    temp.setbankName(returnValue[0]);
    temp.setbankID(returnValue[1]);
    temp.setbankAddress = (returnValue[2]); //this one won't work, see below
       return temp;
}

}

And THESE are the methods in Bank and Address that apply to bankParser

    public void setBankName(String bank1) {
    bankName = bank1;
}

public void setBankID(String bankID1) {
    bankID = bankID1;
}

public void setBankAddress(String city, String state) {
    bankAddress.setCity(city);
    bankAddress.setState(state);
}

In Address.java:

    public void setCity(String city1) {
    city = city1;
}

public void setState(String state1) {
    state = state1;
}
Was it helpful?

Solution 2

Your bankParser method is empty. It needs to return a Bank object, and Java will complain until you do this. You could always have it return null for now til, make it at least a compilable stub you figure this out:

public static Bank bankParser(String lineToParse) {
   Bank returnValue = null;
   // TODO: create a Bank object, assign to returnValue
   return returnValue;
}

As for your override bit, are you getting an error message? Or a warning? The code you've posted seems kosher, so it should compile. Please show the actual full message.

As for your actual parsing, I'd use String#split("/") to split the lineToParse into an array of tokens and then work with each token, create arguments for a Bank constructor call and create a Bank object.

i.e., code to show the concept:

String text = "Bank Arizona/10001/Phoenix,AZ";

String[] tokens = text.split("/");
System.out.println(java.util.Arrays.toString(tokens));

OTHER TIPS

I would use library like Apache Common CSV for reading and writing.

Reader in = new StringReader("bankName/bankID/city,state");

Iterable<CSVRecord> parser = CSVFormat.newBuilder()
     .withDelimiter('/')
     .parse(in);

for (CSVRecord csvRecord : parse) {
     ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top