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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:04:16+00:00 2026-06-12T07:04:16+00:00

I am trying to take a text file with fractions on it. Convert the

  • 0

I am trying to take a text file with fractions on it. Convert the fractions from strings to bigdecimal. Then divide and store the results on a stack.

Each line in the txt file is one fraction which was randomly generated with the char ‘/’ separating the numerator and denominator. The numerator and denominator can be of length 1-50 and must be greater than 0. The numbers are generated randomly resulting in the need of storing decimal places. In my code I convert the string to BigInteger first and then to BigDecimal. This works for the first string but breaks on the numerator of the second string.

public static void main(String[] args) throws FileNotFoundException {
    File file = new File("C:/Users/eric/workspace/sortProj/src/data.txt");

    FileReader fr = new FileReader(file);
    Scanner scanner = new Scanner(fr);
    Stack<BigDecimal> pile = new Stack<BigDecimal>();
    String numerator = "";
    String denominator = "";

    while (scanner.hasNextLine()) {
        String str = scanner.nextLine();
        numerator = "";
        denominator = "";

        separateStrings(pile, numerator, denominator, str);
    }
}


// appends chars to numerator string until it hits '/' then appends chars to
// denominator string
public static void separateStrings(Stack<BigDecimal> stack,
        String numerator, String denominator, String source) {
    boolean isNumerator = true;
    for (int i = 0; i < source.length(); i++) {
        if (source.charAt(i) == '/') {
            isNumerator = false;

        }
        else if (isNumerator == true) {
            numerator += source.charAt(i);          
        }else{
            denominator += source.charAt(i);
        }
    }
             //converts and stores fractions.

    BigDecimal BD2 = new BigDecimal(new BigInteger(denominator, denominator.length()));
    BigDecimal BD1 = new BigDecimal(new BigInteger(numerator, numerator.length()));
    stack.push(BD1.divide(BD2, 20, BigDecimal.ROUND_FLOOR));
}

I got this error.

Exception in thread “main” java.lang.NumberFormatException: Radix out of range
at java.math.BigInteger.(Unknown Source)
at sorting.separateStrings(sorting.java:60)<—Being where I try to convert the numerator to BigInteger.

at sorting.main(sorting.java:26)<— where I call separateStrings.

The string that it broke on was numerator = “9821020392310432153773517114542035029”

I’m not sure why I’m getting this error. I was under the impression that BigInteger can be
up to length 50. Plus, it worked on the first line of txt which was “8731472450870/1572580584296783758”.

  • 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-12T07:04:17+00:00Added an answer on June 12, 2026 at 7:04 am

    The error is caused by the object:

    new BigInteger(denominator, denominator.length())
    

    which is attempting to create a BigInteger with radix of 37, exactly 1 more than the allowed maximum of 36.

    In this example you probably didn’t need to set the radix or even use BigInteger, so you could simply use:

    BigDecimal bigDecimal = new BigDecimal(denominator);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to take input from the keyboard and then store it in
I am trying to write a php script that take a text file break
Ok so I'm trying to take an input text file and justify it like
I'm trying to take statistics of a specific column in a text file and
I am trying to write a bash script that will take a text file
Im trying to take a text file and use only the first 30 lines
Part 1 I'm trying to read a text file and then copy into another
I have been trying to take chars from a txt file(in which the words
I am trying to write a program that will take a text file with
I'm trying to take a text string, (e.g. the word testing) and calculating if

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.