views:

69

answers:

3

This is my situation:

i need the code to run in a loop, over and over again asking the same question(s) to the user, until the user types a "q" for any point to terminate/exit the loop, thus exiting the program.

The problem is that i tried to used a do-while/while loop, and those loops executes only if the conditions comes out to be true. But i need the condition ("q") to be false so it can continue the loop. If the condition is true (input.equals("q")) , then it just does not nothing because instead of the integer/double, it will use a string ("q") to calculate the distance.

i have already figured out how to get the distance, the code as is works well, but is there any work-around that i can make the loop continue while the condition is false?

and by the way, i am just barely learning java just in case...

'.

import java.*;
public class Points {
public static void main(String[] args){

    java.util.Scanner input = new java.util.Scanner(System.in);

    System.out.print("Enter the first X coordinate: ");
    double x1 = input.nextDouble();
    System.out.print("Enter the first Y coordinate: ");
    double y1 = input.nextDouble();
    System.out.print("Enter the second X coordinate: ");
    double x2 = input.nextDouble();
    System.out.print("Enter the second Y coordinate: ");
    double y2 = input.nextDouble();
    System.out.println("(" + x1 + ", " + y1  + ")" + " " + "(" + x2 + ", " + y2+ ")");

    double getSolution = Math.sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));
    System.out.println(getSolution);
    }
}'
+1  A: 

The solution to this is to use String line = input.nextLine() instead of nextDouble(). Then you can have a method like:

public static boolean timeToExit(String input) {
    return input.equalsIgnoreCase("q");
}

This method will need to be called each time the user provides input:

if (timeToExit(line)) break;

That will exit a loop.

Now, since you have a String representation of the double, you will need to use Double.parseDouble(line) to turn the String into a number.

Then all you have to do is enclose everything in an infinite loop -> while(true) { }

And, the only time it will exit the loop is if the timeToExit method returned true, and you break the loop.

This all turns into something like:

while (true) {
    ...
    System.out.print("Enter the first X coordinate: ");
    String x1 = input.nextLine();
    if (timeToExit(x1)) break;
    double x1_d = Double.parseDouble(x1);
    ...
}
jjnguy
Thanks, it works. I also had to change the getSoulition variable to the parseDouble ones...and holy-sh!t, i need more practice with this. I knew the parse concept, never realized it.
Carlos_Gomez
@carlos, glad I could help, and glad you got it working.
jjnguy
+1  A: 

just some pseudocode:

while (! input.equals("q") )
// do something

If the user inputs q, input.equals("q") returns true which is then negated and breaks the loops.

Otherwise, the user inputs another number say 44, input.equals("q") equals false, which is negated and the loop continues.

townsean
I'm not a java programmer but that should allow you to loop until the user types 'q'
townsean
Many more changes are required to make that work.
jjnguy
well I'm no good at java so I tried to keep it kinda language indpendent :( What would be changed to make it work? Just curious! Thanks! EDIT: Ah, I get it...the number-to-string conversion part?
townsean
that did not worked, it will compile fine but the loope runs once, and if i put "q", there is a runtime error...
Carlos_Gomez
A: 

I don't see a loop in your code... :s

But, why don't you try this:

while(true)
{
  string input = // I don't remember the code to create a stream for standard input
  if(input == "q"){
    break;
  }
  else{
    java.util.Scanner inputWithNumbers = new java.util.Scanner(input);
    //---! All math operations here
  }
}
chrono.ku
This solution will miss an input if a user entered a number.
jjnguy