View Single Post
Old Apr 5th, 2008, 4:29 PM   #6
dimlyBright
Newbie
 
dimlyBright's Avatar
 
Join Date: Feb 2008
Posts: 10
Rep Power: 0 dimlyBright is on a distinguished road
Re: Using ctrl+d to end input

This seems to work... I still don't think it is written correctly. I could not implement hasNextDouble() for some reason. Every time I hit ctrl-z (ctrl-d for linux) it crashed. I wrote comments to help me read it and know what is happening.

/*
 * import statements go at the top
 */
//import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

/*
 * javadoc goes here for author and description of the class and...
 */
/**
 * enter description of the class here:
 * @author Anonymous
 */

/*
 * This is how you make a public class
 */
public class Simplify {

  /*
   * javadoc goes here for description of the method and/or 
   * parameters (if any) and... 
   */
  /**
   * enter description of the method here:
   */

  /* this is how you make a public method named UseArrayList that
   * returns an array made up of objects
   */
  public Object[] UseArrayList(){

    /* 
     * An arrayList is a list you can use like an array... it 
     * grows as you need, because it is technically a list
     */      
    ArrayList <Double> collectInputs = new ArrayList<Double>();
    Scanner variNum;
    boolean ctrlD = true;      
    String instruct = "\nPlease enter a series of numbers, " +
    "each followed \nby the return key.  When you are finished,"+
    " use the\nkeyboard command ctrl+d to stop inputing " +
    "numbers";

    /*
     * instructions to the user
     */
    System.out.println(instruct);


    /*    
     * a try is to identify an error before it crashes your
     * program. Because I am parsing an input to a double, 
     * I need to make sure it catches non-number entries 
     * without crashing the program.
     */
    try{
//    variNum = new Scanner(new FileReader("Information"));

      variNum = new Scanner(System.in);

      /*       
       * this 'while' is always true.  This means it will loop 
       * forever unless I either use ctrl+d or give it something
       * the try will choke on, like a non-number for instance.
       * notice I made a variable named ctrlD and used it instead
       * of just writing true.  This is on purpose to allow me to
       * set the variable inside instantiated objects to false if
       * I wanted to.
       */
      while(ctrlD){

        /*
         * Parse a string input to a double and add it to the 
         * arrayList in one shot
         */
        collectInputs.add(variNum.nextDouble());

        /*
         * closes my while loop
         */
      }

      /*
       * closes my try statement
       */
    }

    /*
     * every 'try' must have a 'catch'.
     */
    catch(Exception e){     

      /*
       * check for empty array because you can't use toArray() on
       * an empty arrayList
       */
      if(collectInputs.isEmpty()){

        /*
         * Just writing something so if it happens I see it.
         */
        System.err.println("\nNo elements in the set.");

        /*
         * abandon the program if nothing was entered.  The
         * integer 1 is to signal a program failure.
         */
        System.exit(1);

        /*
         * closes my if statement
         */
      }else{

        /*
         * make a real array (i.e. not arraylist)
         */
        return collectInputs.toArray();


        /*
         * closes my else statement
         */
      }
    }
    /* because I have to. The method needs a return no matter 
     * what and the 'else' does not have one so if the program
     * were to go to the else statement the compiler thinks the
     * code would lead to a non-return, thus I write a bogus
     * return here to satisfy the compiler.
     */
    return null;

    /*
     * closes my method
     */
  }

  /*
   * closes my class
   */
}

Last edited by dimlyBright; Apr 5th, 2008 at 4:41 PM.
dimlyBright is offline   Reply With Quote