educative.io

Educative

Integer to english

why am i getting integer too large error when the same code works fine in feature lesson?


Type your question above this line.

Course: https://www.educative.io/collection/10370001/4938268330688512
Lesson: https://www.educative.io/collection/page/10370001/4938268330688512/6280499599769600

Hello @Pranjali_Choudhary,

I tested my implementation on the provided code widget and all of the test cases are passing. Can you please provide the specific input value you are using or share your implementation of the code so that I can cross reference it?

Hope it helps!

public static String numberToWords(int n) {

if(n==0) {

  return "Zero";

}

int billion = n/1000000000;

int million = (n-billion*10000000000)/1000000;

int thousand = (n-billion*10000000000 - million*1000000)/1000;

int rest = n-billion*10000000000 - million*1000000 - thousand*1000;

String result = "";

if(billion!=0) {

  result = three(billion) + " Billion";

}

if(million!=0) {

  if(!result.isEmpty()) {

    result += " ";

  }

  result+= three(million) + " Million";

}

if(thousand!=0) {

  if(!result.isEmpty()) {

    result+=" ";

  }

  result+= three(thousand) + " Thousand";

}

if(rest!=0) {

  if(!result.isEmpty()) {

    result+= " ";

  }

  result += three(rest);

}

return result;

}

main.java:80: error: integer number too large: 10000000000
int million = (n-billion*10000000000)/1000000;
                         ^

main.java:81: error: integer number too large: 10000000000
int thousand = (n-billion10000000000 - million1000000)/1000;
^
main.java:82: error: integer number too large: 10000000000
int rest = n-billion10000000000 - million1000000 - thousand*1000;
^
3 errors

10000000000 cannot be represented as a 32-bit integer. Hence, we have to represent it as a 64-bit integer using the long data type and by appending L at the end of the number just like this:

long billion = n/1000000000L;
long million = (n-billion*10000000000L)/1000000;
long thousand = (n-billion*10000000000L - million*1000000)/1000;
long rest = n-billion*10000000000L - million*1000000 - thousand*1000;

By following this rule, the errors are resolved. However, lines such as result = three(billion) + " Billion"; and result+= three(million) + " Million"; are still throwing in errors because three is not defined anywhere. Can you clarify as to what three is?

Hope this helps!

Hi, but ā€œnā€ here is an integer, it will throw lossy conversion error.
And why didnt it throw error in this lesson

Sharing full code here.

class Solution {
  public static String ones(int fare) {
switch(fare) {
  case 1: return "One";
  case 2: return "Two";
  case 3: return "Three";
  case 4: return "Four";
  case 5: return "Five";
  case 6: return "Six";
  case 7: return "Seven";
  case 8: return "Eight";
  case 9: return "Nine";
}
return "";
  }
  public static String teens(int fare) {
switch(fare) {
  case 10: return "Ten";
  case 11: return "Eleven";
  case 12: return "Twelve";
  case 13: return "Thirteen";
  case 14: return "Fourtee ";
  case 15: return "Fifteen";
  case 16: return "Sixteen";
  case 17: return "Seventeen";
  case 18: return "Eighteen";
  case 19: return "Nineteen";
}
return "";
  }
  public static String tens(int fare) {
switch(fare) {
  case 20: return "Twenty";
  case 30: return "Thirty";
  case 40: return "Fourty";
  case 50: return "Fifty";
  case 60: return "Sixty ";
  case 70: return "Seventy";
  case 80: return "Eighty";
  case 90: return "Ninety";
}
return "";
  }
  public static String two(int fare) {
if(fare==0) {
  return "";
} else if(fare<10) {
  return ones(fare);
} else if(fare<20) {
  return teens(fare);
} else {
  int tenner = fare/10;
  int rest = fare - tenner*10;
  if(rest!=0) {
    return tens(tenner)+ " "+ones(rest);
  } else {
    return tens(tenner);
  }
}
  }
  public static String three(int fare) {
int hundred = fare/100;
int rest = fare - hundred*100;
String res = "";
if(hundred*rest!=0) {
  res= ones(hundred) + " Hundred "+two(rest);
} else if(hundred==0 && rest!=0) {
  res= two(rest);
} else if(hundred!=0 && rest==0) {
  res= ones(hundred) + " Hundred";
}
return res;
  }
  public static String numberToWords(int n) {
if(n==0) {
  return "Zero";
}
int billion = n/1000000000L;
int million = (n-billion*10000000000L)/1000000L;
int thousand = (n-billion*10000000000L - million*1000000L)/1000;
int rest = n-billion*10000000000L - million*1000000L - thousand*1000;
String result = "";
if(billion!=0) {
  result = three(billion) + " Billion";
}
if(million!=0) {
  if(!result.isEmpty()) {
    result += " ";
  }
  result+= three(million) + " Million";
}
if(thousand!=0) {
  if(!result.isEmpty()) {
    result+=" ";
  }
  result+= three(thousand) + " Thousand";
}
if(rest!=0) {
  if(!result.isEmpty()) {
    result+= " ";
  }
  result += three(rest);
}
return result;
  }
}

