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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:26:33+00:00 2026-05-28T04:26:33+00:00

I’m running an online automatic program evaluation platform and for one of the exercises

  • 0

I’m running an online automatic program evaluation platform and for one of the exercises the Java “Scanner” is using way too much memory (we are just starting to support Java so the problem did not arise before). As we are teaching algorithmics to beginners we can’t just ask them to re-code it themselves by reading one byte after an other.

According to our tests, the Scanner is using up to 200 Bytes to read ONE integer…

The exercise : 10 000 integers, which window of 100 consecutive integers has the maximal sum ?

The memory usage is small (you only need to memorize the last 100 integers) but between a classical version with “Scanner / nextInt()” and the manual version (see below) we can see a difference of 2.5 Mb in memory.

2.5 Mb to read 10 000 integers ==> 200 Bytes to read one integer ??

Is there any easy solution that one could explain to a beginner or is the following function (or similar) the way to go ?


Our test-function to read integers much faster while using much less memory :

public static int read_int() throws IOException
   {
     int number = 0;
     int signe = 1;

     int byteRead = System.in.read();
     while (byteRead != '-'  && ((byteRead < '0') || ('9' < byteRead)))
       byteRead = System.in.read();
     if (byteRead == '-'){
       signe = -1;
       byteRead = System.in.read();
     }
     while (('0' <= byteRead) && (byteRead <= '9')){
        number *= 10;
        number += byteRead - '0';
        byteRead = System.in.read();
     }
     return signe*number;
   }

Code using Scanner, as requested :

import java.util.Scanner;

class Main {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int nbValues = sc.nextInt();
      int widthWindow = sc.nextInt();

      int values[] = new int[widthWindow];

      int sumValues = 0;
      for (int idValue = 0; idValue < widthWindow; idValue++){
         values[idValue] = sc.nextInt();
         sumValues += values[idValue];
      }

      int maximum = sumValues;
      for (int idValue = widthWindow; idValue < nbValues; idValue++)
      {
         sumValues -= values[ idValue % widthWindow ];
         values[ idValue % widthWindow ] = sc.nextInt();

         sumValues += values[ idValue % widthWindow ];
         if (maximum < sumValues)
             maximum = sumValues;
      }
      System.out.println(maximum);
   }
}

As requested, memory used as a function of the number of integers :

  • 10,000 : 2.5Mb
  • 20,000 : 5Mb
  • 50,000 : 15Mb
  • 100,000 : 30Mb
  • 200,000 : 50Mb
  • 300,000 : 75Mb
  • 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-28T04:26:34+00:00Added an answer on May 28, 2026 at 4:26 am

    We finally decided to re-write (part of) the Scanner class. This way we only need to include our Scanner instead of the Java’s one and the rest of the code remains the same. We don’t have any memory problem anymore and the programs are 20 times faster.

    The code below is from Christoph Dürr, one of my colleague :

    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    class Locale {
       final static int US=0;
    }
    
    public class Scanner {
       private BufferedInputStream in;
    
       int c;
    
       boolean atBeginningOfLine;
    
       public Scanner(InputStream stream) {
          in = new BufferedInputStream(stream);
          try {
             atBeginningOfLine = true;
             c  = (char)in.read();
          } catch (IOException e) {
             c  = -1;
          }
       }
    
       public boolean hasNext() {
          if (!atBeginningOfLine) 
             throw new Error("hasNext only works "+
             "after a call to nextLine");
          return c != -1;
       }
    
       public String next() {
          StringBuffer sb = new StringBuffer();
          atBeginningOfLine = false;
          try {
             while (c <= ' ') {
                c = in.read();
             } 
             while (c > ' ') {
                sb.append((char)c);
                c = in.read();
             }
          } catch (IOException e) {
             c = -1;
             return "";
          }
          return sb.toString();
       }
    
       public String nextLine() {
          StringBuffer sb = new StringBuffer();
          atBeginningOfLine = true;
          try {
             while (c != '\n') {
                sb.append((char)c);
                c = in.read();
             }
             c = in.read();
          } catch (IOException e) {
             c = -1;
             return "";
          }
          return sb.toString();   
       }
    
       public int nextInt() {
          String s = next();
          try {
             return Integer.parseInt(s);
          } catch (NumberFormatException e) {
             return 0; //throw new Error("Malformed number " + s);
          }
       }
    
       public double nextDouble() {
          return new Double(next());
       }
    
       public long nextLong() {
          return Long.parseLong(next());
       } 
    
       public void useLocale(int l) {}
    }
    

    It is possible to be even faster by integrated the code in my question where we are “building” numbers by reading caracter after caracter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.