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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:44:29+00:00 2026-05-12T13:44:29+00:00

Suppose my input file contains: 3 4 5 6 7 8 9 10 I

  • 0

Suppose my input file contains:

3 4 5 6    7        8
9


10

I want to run a while loop and read integers, so that I will get 3,4,5,6,7,8 and 10 respectively after each iteration of the loop.

This is really simple to do in C/C++ but not in Java…

I tried this code:

try {
            DataInputStream out2 = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));

            int i=out2.read();
            while(i!=-1){
                System.out.println(i);
                i=out2.readInt();
            }

    } catch (IOException ex) {

    }

and what I get is:

51
540287029
540418080
538982176
151599117
171511050
218762506
825232650

How do I read the integers from this file in Java?

  • 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-12T13:44:29+00:00Added an answer on May 12, 2026 at 1:44 pm

    One can use the Scanner class and its nextInt method:

    Scanner s = new Scanner("3  4        5   6");
    
    while (s.hasNext()) {
      System.out.println(s.nextInt());
    }
    

    Output:

    3
    4
    5
    6
    

    Basically by default, the Scanner object will ignore any whitespace, and will get the next token.

    The Scanner class as a constructor which takes an InputStream as the source for the character stream, so one could use a FileInputStream that opens the source of the text.

    Replace the Scanner instantiation in the above example with the following:

    Scanner s = new Scanner(new FileInputStream(new File(filePath)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a file that contains IP addresses, one address in each line:
Suppose there is a input file. The file contains php class with many functions
Suppose I have input[type=file] element and I want to intercept onclick event and prevent
Suppose that my Haskell function is given an input, which is supposed to be
Suppose I have a chain of streams, that does Compression -> Encryption -> File
We have a command line exe that takes input from a text file and
I want to delete a database row which contains a file name when the
Suppose I've got a list of files file1 file 1 file2 a for...in loop
I want to input code in Python like \input{Sources/file.tex} . How can I do
From an input file I'm suppose to extract only first name of the student

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.