Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trying to use CTRL-D to terminate the program (sentinel value)

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Trying to use CTRL-D to terminate the program (sentinel value)

    The overall homework assignment is not difficult, but I am having a bit of trouble figuring out how to use the CTRL-D in my program. The program records the amount of times a positive and negative integer is typed. When the user decides to end the program, they will use the CTRL-D to terminate the program. After the button combination has been pressed, I spit out to the user the results. The problem is when I press CTRL-D the program just terminates and I can't even get it to read the next line of code. Any help that can possibly point me in the right direction will be greatly appreciated.

    --- Update ---

    PHP Code:
      public static void main(String[] args) {
            
    // number entered and the running total
            
    int inputNumsum 0;
    // count of valid and invalid numbers seen
            
    int numValid 0numInvalid 0;
            
    double average;
            
    Scanner sr = new Scanner(System.in);
            while (
    sr.hasNextLine()) {

                
    System.out.println("CTRL-Z");

                
    int inputNumber sr.nextInt();
                
    System.out.println(inputNumber);

            }
            
    System.out.println("Test");
        }

    If Control D is pressed, it does print out the CTRL-Z, which is progress. The problem is if the user enters a numerical value and presses CTRL -D it does print out "CTRL-Z". I am clearly missing something hear. Is there a way to have the user entering numbers in, and once he presses CTRL-Z it can either say "CTRL-Z" or get out of the while loop and print out "Test".


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Trying to use CTRL-D to terminate the program (sentinel value)

    Ok, first off are you using Windows or a Unix based Operating System? Second, if you are only concerned about catching integer values then all of your scanner methods should be those that deal with integers.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to use CTRL-D to terminate the program (sentinel value)

    Are you getting an error when the program terminates?

    Edit: Try using hasNext() instead of hasNextLine(). Also, I know it's a test/demo program and a work in progress, but the local variable inputNumber inside the while loop is pretty much pointless. Use inputNum.

  4. #4
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Trying to use CTRL-D to terminate the program (sentinel value)

    Quote Originally Posted by GregBrannon View Post
    Are you getting an error when the program terminates?

    Edit: Try using hasNext() instead of hasNextLine(). Also, I know it's a test/demo program and a work in progress, but the local variable inputNumber inside the while loop is pretty much pointless. Use inputNum.
    This worked. Thank you. Why did hasNext work and not hasnextLine not work?

Similar Threads

  1. [SOLVED] Need help with Sentinel Control Loop
    By Roku in forum Loops & Control Statements
    Replies: 1
    Last Post: March 21st, 2013, 06:52 AM
  2. javaw.exe (JVM) does not terminate
    By jeffwg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 13th, 2013, 11:03 AM
  3. Replies: 2
    Last Post: July 15th, 2012, 09:19 AM
  4. help me terminate the infinite loop.
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 10th, 2012, 03:40 AM
  5. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM

Tags for this Thread