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 6625511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:47:01+00:00 2026-05-25T21:47:01+00:00

I have conversion to Map problem in Core Java. Below is requirement: Given a

  • 0

I have conversion to Map problem in Core Java.

Below is requirement:
Given a String array below

String str[] = {"abc","123","def","456","ghi","789","lmn","101112","opq"};          

Convert it into a Map such that the resultant output is below

Output

======      ======         
key         Value        
======      ======                  
abc          true       
123          false       
def          true      
456          false

The above should be printed for each element in the array. I have written the code but it’s not working and I’m stuck. Please let me know how it can be resolved. Thanks in advance.

import java.util.HashMap;       
import java.util.Iterator;       
import java.util.Map;       

public class CoversionToMap {

/**
 * @param args
 */
public static void main(String[] args) {
    String str[] = {"abc","123","def","456","ghi","789","lmn","101112","opq"};
    Map m = new HashMap();
    for(int i=0;i<str.length;i++){
        if(Integer.parseInt(str[i]) < 0){
            m.put(str[i],true);
        }else{
            m.put(str[i],false);
        }
    }
    //Print the map values finally
    printMap(m);
}   

public static void printMap(Map mp) {
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();          
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
        }
}  
}           

exception:

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"       
    at java.lang.NumberFormatException.forInputString(Unknown Source)       
    at java.lang.Integer.parseInt(Unknown Source)       
    at java.lang.Integer.parseInt(Unknown Source)       
    at CoversionToMap.main(CoversionToMap.java:22)       
  • 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-05-25T21:47:02+00:00Added an answer on May 25, 2026 at 9:47 pm

    Everyone is suggesting using exception handling for this, there is nothing exceptional here to warrant using exceptions like this, you don’t try turning left in your car and if you crash go right do you? Something like this should do it

    Map<String, Boolean> m = new HashMap<String, Boolean>();
    for (String str: strs) {
        m.put(str, isInteger(str));
    }
    
    public boolean isInteger(String str) {
        int size = str.length();
    
        for (int i = 0; i < size; i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
    
        return size > 0;
    }
    

    Much clearer and more efficient that catching throwing exception, even when there are 99% integers as the integer value is not even needed so no conversion required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following implicit conversion for java.util.Enumerations implicit def enumerationIterator[A](e : Enumeration[A]) :
The problem: I have a mutable.Map[Integer, String], I want to pass it to two
I have a string containing byte data. How can I perform an in-place conversion
I have a little problem with Java (being a C++ programmer). I have 2
If I have a structure like std::map<string, int> myMap; myMap["banana"] = 1; myMap["apple"] =
I have a question concerning number conversion i JS. I have a number like
I have to create a regular expression for some path conversion. Example for path
Are there any standalone type conversion libraries? I have a data storage system that
I have used many of the Convert.To..... functions for conversion , but I didn't
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void

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.