Results 1 to 6 of 6

Thread: Converting a map.get (key) to int

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Converting a map.get (key) to int

    Hello,
    I'm on an application from a server logs produces statistics on this one. However I encounter some difficulties and I'd like your help . Let me explain:
    I put the logs in a 2D array (online information). Subsequently, I created a map with key as the name of a database and I value is the connection time on the latter in milliseconds.
    Code:
    Map m = new TreeMap ();
          for (int i = 0; i <x; i + +)
            (
            Object o = m.get (Logsession [i] [3]);
            if (o == null)
              m.put (Logsession [i] [3], new Integer (Integer.parseInt (Logsession [i] [1 ])));
            else
              (
                (
              Integer cnt = (Integer) o;
              m.put (Logsession [i] [3], new Integer (cnt.intValue () + Integer.parseInt (Logsession [i] [1 ])));
              ))
            )
    
          List ky = new ArrayList (m.kySet ());
            Iterator it = ky.it ();
            while (it.hasNext ())
              (
              Object ky = Iterator.next ();
              nb = Integer.parseInt ((String) m.get (ky)) / (1000 * 60 * 60);
              modulo =Integer.parseInt ((String) m.get (ky))% (1000 * 60 * 60);
              nbMin = mod / (60 * 1000);
              modulo = modulo% (60 * 1000);
              nbSec = modulo/1000;
              label = new JLabel ( "Time of connection to the Database" + ky + ":" + nb + "hours," nbMin + + "minutes and" + nbSec + "seconds");
              )

  2. #2
    Join Date
    Nov 2009
    Posts
    347

    Re: Converting a map.get (key) to int

    Hello,
    Your map is of type Map <String,Integer>, if I remember correctly, the key to a map is unique so if you have several times the same key in a. Put on your map, risk of crush the values as and. You have in your map values of type Integer then when you go to a map.get (key) you will have the value type Integer, you do not need the caste to convert String to Integer with Integer.parseInt.

  3. #3
    Join Date
    Nov 2009
    Posts
    343

    Re: Converting a map.get (key) to int

    Hello,
    Just have a look at the following code this can help you.
    Code:
    for (int j = 0 j <map.size () j + +) (
    nb = map.get (j)/ (1000 * 60 * 60);
    mod =map.get (j)% (1000 * 60 * 60);
    nbn = mod / (60 * 1000);
    modu = modulo% (60 * 1000);
    nbSe = modulo/1000;
    lb = new JLabel ( "Time of connection to the Database" + key + ":" + nbHour + "hours," nbMin + + "minutes and" + nbSec + "seconds");
    )
    I have modified it, just see if it work for you.

  4. #4
    Join Date
    Dec 2009
    Posts
    296

    Re: Converting a map.get (key) to int

    Hello,
    Yes, actually what I wanted to do was to convert the value to int, not one is designated as the object by the map when it is actually of type Integer (I myself loses). By dint of tinkering I found something that works:
    Code:
    nb = ((Integer)(map.get(key))).intValue()/(1000*60*60);
    mod =((Integer)(map.get(key))).intValue()%(1000*60*60);
    If you have an alternative for this then please let me know. Thanks in advance.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Converting a map.get (key) to int

    Hello,
    You have try the following code
    Code:
         Iterator it = map.keySet().iterator();
         while (it.hasNext()){
              Object key = it.next();
              nb = map.get(key)/(1000*60*60);
              mod = map.get(key )%(1000*60*60);
              nbM = modulo /(60*1000);
              modu = modulo%(60*1000);
              nbSe = modulo /1000;
              lb =new JLabel("Time of connection to the Database" + Key
                        + " : "+ + nbHour"Hours" + + NbMin "Minutes" + NbSec
                        + "Seconds");
         }

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Converting a map.get (key) to int

    Hello,
    If you are using Java 1.4 then the following code should work with you.
    Code:
         Iterator itr = map.kySet().iterator();
         while (itr.hasNext()){
              Object ky = itr.next();
              int milSec = ((Integer) map.get(ky)).intValue();
              nb = milSec /(1000*60*60);
              mod = milSec%(1000*60*60);
              nbM = modulo /(60*1000);
              modu = modulo%(60*1000);
              nbSe = modulo /1000;
              lb =new JLabel("Time of connection to the Database" + Key
                        + " : "+ + nbHour"Hours" + + NbMin "Minutes" + NbSec
                        + "Seconds");
         }

Similar Threads

  1. Converting MP4 to MP3
    By SajalSOFT in forum Software Development
    Replies: 6
    Last Post: 30-08-2010, 06:43 PM
  2. Converting mov to mp4
    By Nadiaa in forum Windows Software
    Replies: 6
    Last Post: 22-05-2010, 11:48 PM
  3. Converting H.264 to DVD
    By Quinton in forum Windows Software
    Replies: 5
    Last Post: 04-05-2010, 10:09 AM
  4. converting adc to dc
    By mohdomer in forum Networking & Security
    Replies: 2
    Last Post: 24-11-2009, 01:58 PM
  5. Need help converting to mp3 wav
    By kyosang in forum Windows Software
    Replies: 2
    Last Post: 27-10-2008, 12:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,334,966.81374 seconds with 16 queries