Cucko Oooo
Cucko Oooo

Reputation: 91

Unusual output from using split()

I'm reading from a file that has the following format: name : symptoms : causes : treatments : rate : prognosis

There are a total of 21 entries but when I try to read from the file and use .split(":");, the output changes each time but is always along the lines of: [Ljava.lang.String;@614951ff. I'm guessing it's the pointer or memory address but I want the String value. I'm not getting any exceptions though so I'm not sure where I've gone wrong. The purpose of the method is to read the file and split into an array using the delimiter for the given file row selected.

public String[] readCancer(int row) {
    cancers = new String[22];
    FileInputStream fis;
    InputStreamReader isr;
    BufferedReader br = null;
    String eachCancer;
    String[] splitCancer = null;
    int j = 0;
    try {
        fis = new FileInputStream(myData);
        isr = new InputStreamReader(fis);
        br = new BufferedReader(isr);
        input = new Scanner(br);
        while(input.hasNext() && j < 23) {
            cancers[j++] = input.nextLine();
        }
        eachCancer = cancers[row].toString();
        splitCancer = eachCancer.split(":");
    } catch (IOException iox) {
        JOptionPane.showMessageDialog(null, "Problem with file input");
    } finally {
        try {
            if(br != null) {
                br.close();
            }
        } catch (IOException iox) {
            JOptionPane.showMessageDialog(null, "Problem closing the file");
        }
    }
    return splitCancer;
}

Upvotes: 0

Views: 99

Answers (4)

Cucko Oooo
Cucko Oooo

Reputation: 91

I tried pasting the entire file but it came a horrid block of text, so here are 2 lines from it. Each line has the same format but differing lengths:

The general format of the file is name : symptoms : causes : treatment : rate : prognosis

The rate is a String since it is unknown for some diseases and when it is known, the rate is not always out of 250,000. Sometimes it is out of 1,000,000 or 100,000, etc... .

acute promyelocytic leukemia : easy bruising, rapid internal bleeding, fatigue, anemia, frequent fever, infection, blood clots : PML and RARA genes : Medications, chemotherapy : 1 in 250,000 : Good

familial cylindromatosis : numerous skin tumours, ulcers, infection, impaired eyesight, hearing, smell, balance : CYLD gene : Surgery, chemotherapy : Unknown : Unknown

My most recent code attempt is at Unusual output from using split()

The 2 arrays of cancers and split are private String[] as field variables declared outside any of the methods. The variable myData is a private File also declared as a field variable outside any of the methods. I have checked and already verified the file path is correct.

The main method that calls the method:

public static void main(String[] args) {  
    CancerGUI _gui = new CancerGUI();
    String[] resultCancer;
    resultCancer = _gui.readCancer();
    //System.out.println(resultCancer);
    System.out.println(Arrays.toString(resultCancer));
}

I am only calling it in the main method to test whether it correctly returns the String[]. Once it does, then I will call it in a different method that adds the data to a GUI (this part I am reasonably confident I know how to do and have examples from my instructor and textbook to follow).

Upvotes: 0

Cucko Oooo
Cucko Oooo

Reputation: 91

Currently I have the following:

public String[] readCancer() {
    cancers = new String[22];
    split = new String[22];
    FileInputStream fis;
    InputStreamReader isr;
    BufferedReader br = null;
    String eachCancer;
    int j = 0;
    try {
        fis = new FileInputStream(myData);
        isr = new InputStreamReader(fis);
        br = new BufferedReader(isr);
        input = new Scanner(br);
        while(input.hasNext() && j < 23) {
            cancers[j] = input.nextLine().toString();
            //cancers[j] = input.nextLine();
            split[j] = cancers[j].split(":");
            //split[j] = "blah";    this line works
            j++;
        }
        System.out.println(Arrays.toString(split));
    } catch (IOException iox) {
        JOptionPane.showMessageDialog(null, "Problem with file input");
    } finally {
        try {
            if(br != null) {
                br.close();
            }
        } catch (IOException iox) {
            JOptionPane.showMessageDialog(null, "Problem closing the file");
        }
    }
    return split;
    //return split[j]; does not work
}

In my while loop, I keep getting compile errors saying it requires a String but found Stirng[] for split. When I try something simpler, such as split[j] = "blah";, there are no compile errors. I can return cancers perfectly but I need to split by the : delimiter and that seems to be something I cant get my head around. When I try return split[j], then I get another compile error saying it requires a String[] but found String. I've been at this for more than an hour, read through examples in my textbook and tutorials online for using split but it still isn't working. This is the only part of my program that I'm not sure how to do.

Upvotes: 0

Maroun
Maroun

Reputation: 95968

If you want to display the string array, you should use:

System.out.println(Arrays.toString(splitCancer));

Because when you print splitCancer you'll get the address of the array and not the content of it.

Of course you can print the content in other ways:

for(String str : splitCancer) {
   System.out.println(str);
}

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49372

To print the contents of array :

1) System.out.println(Arrays.deepToString(splitCancer));

2) System.out.println(Arrays.toString(splitCancer));

3) System.out.println(Arrays.asList(splitCancer));

Upvotes: 2

Related Questions