Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
public class ShowHeapError {
  Vector<String> names = new Vector<String>();
  InputStreamReader input = new InputStreamReader(System.in);
  BufferedReader reader = new BufferedReader(input);

  public void addNames() throws IOException {
    while(true) {     
      // Adding unknown number of records to a list; user can exhaust the heap
      String newName = reader.readLine();
      if(!newName.equalsIgnoreCase("quit")) { // Enter "quit" to quit the program
        names.addElement(newName);
      } else {
    	break;
      }
    }
    // closeClose "reader" and "input"
  }

  public static void main(String[] args) throws IOException {
    ShowHeapError demo = new ShowHeapError();
    demo.addNames();
  }
}

...

Code Block
bgColor#FFcccc
/** Assuming the heap size as 512 MB (calculated as 1/4th of 2 GB RAM = 512 MB)
 *  Considering long values being entered (64 bits each, the max number of elements
 *  would be 512 MB/64bits = 67108864)
 */
public class ShowHeapError {
   Vector<Long> names = new Vector<Long>(); // Accepts unknown number of records
   long newID = 0L;
   int count = 67108865;
   int i = 0;
   InputStreamReader input = new InputStreamReader(System.in);
   Scanner reader = new Scanner(input);

   public void addNames(){
     do{
       // Adding unknown number of records to a list
       // The user can enter more number of IDs than what the heap can support and 
       // exhaust the heap. Assume that the record ID is a 64 bit long value
      
       System.out.print("Enter recordID (To quit, enter -1): ");
       newID = reader.nextLong();
       
       names.addElement(newID);
       i++;
     }while (i < count || newID != -1);
     // Close "reader" and "input"
   }

   public static void main(String[] args) {
     ShowHeapError demo = new ShowHeapError();
     demo.addNames();
   }
}

...