Converting Integer to int - Junit

Hello
I have a problem with a Junit test case. The search function did not return any helpful results, so I'm trying my luck here.
I'm using Junit 4.4 and JDK1.6.0_01
My class which I would like to test looks like this:
public class SortedIntArrayList<Integer> extends ArrayList<Integer> {
     public SortedIntArrayList(){
          super();
    /* adds an integer val such that the list is
       always sorted in ascending order
     public void addInt(Integer val){
     // @TODO implementation will follow
}and this is my test class
public class SortedIntArrayListTest extends TestCase {
     private SortedIntArrayList<Integer> saExp = new SortedIntArrayList<Integer>();
     private SortedIntArrayList<Integer> saAct = new SortedIntArrayList<Integer>();
     @Override
        public void setUp(){
          // set up the actual sortedIntArrayList
          saAct.add(-6);
          saAct.add(2);
          saAct.add(9);
          saAct.add(22);
          saAct.add(69);
          // set up the expected sortedIntArrayList
          saExp.add(-6);
          saExp.add(2);
          saExp.add(9);
          saExp.add(22);
          saExp.add(69);
     // add an element at the beginning of the arraylist
     public void testAddIntFirstElement(){
          int val = -10;
          this.saAct.add(0,val);
          this.saExp.addInt(new Integer(val));
          assertEquals(saAct,saExp);
}Now to my problem.
I'm trying to create a method addInt(Integer) in my SortedIntArrayList class, such that the resulting arrayList will be ordered.
In my addInt(Integer val) method I want to get the int value from the val Integer object to compare it to the values already in the arrayList.
What I tried to do:
public void addInt(Integer val){
     if(this.get(0).intValue()<val.intValue()){
         // do something
}Does not work! Also, Integer.parseInt and all these Integer methods do not show up.
When I open a new project and try something like
public static void main(String[] args) {
   int i;
   Integer itgr = new Integer(10);
   i = itgr.intValue();
}it does work.
What am I doing wrong? By the way, I tried it in Eclipse as well as in NetBeans.
Thanks for your help
Greetings, David
Edit: Well, after some research I found out that my actual problem is that I cannot Instantiate Integer in my extended class like:
Integer i = new Integer(30);
message: cannot instantiate integer.
Any ideas why that is?
Edited by: strad on Nov 9, 2007 8:43 AM

It sounds like you've defined a class and named it .... Integer! So the compiler thinks "Integer" is your class and not java.lang.Integer.
Solution: rename or get rid of your Integer class, or be forced to write this everywhere:
java.lang.Integer x = new java.lang.Integer(42);

Similar Messages

  • How to convert an Integer to int

    Integer y = new Integer(1);
    int x = ????(y);
    What is the missing command to do the above conversion ?
    I've tried casting etc.
    It's mid nite and I'm still at work!
    Appreciate all help. Thanks!

    Integer is an Object. int is a primitive base type. to convert an Integer Object into an int:
    //let's say you read a line from a file, and the line is this
    5
    //normally, since files contain characters, you would call a method that reads in String Objects
    //so the 5 is actually a String, which you need to convert to an int to use for math, calculations,
    //expressions, etc.
    String blah = Keyboard.readLine();
    int x = Integer.parseInt(blah);
    // now x equals the int 5.
    hope this helps.

  • How to convert from java.lang.Integer to int

    Could you please show me
    how to convert from java.lang.Integer to int?
    and how to convert from java.lang.Integer to String?
    Thanks,
    Minh

    Could you please show me
    how to convert from java.lang.Integer to int?
    and how to convert from java.lang.Integer to String?Tip: always keep a browser open on the API docs; if you've got a
    couple of MBs to spare, download the docs; it's very convenient.
    kind regards,
    Jos

  • [svn] 2142: swfutils: Somehow a Java 1.5 API (Integer.valueOf(int)) slipped into the 30x branch, and has only sporadically caused build problems.

    Revision: 2142
    Author: [email protected]
    Date: 2008-06-18 15:17:01 -0700 (Wed, 18 Jun 2008)
    Log Message:
    swfutils: Somehow a Java 1.5 API (Integer.valueOf(int)) slipped into the 30x branch, and has only sporadically caused build problems.
    * By "somehow" I mean it was my injection :)
    * Apparently this compiles in 1.4.2 on a Mac, go figure? I assume Windows JDK doesn't accept it.
    * Replaced it with 'new Integer(int)'
    Reviewer: Matt, community folks
    Bugs: n/a
    QA: no
    Doc: no
    Modified Paths:
    flex/sdk/branches/3.0.x/modules/swfutils/src/java/flash/swf/tools/SwfxPrinter.java

    Revision: 2142
    Author: [email protected]
    Date: 2008-06-18 15:17:01 -0700 (Wed, 18 Jun 2008)
    Log Message:
    swfutils: Somehow a Java 1.5 API (Integer.valueOf(int)) slipped into the 30x branch, and has only sporadically caused build problems.
    * By "somehow" I mean it was my injection :)
    * Apparently this compiles in 1.4.2 on a Mac, go figure? I assume Windows JDK doesn't accept it.
    * Replaced it with 'new Integer(int)'
    Reviewer: Matt, community folks
    Bugs: n/a
    QA: no
    Doc: no
    Modified Paths:
    flex/sdk/branches/3.0.x/modules/swfutils/src/java/flash/swf/tools/SwfxPrinter.java

  • How to convert String to int in JSP?

    Hi,
    I set a session attribute in Servlet and want use it in JSP, How can I convert it to int or Integer?
    the line in my code doesn't work:
    int quantity=(int)session.getAttribute("vehiclequantity") ;
    Thanks in advance.
    Wolf
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    <HTML>
    <HEAD>
    <TITLE>Using the for Statement</TITLE>
    </HEAD>
    <BODY>
    <H1>Using the for Statement</H1>
    <%=session.getAttribute("vehiclequantity") %>;
    <%
    int loopIndex;
    int quantity=(int)session.getAttribute("vehiclequantity") ;
    out.println(quantity);
    for (loopIndex = 1; loopIndex <=2; loopIndex++) {
    out.println("This is iteration number "
    + loopIndex + "<BR>");
    %>
    </BODY>
    </HTML>

    Learning how to read errors and understand them will help you solve your problems quicker by yourself... So lets take a look at the error and classes involved...
    The error says:
    "Cannor Resolve Symbol: method valueOf(java.lang.Object) in the class java.lang.Integer" and gives you line line where the error occurs: Integer quantity = Integer.valueOf(session.getAttribute("vehiclequantity"));
    Now, if we look at the API for the Integer we notice that are are only two valueOf methods: valueOf(java.lang.String s) and valueOf(java.lang.String s, int radix). Not valueOf(java.lang.Object) method.
    Now we look at the getAttribute(java.lang.String name) method of HttpSession we see that the method returns a java.lang.Object. Now, you know you put a java.lang.String into that attribute, but the get method returns an Object. This is because you could have put any object in there, an Integer, a String, or some other class instance. But you know it is a String, so you can cast the returned value to a String, so that you will be calling the valueOf(java.lang.String s) method of Integer with the Object returned from the HtttpSession's getAttribute(java.lang.String name) method:
    Integer quantity = Integer.valueOf((String)session.getAttribute("vehiclequantity"));

  • Please, Help to convert integer minutes to HH:mm format

    Greetings,
    I's trying to convert integer minutes (for example 150) to HH:mm format (so it'd be 2:30), but couldn't understand how it could be done... I've already looked through SimpleDateFormat with no effect :-( Just do not understand what to do...
    If anyone already knows the sample code I'd very appreciated ^-)
    Thanks much...

    Can't you just execute the math yourself? It's pretty straightforward:
       public String getHourMinFormat(int totalmin){
           int hours = totalmin / 60;
           int min = totalmin % 60;
           return Integer.toString(hours).trim()+":"+Integer.toString(min).trim();
       }No fancy library method required.

  • How to Convert Integer to byte[n]!Help

    I have a Integer 314,Convert to hex is 0x13a,I want to convert to byte[2],and byte[0] equals 0x13 and byte[1] equals [0x30];I use this method convert byte[4],
    int iVal = 314;
    byte [] bVals = new byte[4];for( int i=0; i<4; i++ ) { 
    bVals<i> = (byte)(iVal & 255);
    iVal >>= 8;
    Not I want to result.Please help!

    Try:
    int iVal = 0x01FF03F0;
    byte [] bVals = new byte[4];
    for(int i=3; i>=0; i--) {
        bVals[i] = (byte)(iVal & 255);
        iVal >>= 8
    //print results
    for (int i = 0; i < 4; i++) {
       int temp = bVals[i] & 0xFF;  // avoid displaying negative numbers
       System.out.println("byte " + i + " = " +  Integer.toHexString(temp));
    }

  • Convert Integer to binary

    I want to co convert an interger in binary code. For example int 1= 001010101010101. Is there a method to convert this?

    If you mean, convert an int to a String that represents the int value in binary, check out the Integer class.
    int value = 100;
    String str=Integer.toBinaryString(value);

  • Converting from unsigned int / short to byte[]

    Can anybody help me, I am trying to convert from:
    - unsigned int to byte[]
    - unsigned short to byte[]
    I will appreciate your help lots. thanks.

    @Op.
    This code converts an integer to a byte[], but you have to consider the byte order:
            int value = 0x12345678;
            byte[] result = new byte[4];
            for (int i=3; i>=0; i--) {
                result[i] = (byte)(value & 0xff);
                value = value >> 8;
            }This is another option:
            int dummy = 7;
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            os.write(dummy);
            byte[] bytes = os.toByteArray();Kaj

  • SSRS Convert Integer to Date

    Does anyone know how to convert integer to date in SSRS Report?
    The integer is in this format: 20140404
    Thank you.

    Is it a parameter?
    Is it a report result set?? Please post complete details.
    With less info I assume that is not a interger, It is just a different date format. See below
    Date Patterns
    if it is a real integer, sample
    Declare @test int=20140404
    SELECT cast (convert (varchar(8),@test) as date)
    - please mark correct answers

  • How to convert varchar to int in MSSQL?

    Hi,
    I am using mssql. how can i convert varchar to int type.
    for eg.
    id (int) txt(varchar)
    1 ---------10
    2 ---------10a
    3 ---------10b
    i want to convert varchar to int. HOw? pls explain with query.
    Thanks
    edward

    "10a" wouldn't normally be considered a number. So what sort of number do you expect it to be?
    Other than that you can look at the substring and convert/cast functions.

  • Handling Exceptions with case statement - convert Exception to int value

    Hi,
    I'm developing a web app, with servlets.
    I'm catching Exceptions in my servlets, for example I would like a user to insert data into a form. If the data cannot be converted into a integer value a java.lang.NumberormatException is thrown.
    I would like to catch this and then create a message giving a more specific clue to what happened. Unfortunatly it's possible that other exceptions could be caught, so I want to be able to check which type of Exception has been caught.
    I started writing a getErrorMessage(int Code) class. The method has a case statement which just checks the code and return the appropriate message.
    This worked well for SQL exceptions as they provide a getErrorCode method. But I was wondering is there any way to convert other Exceptions such as
    java.lang.NumberormatException into an integer value?
    Cheers

    Exceptions have their types (classes) and messages (and maybe an embedded exception). That is, they have much richer internal status then an integer.
    Why should they be "converted" to integers?
    FLAME on
    Gone are the days of good old errno.
    FLAME off

  • Fm for converting char3 to int

    suggest me a function module for converting char of length 3 to int
    and a function module for converting from int to char 3

    Hi ,
    You dont require a FM for that.
    Simply do the following:
    Data : l_temp(3) type c,  " CHar
              l_sum     type i.    " Integer
    * To move char to integer
    L_temp = '1234'.
    move l_temp to l_sum.
    * To move integer to char
    clear : l_temp, l_sum.
    l_sum = '23433'.
    move l_sum to l_temp.
    Best regards,
    Prashant

  • Converting string to int

    I cannot convert this string to integer.
    int tempI = Integer.parseInt("fdcedfbd", 16);
    it returns a numberFormatException
    but if I do this then there will be no problem.
    int tempI = 0xfdcedfbd
    Can anybody help me with this? Thank you.

    It sounds like the real problem here is that someone is giving you an 8-byte unsigned value represented as a 16-character hexadecimal string, and they want you to convert that into the actual 8 bytes. Are they going to do bit manipulation on it or something?
    If you successfully convert it into a long and they then try to do math with it, they'll run into the sign/overflow problems everyone else has been talking about. If they just want to look at the individual bits then it would make sense to do the conversion into a long.
    You could do something like this:
      private static final String highOrderDigits = "89abcdef";
      public long hexToLong(String hex) {
        // Only process exactly 16-digit strings.
        if (hex.length() != 16) {
          throw new IllegalArgumentException("Expect 16-digit hex value");
        boolean overflow = false;
        // Check for most significant digit > 7, which would
        // cause problem for parseLong.
        int highOrderIndex = highOrderDigits.indexOf(hex.charAt(0));
        if (highOrderIndex > -1) {
          // Clear most-significant bit and set overflow flag.
          overflow = true;
          hex = "" + highOrderIndex + hex.substring(1);
        long result = Long.parseLong(hex, 16);
        if (overflow) {
          // Manually set most significant bit in result.
          result |= 0x8000000000000000L;
        return result;
      }I more or less tested this and it looks to work.

  • Exception thrown trying to convert string to int

    Hey Guys,
    i imported a csv file into my app which i divide using the split() method and put into an array then I divided the array into variables;
    fruit = piece[0];
    price = piece[1];
    unit = piece[2];Now since price has to be a double i converted it into a double;
    price2 = Double.parseDouble(price);which worked grand
    but when i try to convert unit to an integer
    unit2 = Integer.parseInt(unit);i got error
    Exception in thread "main" java.lang.NumberFormatException: For input string: "3
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    so i tried removing white spaces as follows
    target = unit.replace(" ", "");which worked and displayed grand, then i tried converting to int again but the same exception was thrown, can anyone help me with this problem, thanks
    mark

    sorry for the messed up format previously.
    Like you said the unit.replaceAll(" ", ""); did not do the trick, when displayed as a string it looked like 3 was on its own but still did not convert to int. I assume line break or something existed in the csv file like you mentioned earlier as unit was the last word in the csv file.
    when i used unit.trim(); and tried to convert to integer it worked perfectly.
    thanks all for your help,
    Mark

Maybe you are looking for