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

  • SEARCH
  • Home
  • 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 9192651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:58:38+00:00 2026-06-17T20:58:38+00:00

Could anyone please explain to me why the add() method returns 0 instead 4?

  • 0

Could anyone please explain to me why the add() method returns 0 instead 4?
I am trying to use int 0 in case “invalid” string number is provided (e.g. four).
I am getting the right results for String arguments 3,4 / 3,four / three,four / but not for three, 4.

Can you please give me hints what I am doing wrong?
Thanks!

public class Numbers2
{
    public static void main(String[] args) {

        System.out.println(add("d","4"));
    } // main() method

   public static int add(String a, String b)
   {
       int x = 0;
       int y = 0;

      try{ 
          x = Integer.parseInt(a); 
          y = Integer.parseInt(b);

          System.out.println("No exception: " + (x+y));

          return x + y;
      }
      catch (NumberFormatException e){

          if(x != (int) x ){              
              x = 0;
          }
          if(y != (int) x ){
              y = 0;              
          }      
          return x + y;  
      }

   } // add() method
} // class

  • 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-17T20:58:40+00:00Added an answer on June 17, 2026 at 8:58 pm

    The problem is that the line y = Integer.parseInt(b); doesn’t get the chance to execute as you are passing "d" as the first argument which is not an integer, the line x = Integer.parseInt(a); results in exception and both x and y remain 0.

    You could solve the problem by using separate try/catch for both:

    int x = 0; //x is 0 by default
    int y = 0; //y is 0 by default
    
    try { 
        x = Integer.parseInt(a); //x will remain 0 in case of exception  
    }
    catch (NumberFormatException e) {
        e.printStackTrace();
    }
    
    try {  
        y = Integer.parseInt(b); //y will remain 0 in case of exception  
    }
    catch(NumberFormatException e) {
          e.printStackTrace();
    }
    
    return x + y; //return the sum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anyone please explain why this code compiles : #include <stdio.h> #include <string.h> int
Could anyone please explain what context should i use the AlertDialog.Builder class? I am
Could anyone please explain how can I check if String is null or empty?
Could anyone please explain with a sample...? How can I return an integer/String from
Could anyone please explain to me the use of the xs:list datatype? This could
Could anyone please explain the following line of code, found on http://docs.openttd.org/ai__cargo_8cpp_source.html return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
Qwt seems to be using a lot of magic numbers. Could anyone please explain
Could anyone please tell me why the following line about filter init method invocation
Could anyone please explain to me why the following line of code prints out
I'm a newbie to actor model. Could anyone please explain the lifecycle of an

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.