Advertise on Bikeforums.net



User Tag List

Results 1 to 14 of 14
  1. #1
    la vache fantôme phantomcow2's Avatar
    Join Date
    Aug 2004
    Location
    NH
    Posts
    6,266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Including a "while loop" in my java program.

    Can someone tell me what is wrong with my program? I'm trying to set up a while loop so that I read in the number of test cases, then the program asks that many times what the values of x1, x2, y1, and y2 are to compute that distance. For some reason when I run the program it doesn't do anything like it's waiting for an input.



    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class ComputeDistance
    {
    public static void main (String[] arv)
    {
    double x1;
    double x2;
    double y1;
    double y2;
    double d1;
    double distance=0;
    int testno;

    Scanner scan=new Scanner (System.in);
    DecimalFormat answer = new DecimalFormat("0.###");
    testno=scan.nextInt();
    int count = 1;
    while (count <testno)
    {
    System.out.println("Value of x1?");
    x1=scan.nextDouble();
    System.out.println("Value of y1?");
    y1=scan.nextDouble();
    System.out.println("Value of x2?");
    x2=scan.nextDouble();
    System.out.println("Value of y2?");
    y2=scan.nextDouble();
    d1=((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2));
    distance=Math.sqrt(d1);
    count=count++;
    System.out.println(answer.format(distance));
    **
    **
    **
    C://dos
    C://dos.run
    run.dos.run

  2. #2
    That darn Yankee TexasGuy's Avatar
    Join Date
    Jun 2005
    Location
    West West Fort Worth
    My Bikes
    Mongoose XR-100, Eros Bianchi
    Posts
    4,286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol

    that's probably because it's waiting for input.
    Life is about hanging onto what you think is important and finding out what really is important.
    "Stop Ruining my joke!", "No, a joke implies humor attached at no additional cost"
    So many sayings, so little sig space.

  3. #3
    Look! My Spine! RubenX's Avatar
    Join Date
    Apr 2008
    Location
    Winter Springs, FL
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What's the value of testno at the beginning of your program? Print that value out.

    I'm also curious about this lines:

    System.out.println("Value of x1?");

    will it print without a "\n" before waiting for the input?
    "Hoy es un dia normal, pero yo voy a hacerlo intenso" ~ Juanes

  4. #4
    That darn Yankee TexasGuy's Avatar
    Join Date
    Jun 2005
    Location
    West West Fort Worth
    My Bikes
    Mongoose XR-100, Eros Bianchi
    Posts
    4,286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps putting a
    Code:
    System.out.println("How many coordinate distances do you wish to find?");
    prior to

    Code:
    testno=scan.nextInt();
    will show you where you're at.

    Also. You'll probably want to fix
    Code:
    int count = 1;
    while (count <testno)
    Think what will happen if the user inputs 1.
    Life is about hanging onto what you think is important and finding out what really is important.
    "Stop Ruining my joke!", "No, a joke implies humor attached at no additional cost"
    So many sayings, so little sig space.

  5. #5
    Look! My Spine! RubenX's Avatar
    Join Date
    Apr 2008
    Location
    Winter Springs, FL
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Don't you love it when we get a chance to talk geeky?

    and also... some data validation should be put into play... never trust a user's input, ever.
    "Hoy es un dia normal, pero yo voy a hacerlo intenso" ~ Juanes

  6. #6
    That darn Yankee TexasGuy's Avatar
    Join Date
    Jun 2005
    Location
    West West Fort Worth
    My Bikes
    Mongoose XR-100, Eros Bianchi
    Posts
    4,286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is obviously a school project. Doing too much will often mean you cheated and will quite often automatically get you an F.
    Life is about hanging onto what you think is important and finding out what really is important.
    "Stop Ruining my joke!", "No, a joke implies humor attached at no additional cost"
    So many sayings, so little sig space.

  7. #7
    You Know!? For Kids! jsharr's Avatar
    Join Date
    Apr 2005
    Location
    Just NW of Richardson Bike Mart
    My Bikes
    '05 Trek 1200 / '90 Trek 8000 / '? Falcon Europa
    Posts
    6,117
    Mentioned
    12 Post(s)
    Tagged
    3 Thread(s)
    this thread has nothing to do with coffee that I can see!?
    Are you a registered member? Why not? Click here to register. It's free and only takes 27 seconds! Help out the forums, abide by our community guidelines.
    Quote Originally Posted by colorider View Post
    Phobias are for irrational fears. Fear of junk ripping badgers is perfectly rational. Those things are nasty.

  8. #8
    la vache fantôme phantomcow2's Avatar
    Join Date
    Aug 2004
    Location
    NH
    Posts
    6,266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TexasGuy View Post
    This is obviously a school project. Doing too much will often mean you cheated and will quite often automatically get you an F.
    Yep. And I'd feel great shame if this was anything other than a practice homework assignment.
    C://dos
    C://dos.run
    run.dos.run

  9. #9
    la vache fantôme phantomcow2's Avatar
    Join Date
    Aug 2004
    Location
    NH
    Posts
    6,266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the comments . Forgetting the input was pretty dumb; I'm still a noob.
    C://dos
    C://dos.run
    run.dos.run

  10. #10
    Senior Member
    Join Date
    May 2005
    Location
    Terre Haute, Lafayette, or Indianapolis, IN, depending on the day
    My Bikes
    n, I would like n+1
    Posts
    1,917
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RubenX View Post
    Don't you love it when we get a chance to talk geeky?

    and also... some data validation should be put into play... never trust a user's input, ever.
    Attached Images Attached Images

  11. #11
    RacingBear UmneyDurak's Avatar
    Join Date
    Dec 2004
    Location
    NorCal
    Posts
    8,186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by phantomcow2 View Post
    Can someone tell me what is wrong with my program?
    You are using JAVA.

    Quote Originally Posted by jccaclimber View Post
    I have that outside my cubicle. Along with this. Not sure how my manager feels about it...
    I see hills.... Bring them on!!!
    Stay calm and bring a towel.

  12. #12
    Senior Member mikeybikes's Avatar
    Join Date
    May 2009
    Location
    Edgewater, CO
    My Bikes
    Tons
    Posts
    3,214
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Make sure your program starts with :

    while(1) continue;
    My Bikes: 2010 Breezer Uptown EX | 1980 Miyata 610 | 1970 Hercules | 198? Miele ?
    Wife's Bike: 2008 Globe City 7

    My Blog, a mix of bicycling, tech, and family.

  13. #13
    That darn Yankee TexasGuy's Avatar
    Join Date
    Jun 2005
    Location
    West West Fort Worth
    My Bikes
    Mongoose XR-100, Eros Bianchi
    Posts
    4,286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mikeybikes View Post
    Make sure your program starts with :

    while(1) continue;
    Life is about hanging onto what you think is important and finding out what really is important.
    "Stop Ruining my joke!", "No, a joke implies humor attached at no additional cost"
    So many sayings, so little sig space.

  14. #14
    That darn Yankee TexasGuy's Avatar
    Join Date
    Jun 2005
    Location
    West West Fort Worth
    My Bikes
    Mongoose XR-100, Eros Bianchi
    Posts
    4,286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by UmneyDurak View Post
    You are using JAVA.



    I have that outside my cubicle. Along with this. Not sure how my manager feels about it...
    Life is about hanging onto what you think is important and finding out what really is important.
    "Stop Ruining my joke!", "No, a joke implies humor attached at no additional cost"
    So many sayings, so little sig space.

Posting Permissions

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