Results 1 to 8 of 8
  1. #1
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Stopping a program

    A small program I'm making has to take input as a String and I want to make it so that if the input string was "-1" it would skip the code and end, having not done anything. I did a separate program had the same thing but it was an int instead of a string as input. That looked like

    Java Code:
    while (int != -1){
    do stuff here
    }
    end of program
    I tried doing an Integer.parseInt() but then it doesn't accept anything that's not -1. Any suggestions?
    Last edited by Majeh; 02-08-2011 at 08:16 AM.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Show us your code.
    What is your input? And what is the output? Do you receive exception?

  3. #3
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
                 System.out.println("Enter string here");
                 textString = input.nextLine();
            
    (Line35)     while (Integer.parseInt(textString) != -1){
            
                 stuff happens here
    
                 }
    Exception in thread "main" java.lang.NumberFormatException: For input string: "asdf"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at cats.Main.main(Main.java:35)

    Does this help? I don't really know too much about Java so I could be using Integer.parseInt() wrong or not even need to use it.
    Last edited by Majeh; 02-08-2011 at 08:36 AM.

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Your input is "asdf"? Integer.parseInt converts string input (but in number form (like 1,2,3 etc)). "asdf" is not a numbers.

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

    Default

    Instead of taking the input string and parseing it to get an int to compare with the int -1, just take the string itself and compare it to the String "-1".

    The String class has an equals() method for just such a comparison.

  6. #6
    Majeh is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    I see. That does make more sense, but looking at the syntax confuses me a bit and I don't know how I would set it up to compare it to "-1" using equals(). Thanks for the help though, I appreciate it.

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    As you are in the starting stage u can do this.
    Accept a string.
    the do maniplation like this below
    textString =input.nextLine().trim();
    //then compare like this whether user mistakenly entering mixed case with space also it //will trim and compare ignoring spaces till he types exit as an input
    while(!textString.equalsIgnoreCase("exit"))
    {
    do something
    }
    Ramya:cool:

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

    Default

    the syntax confuses me a bit and I don't know how I would set it up

    If RamyaSivakanth's post clears things up, say that.

    Otherwise post your attempt (with any compiler messages if it goes wrong). It is important that you make that attempt because learning how to use the equals() method - or other methods - is a large part of the reason for this problem. Merely acquiring the code - even if it wroks - isn't learning.

Similar Threads

  1. Stopping threads
    By phil128 in forum Threads and Synchronization
    Replies: 4
    Last Post: 02-27-2014, 09:21 PM
  2. stopping the thread
    By kailash in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-06-2011, 02:21 PM
  3. Stopping a thread
    By Arne in forum Threads and Synchronization
    Replies: 9
    Last Post: 10-21-2010, 11:26 AM
  4. Stopping a thread
    By userj2ee in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 08:57 PM
  5. Stopping a .swf with java
    By ercarls in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 07:33 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
  •