Results 1 to 9 of 9
  1. #1
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default printing out errors

    Hi i have some noobish questions for java
    I am making a program to enter a positive value into the array and i want the
    program to give out an error if i input any negative or strings and decimals in the array

    for negative i get that i use
    if (number < 0)

    but how do i do it for strings and decimals?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: printing out errors

    That's work for decimals, and for Strings you'll need to turn them into a number using parseInt or parseFloat methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: printing out errors

    Tolls, I'm not sure that's quite what gnawk was asking about.

    gnawk - what defines a decimal? It containing a decimal point? Or just having digits after the decimal point? n%1 might help you here.

    As for a string - could you check to see if the input contains non-number chars?

    EDIT: Try doing Integer.parseInt("esdF") and parseInt("8.8") and see what happens - maybe that could be of use!
    Last edited by Diargg; 07-10-2012 at 03:17 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: printing out errors

    Ah.
    I see.
    Your interpretation probably does make more sense...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: printing out errors

    i used try catch exception print out errors
    try{
    System.out
    .println("Type in postivie distance you wish to delete from the array.");
    del = inp.nextInt(); // delete the number that was inputed from the array
    }
    catch(InputMismatchException ime)
    {
    System.out.println("Input error. Must enter an integer. Returning to main menu.");
    inp.nextLine(); // flush the String that is not an int
    }

  6. #6
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: printing out errors

    That should work fine. (Assuming it does what you intended it to do! Something of a tautology there...)

  7. #7
    FussyCashew is offline Member
    Join Date
    Jun 2012
    Location
    Ohio
    Posts
    10
    Rep Power
    0

    Default Re: printing out errors

    I think gnawk wants an error to be thrown if someone enters a non-integer, so parseInt() is what he/she is looking for.

  8. #8
    FussyCashew is offline Member
    Join Date
    Jun 2012
    Location
    Ohio
    Posts
    10
    Rep Power
    0

    Default Re: printing out errors

    Quote Originally Posted by gnawk View Post
    i used try catch exception print out errors
    try{
    System.out
    .println("Type in postivie distance you wish to delete from the array.");
    del = inp.nextInt(); // delete the number that was inputed from the array
    }
    catch(InputMismatchException ime)
    {
    System.out.println("Input error. Must enter an integer. Returning to main menu.");
    inp.nextLine(); // flush the String that is not an int
    }
    Note, when printing an error, you can use System.err.println()

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: printing out errors

    Which will go to the same place as out, since the OP is writing a console app.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  2. printing errors
    By droidus in forum New To Java
    Replies: 5
    Last Post: 12-07-2011, 09:53 PM
  3. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM
  4. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

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