Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:02:47+00:00 2026-06-15T20:02:47+00:00

My goal is to simply take my int and interpret it as an Integer

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:02:49+00:00Added an answer on June 15, 2026 at 8:02 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a Maven phase or goal to simply execute the main method of
Here's my list that I want to expand/collapse: My goal is simply to toggle
I think I have a pretty simple goal but cant seem to reach it.
I have very simple piece of code. The goal is when i input four-digit
My goal is to take HTML entered by an end user, remove certain unsafe
My goal is to write a custom camera view controller that: Can take photos
My goal is to write a custom camera view controller that: Can take photos
I actually have a few questions here: My end goal is to write a
Our goal is simple for an iPhone app: show a splash page then hide
I am trying to achieve a very simple goal, however it does not seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.