1

My goal is to simply take my int and interpret it as an Integer trough any means, no work-arounds

I have an object Node<T> (int Key, T value).

I am working towards a program that can properly use generics, for now I just want it to use integers. However I can't make Node<int>, I have to use Node<Integer>.

I don't know how to read an Integer from the console, I know how to read only int.

code

public void addNumber (int number) {
   Node<Integer> newNode = new Node<Integer>(number,(Integer)number); //does not work
   this.gd.add(newNode);                                       
}

What I tried:

Integer iNumb = new Integer(number);   // Could not instantiate the type integer

and:

Node<Integer> newNode = new Node<Integer>(number, number);

I have no constructor for this, going that route would be pointless.

I've also tried this:

public void addNumber() throws GenericDictionary_exception {
    Scanner input = new Scanner(System.in);
    int number;
    System.out.print("Number: ");

    if (input.hasNextInt()) { 
    number = input.nextInt();
    } else
    throw new GenericDictionary_exception(
              "Error\n\t**This version only supports input of numbers**");
    
    Integer integer = number; // Type missmatch
    }

How do I take an int and cast it to Integer if generics are in play in Java?

 int num = 5;
 Integer integer = num;

That works.

Community
  • 1
  • 1
Kalec
  • 2,380
  • 6
  • 26
  • 42
  • have a look at http://stackoverflow.com/questions/564/what-is-the-difference-between-an-int-and-an-integer-in-java-c – dmaij Dec 13 '12 at 19:30
  • why can't you make `Node` – Sam I am says Reinstate Monica Dec 13 '12 at 19:30
  • @Kalec If you want to read something from console, follow this link (http://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in-java) – Smit Dec 13 '12 at 19:32
  • What version of Java are you using? `new Node(number, number)` should work just fine if `number` is an `int`. – Louis Wasserman Dec 13 '12 at 19:34
  • 2
    And `(Integer)number` should also box an `int` to an `Integer` with no problems. – Marko Topolnik Dec 13 '12 at 19:39
  • That's my biggest problem. I can't understand why `(Integer)number` isn't working. I have JDK 7 or higher, either way it should not matter, as far as I understand it should box the int with anything over 1.5. This "Cannot cast from int to integer" error is perplexing. – Kalec Dec 13 '12 at 19:49

1 Answers1

1

Assuming number is an int:

Integer myInteger = number;

Java generics have some limitations, and one of them is that you can't use primitives, you must instead use Java's primitive wrapper classes.

Here's a more involved explanation:

Why don't Java Generics support primitive types?

EDIT:

Just to be clear, I assume your Node class looks something like this:

public class Test {
    public static void main(String[] args) {
        int number = 2;
        Integer myInteger = number;
        Node<Integer> myNode = new Node<Integer>(number, number);
        System.out.println(myNode.getValue());
    }

    public static class Node<T> {
        private int key;
        private T value;

        public Node(int key, T value) {
            this.key = key;
            this.value = value;
        }

        public T getValue() {
            return value;
        }
    }    
}

My suggestion would be to try compiling and running this test code. If it doesn't work, then there's something wrong with your environment. If it does work, the problem may be something to do with your code that we're not seeing.

EDIT 2:

This worked for me. Again, I'd suggest trying it as a standalone program:

public class Test {
    public static void main(String[] args) throws Exception {
        addNumber();
    }

    public static void addNumber() throws Exception {
        Scanner input = new Scanner(System.in);
        int number;
        System.out.print("Number: ");

        if (input.hasNextInt()) {
            number = input.nextInt();
        } else {
            throw new Exception(
                "Error\n\t**This version only supports input of numbers**");
        }

        Integer integer = number; // Type missmatch
        System.out.println(integer);
    }
}
Community
  • 1
  • 1
Travis Addair
  • 377
  • 3
  • 10
  • Yeah, same problem: "Type mismatch: cannot convert from int to Integer". Is there anything I should maybe explicitly import. Any way to check the version of java I use. It can't be 1.5 or lower but I'd like to be sure. – Kalec Dec 13 '12 at 19:53
  • 1
    I'm not getting any problems with anything that's been described here, including "Node newNode = new Node(number, number);". What version of the JDK are you using? – Travis Addair Dec 13 '12 at 20:04
  • Try typing "java -version" in a terminal. – Travis Addair Dec 13 '12 at 20:04
  • I don't see what version of JDK I have (I assume 7) but I have java 1.6.0_35-b10. I also use Eclipse, downloaded it either 3-4 moths ago or 1 year 3-4 months. – Kalec Dec 13 '12 at 20:07
  • Yes, that is my `Node`. Either way I've added some code that doesn't even touch that. I am unable to interpret an int as Integer no matter what. – Kalec Dec 13 '12 at 20:11
  • Ok. That worked. However I mentioned that if I locally declare the number it works, but if it's passed as a parameter and / or read from console, it does not. While I do think something is wrong with my environment, I would like to know that for sure. – Kalec Dec 13 '12 at 20:20
  • Did you define your own class named `Integer`? – Louis Wasserman Dec 13 '12 at 20:25
  • @LouisWasserman I am sure I did no such thing. It may be that it's broken. I just want to know if I can explicitly import Integer ? InteliSense isn't even touching the thing. I have no idea ... I am almost sure I used it in the past. – Kalec Dec 13 '12 at 20:26
  • Oh, I think I may have done something wrong it seems. Or is it wrong ? I have `ClassName` thus screwing up everything. Now it seems to work, I think. – Kalec Dec 13 '12 at 20:28
  • What do you mean by "ClassName"? – Travis Addair Dec 13 '12 at 20:40
  • Ehh I'm having a problem for which I should ask another question. I have a hashMap and I want to add integers (for now at least) but I have no idea how to do that, can't typecast from Integer to (type missmatch) so I tried hashMap and that seems to have cause my problem. – Kalec Dec 13 '12 at 20:42
  • A HashMap takes two types: the key and the value. If you want to map from integer to T, you'd want: HashMap. Is this HashMap a field in your Node? – Travis Addair Dec 13 '12 at 20:46
  • Never mind about that. While not pretty at all, it has been sorted out for now. – Kalec Dec 13 '12 at 22:12