pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Java pastebin - collaborative debugging tool View Help


Posted by repsilat on Sat 7 Nov 03:46
report abuse | download | new post

  1. //(c) Matthew Steel 2009-, no warranty etc, licensed under the WTFPL v2.
  2. //Finds when all four digit numbers have gone through stdin.
  3.  
  4. import java.io.IOException;
  5. public class Searcher {
  6.  
  7.         public static void main(String[] args) {
  8.                 int unseen = 10000;
  9.                 boolean[] seen = new boolean[10000];
  10.  
  11.                 int number=0;
  12.                 for(int i = 0; i < 3; ++i)
  13.                         number = 10*number + (readChar());
  14.  
  15.                 int digitsSeen;
  16.                 for(digitsSeen = 3; unseen > 0; ++digitsSeen) {
  17.                         number = (10*number + (readChar()))%10000;
  18.                         if(!seen[number]) {
  19.                                 seen[number] = true;
  20.                                 --unseen;
  21.                         }
  22.                 }
  23.                 System.out.println(number);
  24.                 System.out.println(digitsSeen);
  25.         }
  26.         private static int readChar() {
  27.                 try {
  28.                         int c = System.in.read();
  29.                         if (c == -1) throw new IOException("End of file");
  30.                         //System.out.println("a");
  31.                         return c - '0';
  32.                 } catch (IOException ioe) {
  33.                         ioe.printStackTrace();
  34.                         System.exit(1);
  35.                 }
  36.                 return 0;
  37.         }
  38. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post