Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Unhappy I need help with my code!!

    So i am going to code a program that counts ow may hours it takes to get on place to another.
    I am making a feature that asks you would you like to count the time when you will arrive there.

    I have coded it like "Do you like to count when will you arrive y/n".
    Heres the code:

    System.out.println("Would you like to count the time whne you will arrive : y/e");


    Boolean y,n;
    y = bucky.nextBoolean();
    n = bucky.nextBoolean();

    if (y = bucky.nextBoolean());


    And when i try to run the program i count the time the trip will take and the it asks do i want to count the arrival time and i put y and the it shows an arror and shows the line number where the error is.
    and it shows this line

    y = bucky.nextBoolean();
    n = bucky.nextBoolean();

    so could you help me with this code please!!!!!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    And you don't think it's important to show us what the error is?

    If you want help with code, make sure the code you post is in SSCCE form. Don't forget the code tags.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Here's the whole code and error message!

    ^^
    ||| Title!

    import java.util.Scanner;
    public class apples {
    public static void main (String []args){
    Scanner bucky = new Scanner (System.in);

    Double matka,nopeus,keskinopeus;
    System.out.println("Ohjelman koodaus by Samppa");
    System.out.println("Anna matkan pituus kilometreinä");
    matka = bucky.nextDouble();
    System.out.println("Anna keskinopeus");
    nopeus = bucky.nextDouble();
    keskinopeus = matka / nopeus ;
    System.out.println("Matkasi kestää:");
    System.out.print(keskinopeus);
    System.out.println("Tuntia");
    System.out.println("Would you like to count the time whne you will arrive : y/e");


    Boolean y,n;
    y = bucky.nextBoolean();
    n = bucky.nextBoolean();

    if (y = bucky.nextBoolean());

    System.out.println("Anna lähtöaika tunteina ja minuutteina");
    Double aika,tulos;
    aika = bucky.nextDouble();
    tulos = aika + keskinopeus;
    System.out.println("Olet paikalla Klo :");
    System.out.println(tulos);
    return;

    }
    }


    And then the error message!

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextBoolean(Unknown Source)
    at apples.main(apples.java:21)

    And i have coded this program in eclipse.
    Look the zip file i attached theres 3 pictures.
    1 of the error and 2 of the whole code.
    Please help me =)
    Attached Files Attached Files

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Didn't I say don't forget the code tags? Also, don't you think you should have told us what you're trying to input when you get that error?

    Anyway, your problem, I'm guessing, is that nextBoolean is expecting "true" or "false", not "y" or "n" or "e" or anything else. What is the input when you get that error?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Reply!

    Sorry but i am new out here and in java and i don't know aht code tags does mean.
    And the input is y.
    And if you know how can i fix my problem, i am all ears.
    Thanks for helping me!

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Check out anybody else's post (by hitting the quote button) to check out how to use the code tags.

    But I already told you what the problem was- the nextBoolean() method is expecting the Strings "true" or "false" (case insensitive). You're trying to input a "y". That won't work.

    Either change your input to "true" or "false", or read the input in as a String. Check out the API for useful functions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    As Kevin says, nextBoolean() expects "true" or "false".
    If you want to do "y" or something then you'll have to read the String and interpret it yourself.

    ETA: Damn. Too slow.

  8. #8
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Reply 2

    So could either of you change that piece of the code like that it works.
    Becuse i really don't understand that "true" or "false" thingy yet.
    But if you could edit it like that it works i could understand how to do it =D
    Thanks for helpingme out!

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Sorry, that's not really how this works. What part about what we're saying don't you understand?

    To use nextBoolean(), your input must be either "true" or "false", not "yes" or "no", not "y" or "n". True or false.

    If you want to take in other input, use a method that reads in a String. The API is your friend: Scanner (Java Platform SE 6)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Thanks For Helping!

    Thank you for helping me.
    Now i understand how this Boolean thing works.!
    I am so glad now!
    Again! Thank you very mutch!D

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •