Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: hasNext, Ctrl-Z not working

  1. #1
    kahwawashay is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default hasNext, Ctrl-Z not working

    I am using Windows 7, netbeans 7.0.1 and the latest Java package
    I am writing a simple while loop with hasNext() method as its condition, and supposedly the end-of-input indicator for Windows is Ctrl+Z, but that does not work for me. I also tried Ctrl-d, which supposedly works for Macs, unix, etc, but that also does not work.

    Does anyone here know the proper end-of-input indicator?

  2. #2
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    Post your code, and I may be able to help. I know that ctrl+z is the "Stop" indicator in Terminal/Command Prompt.
    If I have helped you, + rep

  3. #3
    kahwawashay is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    import java.util.Scanner;
    public class HasNext {
    public static void main(String[] args) {
    int i=0;
    double grade;
    Scanner input = new Scanner(System.in);

    while(input.hasNext()) {
    System.out.print("input: ");
    grade = input.nextDouble();
    i++;
    }
    System.out.print("i = "+i);

    }
    }


    ...it never prints the last system.out statement because it never breaks the loop, and Ctrl-Z is supposed to break it.. ive tried in netbeans, eclipse, and command prompt

  4. #4
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    Ah, I see, you should try limiting the amount of inputs you can have to say maybe 10, with a for loop, otherwise it probably will never stop.
    If I have helped you, + rep

  5. #5
    kahwawashay is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    well, the point of this program is that I don't know ahead of time how many things I want to input. the hasNext() method is supposed to return false and hence break the loop once Ctrl-Z is typed on windows system, and ctrl-d for macs, unix, etc...but neither of those are working on my windows 7

  6. #6
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    Try using..
    Java Code:
    while(input >= 0 && input <= 100)
    inside the while loop already there.
    If I have helped you, + rep

  7. #7
    kahwawashay is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    what on earth is that? on what level of programming are you?

    could someone else please help me???

  8. #8
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    Judging by your programming, you are using a grade score system, so you only need 0 - 100. So in order to finish the program, just go outside the boundaries.
    If I have helped you, + rep

  9. #9
    kahwawashay is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    no I am actually trying to average an arbitrary amount of numbers, not just between 0 and 100..the name "grade" was just random...specifically, I want to see how hasNext() works

  10. #10
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    if you want to see how hasNext() works, read the API.
    If I have helped you, + rep

  11. #11
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    You could maybe prompt them for the number of grades they want to enter first, then loop that many times?

  12. #12
    kahwawashay is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    thats the first place i looked.
    unfortunately, they do not say what the end of input indicator is for each operating system

  13. #13
    kahwawashay is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: hasNext, Ctrl-Z not working

    Bestsanchez, the name "grades" was random, I actually just want to input an arbitrary amount of numbers, so I don't want to use counter controlled repetition, because I don't know how many number I am inputing before I start looping

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: hasNext, Ctrl-Z not working

    ive tried in netbeans, eclipse, and command prompt
    The code in #3 works fine for me.

    Note that Windows command processor may interpret Ctrl+Z as end-of-file, but there's no guarantee that other software will. When you use an IDE then System.in/System.out are commonly not the ordinary console streams, but are redirected to a little window where you see output and can enter input. The IDE may interpret Ctrl+Z as end-of-file, or it may not. Running your code reveals that Eclipse Helios (SR 2) does, and Netbeans 7.0 does not.
    DarrylBurke likes this.

  15. #15
    Vira Yatta is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Lightbulb Re: hasNext, Ctrl-Z not working

    In not work in netbean, it work only in cmd.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: hasNext, Ctrl-Z not working

    If works fine in Eclipse and in an ordinary terminal as well; I don't know Netbeans though ... (type ^Z followed by <enter>)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. hasNext call
    By scotts in forum New To Java
    Replies: 4
    Last Post: 06-20-2010, 04:58 PM
  2. Ctrl+X on JButton
    By tyroceur in forum New To Java
    Replies: 35
    Last Post: 06-16-2010, 05:20 PM
  3. Write ctrl-z characters
    By jithan in forum New To Java
    Replies: 3
    Last Post: 02-18-2009, 04:11 AM
  4. Using scanner.hasNext() but recognize return
    By ScKaSx in forum New To Java
    Replies: 5
    Last Post: 01-27-2009, 11:11 AM
  5. Run method on Ctrl+Shift+D
    By Echilon in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •