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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:42:47+00:00 2026-06-04T17:42:47+00:00

I have the following code and an input file that has all numbers such

  • 0

I have the following code and an input file that has all numbers such that each line in the txt file has only one number. I print each number on each line onto standard out and I stop printing if I encounter the number 42. But the problem is my scanner object which i’m using to read the file is not displaying the first number and is only printing from the second number of my txt file. I think this has something to do with the scanner.nextline function I don’t know but I wish scanner had a getcurrent or something like that to make things simpler. Anyway could anyone please tell me how to solve this problem and get the first line to display.

Here’s my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class driver {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub


        File theFile = new File("filepath.../input.txt");

        Scanner theScanner = new Scanner(theFile);

        //so that the scanner always starts from the first line of the document.
        theScanner.reset();


        while(theScanner.hasNext())
        {
            if(theScanner.nextInt() == 42)
            {
                break;
            }

            System.out.println(theScanner.nextInt());

        }
    }

}
  • 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-04T17:42:48+00:00Added an answer on June 4, 2026 at 5:42 pm

    I was calling the nextInt() method twice on the scanner object before I printed out to standard output. Once in the if statement and again in the System.out.println. Hence the scanner started printing from the second line of the txt file.

    But the solution would be including a line of code like the following:

     int temp = theScanner.nextInt();
    

    before the if statement and then modifying the if statement to :

    if(temp == 42)
       { 
          break;
    
       }
    
       System.out.println(temp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to open a input and output file: if ((source_file_ptr
I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
I have the following code: main = do putStr Test input : content <-
i have the following code : <input type=text value=<?php echo $_GET['msg']; ?>> This input
I have the following code, ... <input type=hidden name=unchecked id=unchecked value= /> <?php $ind
I have the following code: HTML <div id='foo'> <input type='checkbox' value='english' name='bar[english]' /> <input
I have the following code: ​<div id=container> <input type=text name=a1 id=a1> <input type=text name=a2
I have the following code: index.jsp: <form name=f action=db/ajoutConducteur.jsp method=post> <input type=text pattern=\d{8} required
I have the following code: echo ' <td> <input type=button name=delete value=X onclick=clearSelection(this.form, '.$type.');this.form.submit();
I have the following code for uploading a csv file to a mysql database.

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.