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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:52:04+00:00 2026-05-21T19:52:04+00:00

I have the following code but I don’t understand how I can reset the

  • 0

I have the following code but I don’t understand how I can reset the pointer to the starter position:

BufferedReader inp=new BufferedReader(new FileReader(file));
Scanner leggi=new Scanner(inp);
for(int i=0;i<nwords;i++){
  while(leggi.hasNext()) 
    if(leggi.next().equals(args[i+2])) 
      occorrenze[i]=occorrenze[i]+1;
}
inp.close();

I tried

inp.mark(0);
inp.reset();

with no results.

  • 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-21T19:52:05+00:00Added an answer on May 21, 2026 at 7:52 pm

    Paul,

    I suggest you read through this old thread: Java BufferedReader back to the top of a text file?.

    Personally I prefer Jon Skeet’s response, which boils down to “Don’t bother [unless you MUST].”

    Cheers. Keith.


    EDIT: Also you should ALLWAYS close that input file, even if you hit an Exception. The finally block is perfect for this.


    EDIT2:

    Hope you’re still with us.

    Here’s my attempt, and FWIW, you DON’T need to reset the input-file, you just need to transpose your while and for loops.

    package forums;
    
    import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class WordOccurrenceCount
    {
      public static void main(String[] args) {
        try {
          String[] words = { "and", "is", "a", "the", "of", "as" };
          int[] occurrences = readOccurrences("C:/tmp/prose.txt", words);
          for ( int i=0; i<words.length; i++ ) {
            System.out.println(words[i] + " " + occurrences[i]);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
      private static final int[] readOccurrences(String filename, String... words) 
        throws IOException
      {
        int[] occurrences = new int[words.length];
        BufferedReader reader = null;
        try {
          reader = new BufferedReader(new FileReader(filename));
          Scanner scanner = new Scanner(reader);
          while ( scanner.hasNext() ) {
            String word = scanner.next();
            for ( int i=0; i<words.length; i++ ) {
              if ( words[i].equals(word) ) {
                occurrences[i]++;
              }
            }
          }
        } finally {
          if(reader!=null) reader.close();
        }
        return occurrences;
      }
    }
    

    And BTW, java.util.Map is perfect for building a frequency table… Parallel arrays are just SOOOOO 90’s. The “default” implementation of Map is the HashMap class… By default I mean use HashMap whenever you need a Map, unless you’ve got a good reason to use something else, which won’t be often. HashMap is generally the best allround performer.

    • 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 which is working, but I don't understand it 100%
i have following code for moving 4 picture around circular way,but it can rotate
I have the following code. but it causes exception. using (var context = new
I have the following code but can't compile it because I have a Type
I have the following code. I feel like I'm being redundant but don't know
I have the following code but it doesnt seem to work: <td align=center style=padding-bottom:52.5px>
I have the following code but the lon/lat seems to be returning null; package
I have the following code but I am unsure how I would adjust it
I have following asp.net code but it gives error when I change dropdown selected
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,

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.