I have been having a little trouble getting my program to output the results I am wanting.

At first I was having trouble with a cast exception at this line:

out = (ParseResult[]) ipHostsFound.toArray();

The cast exception was:

[Ljava.lang.Object; cannot be cast to [Lfilelocation.struct.ParseResult;

So I ended up changing it to this:

out = (ParseResult[])ipHostsFound.toArray(new ParseResult[ipHostsFound.size()]);

However, I don't want the size of it, i just changed it to that to see if I could get it to work correctly with the output as I was briefly reading some information here:

http://stackoverflow.com/questions/395030/quick-java-question-casting-an-array-of-objects-into-an-array-of-my-intended-cla

Here is the source (if you need the ParseResult source I can post it also):

import filelocation.struct.ParseResult;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author ******
 * Scans the network for computers connected
 */
public class AngryIPParser {

    private String ip = "xx.xx.xx.xx";
    private String hostname = "host";
    private String ping = "m/s";
    
    public ParseResult[] parseResult(BufferedReader reader) throws FileNotFoundException, IOException {

        
        ParseResult[] out;
        ArrayList<ParseResult> ipHostsFound = new ArrayList();
        
        try {
            boolean bHeadersDone = false;
            while (reader.ready()) {
                String beanInfo = reader.readLine();
                if (!bHeadersDone) {
                    if (beanInfo.contains("Ping")) {
                        bHeadersDone = true;
                    }
                } 
                else {
                    String[] values = beanInfo.split("," , 3);
                    ip = values[0];
                    hostname = values[1];
                    ping = values[2];

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        out = (ParseResult[])ipHostsFound.toArray(new ParseResult[ipHostsFound.size()]);    
// compiles without errors/exceptions but doesn't function as it should 
            
// out = (ParseResult[]) ipHostsFound.toArray();    <-- comes up with cast exception
        System.out.println(out);
        return out;
        
    }

    /**
     * 
     * @return hostname of the parsed machine
     */
    public String getHostname() {
        return hostname;
    }

    /**
     * 
     * @return ipaddress of the parsed machine
     */
    public String getIP() {
        return ip;
    }
}

Anyways I put a print statement in there to see what the results were for my out and it comes up with this:

[Ledu.utexas.arlut.cristal.struct.ParseResult;@5740bb

Overall what I am trying to do is get it to add it to my arrayList ipHostsFound and then create a viewobject out of it, which will later be displayed from my main program.

Could someone point me in the correct direction :)

Recommended Answers

All 3 Replies

If you must, cast each element individually to the appropriate type.

public static void main(String[] args) {
	
		Object[] ints = new Integer[10];
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(1);
		ints = list.toArray();
		System.out.println((Integer)ints[0]);
		
	}

Well one problem is that I won't know the overall size of the array.

Also if I'm not mistaken wouldn't ints = list.toArray(); also throw a cast exception for incompatible types?

Sorry I am still a bit 'new' to java and don't have much experience under my belt.

No, it would not, because I declared "ints" as an array of Objects. . not as an array of Integers. Then, knowing it was actually storing Integers, I casted the value that was at the index to an Integer. It runs correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.