Hello @Pranjali_Choudhary

I copy pasted the code from the feature lesson to the DIY lesson and it is executing and passing all tests without any errors. Please use the following code snippet for testing:

class Solution {

  public static String ones(int fare) {
    switch(fare) {
        case 1: return "One";
        case 2: return "Two";
        case 3: return "Three";
        case 4: return "Four";
        case 5: return "Five";
        case 6: return "Six";
        case 7: return "Seven";
        case 8: return "Eight";
        case 9: return "Nine";
    }
    return "";
  }

  public static String teens(int fare) {
    switch(fare) {
        case 10: return "Ten";
        case 11: return "Eleven";
        case 12: return "Twelve";
        case 13: return "Thirteen";
        case 14: return "Fourteen";
        case 15: return "Fifteen";
        case 16: return "Sixteen";
        case 17: return "Seventeen";
        case 18: return "Eighteen";
        case 19: return "Nineteen";
    }
    return "";
  }

  public static String tens(int fare) {
    switch(fare) {
        case 2: return "Twenty";
        case 3: return "Thirty";
        case 4: return "Forty";
        case 5: return "Fifty";
        case 6: return "Sixty";
        case 7: return "Seventy";
        case 8: return "Eighty";
        case 9: return "Ninety";
    }
    return "";
  }

  public static String two(int fare) {
    if (fare == 0)
        return "";
    else if (fare < 10)
        return ones(fare);
    else if (fare < 20)
        return teens(fare);
    else {
        int tenner = fare / 10;
        int rest = fare - tenner * 10;
        if (rest != 0)
            return tens(tenner) + " " + ones(rest);
        else
            return tens(tenner);
    }
  }

  public static String three(int fare) {
    int hundred = fare / 100;
    int rest = fare - hundred * 100;
    String res = "";

    if (hundred*rest != 0)
        res = ones(hundred) + " Hundred " + two(rest);
    else if ((hundred == 0) && (rest != 0))
        res = two(rest);
    else if ((hundred != 0) && (rest == 0))
        res = ones(hundred) + " Hundred";
    
    return res;
  }

  public static String numberToWords(int n) {
    if (n == 0)
        return "Zero";

    int billion = n / 1000000000;
    int million = (n - billion * 1000000000) / 1000000;
    int thousand = (n - billion * 1000000000 - million * 1000000) / 1000;
    int rest = n - billion * 1000000000 - million * 1000000 - thousand * 1000;

    String result = "";
    
    if (billion != 0)
        result = three(billion) + " Billion";
    if (million != 0) {
        if (! result.isEmpty())
            result += " ";
        result += three(million) + " Million";
    }
    if (thousand != 0) {
        if (! result.isEmpty())
            result += " ";
        result += three(thousand) + " Thousand";
    }
    if (rest != 0) {
        if (! result.isEmpty())
            result += " ";
        result += three(rest);
    }
    
    return result;
  }
}