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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:44:33+00:00 2026-06-02T20:44:33+00:00

I am creating a program that will print out digits of pi up to

  • 0

I am creating a program that will print out digits of pi up to a number specified by the user. I can read the input from the user, I can read the text file, but when I print the number of digits, it prints out the wrong number.

“Pi.txt” contains “3.14159”.
Here is my code:

    package pireturner;

    import java.io.*;
    import java.util.Scanner;

    class PiReturner {

        static File file = new File("Pi.txt");
        static int count = 0;

        public PiReturner() {

        }
        public static void readFile() {
            try {
                System.out.print("Enter number of digits you wish to print: ");
                Scanner scanner = new Scanner(System.in);
                BufferedReader reader = new BufferedReader(new FileReader(file));
                int numdigits = Integer.parseInt(scanner.nextLine());

                int i;
                while((i = reader.read()) != -1) {
                    while(count != numdigits) {
                        System.out.print(i);
                        count++;
                    }
                }

            } catch (FileNotFoundException f) {
                System.err.print(f);
            } catch (IOException e) {
                System.err.print(e);
            }
        }            

        public static void main(String[] args ) {
            PiReturner.readFile();
        }
    }

This will print out “515151” if the user inputs 3 as the number of digits they wish to print. I do not know why it does this, and I am not sure what I am doing wrong, as there are no errors and I have tested the reading method and it works fine. Any help would be gladly appreciated. Thanks in advance.

By the way, casting integer ‘i’ to a char will print out 333 (assuming input is 3).

  • 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-02T20:44:33+00:00Added an answer on June 2, 2026 at 8:44 pm

    Your inner loop is not left before outputting numdigit times 3

             while (count != numdigits) {
                 System.out.print(i);
                 count++;
             }
    

    instead …

        int numdigits = Integer.parseInt (scanner.nextLine ());
        // for the dot
        if (numdigits > 1) 
            ++numdigits;
        int i;
    
        while ((i = reader.read ()) != -1 && count != numdigits) {
             System.out.print ((char) i);
             count++;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to design a report that will print text on specified x,y coordinates
I am creating an application where a user will input grades and the program
I'm creating a program that authenticates from a before it runs. I also want
I'm creating an object that will represent some system info that can be changed
Hey, I'm trying to write a program that will accept new tasks from people,
I have been tasked with creating a program that will generate an amortization schedule.
I am currently creating a program that dynamically adds Image to a dockPanel every
I am creating a program that communicates with a PHP script on a web
I am creating a client program that talks to a server (which I programmed).
When creating DLL files for a program that already exists, is it customary to

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.