We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,578 Members — Technology Publication meets Social Media

# Double cannot be cast to Integer ERROR HELP

I am writing a program that inputs data and creates a graph represented by two adjacency matricies. One matricie contains one weight, and the other a different weight, ie distance and time between nodes. I am recieving the Double cannot be cast to Integer at a point where no Doubles are even involved let alone casting. I am very confused by this error. Here is my code:

public static void main(String args[]){
ArrayList<Integer> node1 = new ArrayList();
ArrayList<Integer> node2 = new ArrayList();
ArrayList<Integer> time = new ArrayList();
ArrayList<Double> distance = new ArrayList();
String file = "distanceAndTime.dat";

// Input data from file to ArrayLists
inputData(file, node1, node2, time, distance);

int numNodes = howManyNodes(node1,node2);// get number of distinct nodes
graph g = new graph(numNodes);           // create graph of correct size

//Label all nodes
for(int j = 0; j<numNodes; j++){
g.setLabel(j, "v"+j);
}

for(int i=0; i<node1.size(); i++){
g.addTimeEdge(node1.get(i), node2.get(i), time.get(i));  //This is where error occurs
}
/*
for(int i=0; i<node1.size(); i++){
}*/

g.printTime();
g.printDistance();

}

public class graph {

private int[][] timeEdge;  // adjacency matrix w/time traveled node to node
private double[][] distanceEdge; // adjacency matrix w/distance between nodes
private Object[] labels;

public graph(int n) {
timeEdge  = new int [n][n];
distanceEdge = new double[n][n];
labels = new Object[n];
}

public int size(){
return labels.length;
}

public void setLabel(int vertex, Object label){
labels[vertex]=label;
}
public Object getLabel(int vertex){
return labels[vertex];
}

public void addTimeEdge(int source, int target, int w){
timeEdge[source][target] = w;
}

public void addDistanceEdge(int source, int target, double w){
distanceEdge[source][target] = w;
}
...

Thanks to anyone that can help me with this error.

3
Contributors
12
Replies
5 Hours
Discussion Span
1 Year Ago
Last Updated
13
Views
crazymidget01
Light Poster
38 posts since Dec 2010
Reputation Points: 10
Skill Endorsements: 0

Please post the full text of the error message.
What line in the code has the error?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Skill Endorsements: 16

Exception in thread "main" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer at finalProject.finalProject.main(finalProject.java:32)

ABOVE ERROR IS AT LINE 21. If line 26 is un-commented it receives this error as well.

crazymidget01
Light Poster
38 posts since Dec 2010
Reputation Points: 10
Skill Endorsements: 0

Can you post complete code that will compile, execute and show the problem?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Skill Endorsements: 16
public class graph {

private int[][] timeEdge;  // adjacency matrix w/time traveled node to node
private double[][] distanceEdge; // adjacency matrix w/distance between nodes
private Object[] labels;

public graph(int n) {
timeEdge  = new int [n][n];
distanceEdge = new double[n][n];
labels = new Object[n];
}

public int size(){
return labels.length;
}

public void setLabel(int vertex, Object label){
labels[vertex]=label;
}
public Object getLabel(int vertex){
return labels[vertex];
}
public void addTimeEdge(int source, int target, int w){
timeEdge[source][target] = w;
}

public void addDistanceEdge(int source, int target, double w){
distanceEdge[source][target] = w;
}

public boolean isEdge (int source, int target){
return timeEdge[source][target]>0;
}

public int getTime (int source, int target){
return timeEdge[source][target];
}

public double getDistance (int source, int target){
return distanceEdge[source][target];
}

public void printTime() {
for (int j=0; j<timeEdge.length; j++) {
System.out.print (labels[j]+": ");
for (int i=0; i<timeEdge[j].length; i++) {
if (timeEdge[j][i]>0) System.out.print (labels[i]+":"+timeEdge[j][i]+" ");
}
System.out.println ();
}
}

public void printDistance() {
for (int j=0; j<distanceEdge.length; j++) {
System.out.print (labels[j]+": ");
for (int i=0; i<distanceEdge[j].length; i++) {
if (distanceEdge[j][i]>0) System.out.print (labels[i]+":"+distanceEdge[j][i]+" ");
}
System.out.println ();
}
}
}

import java.util.*;
import java.io.*;

public class finalProject {
public static void main(String args[]){
ArrayList<Integer> node1 = new ArrayList();
ArrayList<Integer> node2 = new ArrayList();
ArrayList<Integer> time = new ArrayList();
ArrayList<Double> distance = new ArrayList();
String file = "distanceAndTime.dat";

// Input data from file to ArrayLists
inputData(file, node1, node2, time, distance);

int numNodes = howManyNodes(node1,node2);// get number of distinct nodes
graph g = new graph(numNodes);           // create graph of correct size

//Label all nodes
for(int j = 0; j<numNodes; j++){
g.setLabel(j, "v"+j);
}

for(int i=0; i<node1.size(); i++){
}
/*
for(int i=0; i<node1.size(); i++){
}*/

g.printTime();
g.printDistance();

}

public static void inputData(String fileName, ArrayList n1, ArrayList n2, ArrayList d, ArrayList t){
try{
Scanner Input = new Scanner(new File(fileName));

while (Input.hasNextLine()){
// Each line in file is formatted with 4 values that corespond
// to an edge with two different weights( distance & time)
}

// Print data
for(int i = 0; i<n1.size();i++){
System.out.println(n1.get(i) + " " + n2.get(i) + " " + d.get(i) + " " + t.get(i));
}
}catch(Exception e){
System.err.println("Error: " + e.getMessage());
}
}

public static int howManyNodes(ArrayList n1, ArrayList n2){
ArrayList nodes = new ArrayList();

//Only Add Distinct Nodes to nodes list (NO DUPLICATES)
for(int i=0; i<(n1.size()+n2.size()); i++){
if(i<n1.size()){
if(!(nodes.contains(n1.get(i))))
}else{
if(!(nodes.contains(n2.get(i-n1.size()))))
}
}

return nodes.size();    // Return # of distinct nodes in graph
}
}

Part of file distanceAndTime.dat Input:

1 2 1.5 380
1 3 0.7 190
2 5 3.1 360
3 2 1.4 210
3 4 0.7 200
4 6 3.6 380
4 7 1.1 230
5 6 0.9 120
5 8 1.6 140

crazymidget01
Light Poster
38 posts since Dec 2010
Reputation Points: 10
Skill Endorsements: 0

Maybe a Double is sneaking into your incorrectly initialised ArrayLists - can we see the code for the inputData method?

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Skill Endorsements: 33

I get 10 warnings when I compile the code. I bet if you fix the code so there are no warnings it will fix your problem.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Skill Endorsements: 16

This is a classic example of why you should use classes rather than parallel arrays .
(spoiler follows)

inputData(file, node1, node2, time, distance);
public static void inputData(String fileName, ArrayList n1, ArrayList n2, ArrayList d, ArrayList t)

... and that's how you get Double values in the time array.

If you had initialised your ArryLists properly and declared the parameters fully for the method you would have found this error immediatley.

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Skill Endorsements: 33

how are the ArrayLists not initialized correctly?

crazymidget01
Light Poster
38 posts since Dec 2010
Reputation Points: 10
Skill Endorsements: 0
new ArrayList();           //  an arraylist that accepts any kind of data
new ArrayList<Integer>();  // an arraylist for Integers only
new ArrayList<>();         // short version (needs Java 7)

similarly:

public static void inputData(String fileName, ArrayList<Integer> n1 ...
// prevents you passing an ArrayList<Double> as parameter
JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Skill Endorsements: 33

Do you have warnings turned on for the compiler? It will flag all the places where the code needs to be fixed.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Skill Endorsements: 16

So the following would be a more "correct" way of doing this?

public class problem {
private ArrayList<Integer> node1 = new ArrayList<Integer>();
private ArrayList<Integer> node2 = new ArrayList<Integer>();
private ArrayList<Integer> time = new ArrayList<Integer>();
private ArrayList<Double> distance = new ArrayList<Double>();

public problem(String fileName){
try{
Scanner Input = new Scanner(new File(fileName));

while (Input.hasNextLine()){
// Each line in file is formatted with 4 values that corespond
// to an edge with two different weights( distance & time)
}

// Print data
for(int i = 0; i<node1.size();i++){
System.out.println(node1.get(i) + " " + node2.get(i) + " " + distance.get(i) + " " + time.get(i));
}
}catch(Exception e){
System.err.println("Error: " + e.getMessage());
}
}

public int getNumberNodes(){
ArrayList nodes = new ArrayList();
//Only Add Distinct Nodes to nodes list (NO DUPLICATES)
for(int i=0; i<(node1.size()+node2.size()); i++){
if(i<node1.size()){
if(!(nodes.contains(node1.get(i))))
}else{
if(!(nodes.contains(node2.get(i-node1.size()))))
}
}
return nodes.size();    // Return # of distinct nodes in graph
}

public static void main(String Args[]){
problem one = new problem("distanceAndTime.dat");
System.out.println(one.getNumberNodes());
}
crazymidget01
Light Poster
38 posts since Dec 2010
Reputation Points: 10
Skill Endorsements: 0

That's a lot better - the compiler has a much better chance of picking up any mistakes. Best of all is to have a class rather than those parallel arrays:

class Edge {
public final int node1, node2, time; // I assume it's immutable
public final double distance;
public Edge (int node1, int node2.....
this.node1 = node1;
.....

Then when you read the file create new Edge objects and add them to an ArrayList<Edge>

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636