Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2008, 4:44 PM   #1
dimlyBright
Newbie
 
dimlyBright's Avatar
 
Join Date: Feb 2008
Posts: 10
Rep Power: 0 dimlyBright is on a distinguished road
Using ctrl+d to end input

What is the simplest way to implement a program that uses a loop to ask for a variable amount of inputs and when you are through giving it inputs you use the keyboard command "ctrl+d" ("ctrl+z" for windows).

Here is what I have and it crashes (throws and exception) every time i use "ctrl+d". What it does exactly is throw an exception, erase the contents of the array list and fall into the if statement and exits because the array is empty... I can't explain why it is doing all that. I just wanted to end the input loop with ctrl+d... I know how to end it by other means (i.e. a quit command like "qq").

import java.util.ArrayList;
import java.util.Scanner;
public class Simplify {

public Object [] UseArrayList(){
		
    ArrayList <Double> collectInputs = new ArrayList<Double>();
    Scanner variNum = new Scanner(System.in);
    Double makeNum = null;
    String endingCommand = "";
    boolean ctrlD = true;		

    try{
        while(ctrlD){
        endingCommand = variNum.next();
        makeNum = Double.parseDouble(endingCommand);
        collectInputs.add(makeNum);
        }
    }
    catch(Exception e){
        System.err.println("\nFailed to read input: "+endingCommand);
        if(collectInputs.isEmpty()){
            System.err.println("\nNo elements in the set.");
            System.exit(1);
        }else{
            return collectInputs.toArray();
        }
    }
return null;
}
}
dimlyBright is offline   Reply With Quote
Old Apr 4th, 2008, 5:06 PM   #2
glimmy
Programmer
 
glimmy's Avatar
 
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 glimmy is on a distinguished road
Send a message via AIM to glimmy
Re: Using ctrl+d to end input

I don't know much about Java, but I believe Ctrl-D is not a signal in Unix, but rather the EOF. Instead of trying to detect the keypress, you could try and detect the EOF.
glimmy is offline   Reply With Quote
Old Apr 5th, 2008, 11:46 AM   #3
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

How do you detect end of file (EOF)?
dimlyBright is offline   Reply With Quote
Old Apr 5th, 2008, 12:55 PM   #4
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Using ctrl+d to end input

maybe change while (ctrlD) to while (variNum.hasNext())
mbd is offline   Reply With Quote
Old Apr 5th, 2008, 3:08 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Using ctrl+d to end input

Using Scanner's nextDouble() and hasNextDouble() methods will obviate the need to manually parse each double.

An EOFException is thrown when the end of a file is encountered.
titaniumdecoy is offline   Reply With Quote
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
Old Apr 7th, 2008, 1:31 PM   #7
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 263
Rep Power: 2 Fall Back Son is on a distinguished road
Re: Using ctrl+d to end input

Like was already said, the hasNext type methods will check to see if there is input that matches and once there isn't it will terminate the loop. for example

while ( keyboard.hasNextInt() )
{
keyboard.nextInt();
System.out.println("read in an integer");
}

if you enter something like a-z, it will terminate. or you can press ctrl+z for windows and it will terminate. If you're looking for the simplest solution that answers your question. If you're stuck on figuring out why ctrl+d isn't working I'm not sure.
Fall Back Son is offline   Reply With Quote
Old Apr 12th, 2008, 9:23 AM   #8
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

Thanks for all the suggestions. I think I have it working up to par now.

Cheers,

dB
dimlyBright is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Input Output Function Error kewlgeye Visual Basic 9 Jan 26th, 2008 3:31 PM
Clearing the input box 357mag Java 5 Nov 1st, 2007 9:28 PM
can someone please help me with this tree cwl157 Java 19 Oct 27th, 2007 12:35 PM
Reading character input into an array (raw mode) shoeyfighter C 3 Nov 2nd, 2006 3:49 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC