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 3 of 3

Thread: why i am getting this error

  1. #1
    Junior Member
    Join Date
    May 2024
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default why i am getting this error

    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class RegexDemo2 {
    public static boolean check(String str){



    if(Pattern.matches("SPC[0-9]{3}(0[1-9]|1[0-2])(AM|PM)", str))
    return true;
    else
    return false;

    }

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your name");
    String name=sc.nextLine();
    System.out.println("enter your id");
    String id=sc.nextLine();
    if(id.length()!=10){
    System.out.println("Inavlid");
    }
    else if(!check(id)){
    System.out.println("Invalid id");
    }
    else{
    System.out.println("Hi "+name+" your seat number is "+Integer.parseInt(id.substring(3, 6))+" and the event starts at"+Integer.parseInt(id.substring(6,8))+Integer.pa rseInt(id.substring(8, 10)));
    }




    }}

    output:-

    vivek
    enter your id
    SPC01004PM
    Exception in thread "main" java.lang.NumberFormatException: For input string: "PM"
    at java.base/java.lang.NumberFormatException.forInputString(Num berFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:665)
    at java.base/java.lang.Integer.parseInt(Integer.java:781)
    at RegexDemo2.main(RegexDemo2.java:30)
    Last edited by Vivekvt08; May 18th, 2024 at 02:27 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: why i am getting this error

    java.lang.NumberFormatException: For input string: "PM"
    "PM" is not numeric. parseInt requires numeric data.

    Check that the substring method's indexes match the locations of the numeric parts of the input String.
    Write down the input String on one line and write the index values from 0 to 9 below the String.
    Then check that each of the substrings are for numeric data expected by the parseInt methods.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Vivekvt08 (May 18th, 2024)

  4. #3
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: why i am getting this error

    The error you're encountering seems to be related to parsing a string into an integer. Specifically, it's happening in this line:

    ```java
    System.out.println("Hi "+name+" your seat number is "+Integer.parseInt(id.substring(3, 6))+" and the event starts at"+Integer.parseInt(id.substring(6,8))+Integer. pa rseInt(id.substring(8, 10)));
    ```

    The error message `java.lang.NumberFormatException: For input string: "PM"` indicates that the string "PM" is causing the issue because you're trying to parse it as an integer, which isn't possible.

    The problem arises because you're concatenating the results of `parseInt` without providing spaces or any separators between them. As a result, "PM" is getting concatenated directly after the result of `parseInt(id.substring(6,8))`, causing it to be treated as part of the string to be parsed.

    To fix this, you should ensure proper separation between the parsed integers and the string "PM". Here's the corrected line:

    ```java
    System.out.println("Hi "+name+" your seat number is "+Integer.parseInt(id.substring(3, 6))+" and the event starts at "+Integer.parseInt(id.substring(6,8)) + " " + Integer.parseInt(id.substring(8, 10)) + " " + id.substring(10));
    ```

    I've added spaces between the parsed integers and the string "PM" so that it's clear where each part begins and ends. This should resolve the `NumberFormatException` issue.

    For any challenges with your Java assignments, don't hesitate to seek extra support. Collaborating with peers or exploring online resources like ProgrammingHomeworkHelp.com can provide valuable insights and assistance in overcoming hurdles.

Similar Threads

  1. Replies: 0
    Last Post: January 18th, 2021, 03:59 AM
  2. Replies: 3
    Last Post: May 8th, 2014, 06:25 AM
  3. Replies: 2
    Last Post: March 23rd, 2014, 08:44 AM
  4. Replies: 3
    Last Post: November 30th, 2013, 05:52 PM
  5. Problems with A* Map Search - GC Overload Error and Null Error
    By puneeth.meruva in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2013, 03:01 PM