• need help with my code

    From Knute Johnson@21:1/5 to Karim omar on Mon Dec 12 15:16:08 2016
    On 12/12/2016 10:19, Karim omar wrote:
    when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message

    Exception in thread "main" java.lang.NullPointerException
    at Ifexercise1a.main(Ifexercise1a.java:18)

    and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.

    the complete code is

    import java.util.Scanner;

    public class Ifexercise1a {

    public static void main(String[] args) {

    Scanner myScanner = new Scanner(System.in);
    int age;
    double price;
    char reply;

    System.out.println("how old are you?");
    age = myScanner.nextInt();

    System.out.println("you have a coupon? (Y/N)");
    reply = myScanner.findInLine(".").charAt(0);

    if(age <= 12 || age > 65){
    price = 5.25;
    }else{
    price = 9.25;
    }
    if (reply == 'y' || reply == 'Y'){
    price-=2.00;
    }
    System.out.println("you will pay $" + price);
    }


    }


    I'm not sure what you are trying to do with the line 'reply = myScanner.findInLine(".").charAt(0)' but if you replace the
    .findInLine() with .next() it will work fine.

    --

    Knute Johnson

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Karim omar@21:1/5 to All on Mon Dec 12 08:19:41 2016
    when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message

    Exception in thread "main" java.lang.NullPointerException
    at Ifexercise1a.main(Ifexercise1a.java:18)

    and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself.

    the complete code is

    import java.util.Scanner;

    public class Ifexercise1a {

    public static void main(String[] args) {

    Scanner myScanner = new Scanner(System.in);
    int age;
    double price;
    char reply;

    System.out.println("how old are you?");
    age = myScanner.nextInt();

    System.out.println("you have a coupon? (Y/N)");
    reply = myScanner.findInLine(".").charAt(0);

    if(age <= 12 || age > 65){
    price = 5.25;
    }else{
    price = 9.25;
    }
    if (reply == 'y' || reply == 'Y'){
    price-=2.00;
    }
    System.out.println("you will pay $" + price);
    }


    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)