Jump to content

How do I prompt the user to stop in this cartesian coordinates Java Program1

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
Here is my program. Everything worked until I used something as an invalid input. Question is (tell the user to enter"DONE" or any other invalid number) when they are ready to stop. What invalid number can I use here ?

import java.util.*;


public class CartesianCoordinates
{
    static Scanner console = new Scanner(System.in);    
    
          public static void main (String[] args)

        {  

             int x;
        int y;
                
                                                     
                System.out.println();
                while (true) {

            System.out.printf("\nEnter x and y coordinates of a" + "\npoint in a Cartesian Plane: ");
            System.out.printf("\nTo stop the program, Enter #");
                    x = console.nextInt();
            y = console.nextInt();
                    
            System.out.printf("The Coordinates you entered are "  + x  +  " and "  + y);

                    if (x == 0 && y == 0)
                       System.out.printf("\nThe Point is the Origin.");    
            
            if (x != 0 && y == 0)
               System.out.printf("\nThe point is located" + "\n on the x-axis.");
            
            if (x == 0 && y != 0)
               System.out.printf("\nThe point is located" + "\n on the y-axis.");

            if (x > 0 && y > 0)
                System.out.printf("\nThe point is located" + "\n on the First Quadrant.");

            if (x < 0 && y > 0)
            System.out.printf("\nThe point is located" + "\n on the Second Quadrant.");

            if (x < 0 && y < 0)
                      System.out.printf("\nThe point is located" + "\n on the Third Quadrant.");

            if (x > 0 && y < 0) 
                System.out.printf("\nThe point is located" + "\n on the Fourth Quadrant.");
    
                    
             
                    if(x == #)  {
              System.out.printf("\nInvalid Input.\n"
                        + "Terminate Coordinates program.");  

                         break; 
                      
                                                                  
                                     }
                            }    
         }
}

Edited by ZekeDragon, 17 May 2010 - 02:35 AM.
Use [code] tags (the # button) when posting code.


#2
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
You might wanna reconsider using the 'while(true)' loop.
// d-_-b+

#3
bleedy3

bleedy3

    Newbie

  • Members
  • PipPip
  • 26 posts
You could prompt the user to quit at the end of the program by having them type yes or no.
Here is the code:

import java.util.*;


public class CartesianCoordinates
{
static Scanner console = new Scanner(System.in); 

public static void main (String[] args)

{ 

int x = 0;
int y;


System.out.println();
String decision = "no";
while (decision.equals("no")) {

System.out.println("Points in a Cartesian Plane.");
System.out.printf("Enter the x coordinate: ");
x = console.nextInt();
System.out.printf("Enter the y coordinate: ");
y = console.nextInt();




System.out.printf("The Coordinates you entered are " + x + " and " + y);

if (x == 0 && y == 0)
System.out.printf("\nThe Point is the Origin.\n\n");    


if (x != 0 && y == 0)
System.out.printf("\nThe point is located" + "\n on the x-axis.\n\n");


if (x == 0 && y != 0)
System.out.printf("\nThe point is located" + "\n on the y-axis.\n\n");


if (x > 0 && y > 0)
System.out.printf("\nThe point is located" + "\n on the First Quadrant.\n\n");


if (x < 0 && y > 0)
System.out.printf("\nThe point is located" + "\n on the Second Quadrant.\n\n");


if (x < 0 && y < 0)
System.out.printf("\nThe point is located" + "\n on the Third Quadrant.\n\n");


if (x > 0 && y < 0) 
System.out.printf("\nThe point is located" + "\n on the Fourth Quadrant.\n\n");

System.out.printf("Do you want to quit?\nEnter \"yes\" or \"no\": ");
decision = console.next();
console.nextLine();
System.out.println();


}
} 
}

Edited by ZekeDragon, 17 May 2010 - 02:35 AM.
Use [code] tags (the # button) when posting code.


#4
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks for the code. It worked. it is excellent.

But I don't want to ask the user whether he wants to coninue or not.. What I want is that the user continue reading and printing.. Tell them to enter "Done" (or any invalid number) when they are ready to stop. (I think that will cause the program to terminate with an error message). Can you help me with this please?

Charlote

#5
bleedy3

bleedy3

    Newbie

  • Members
  • PipPip
  • 26 posts
Ok, I think I know what you are asking. The thing is the number character "#" is not an int, and the word "done" is also not an int.
So first you must read the x value as a String and then convert this value to an int using the Integer.parseInt() method.
Here is the code:

import java.util.*;


public class CartesianCoordinates
{
static Scanner console = new Scanner(System.in); 

public static void main (String[] args)

{ 

String ans = "";
int y;
int x;

System.out.println();
while (true) {

System.out.println("Points in a Cartesian Plane.");
System.out.println("To stop the program, Enter\n" +
                    "\"done\" or any Invalid #.");

System.out.printf("Enter the x coordinate: ");

console = new Scanner(System.in);
ans = console.nextLine();

if(ans.equals("#") || ans.equalsIgnoreCase("done")) {
    System.out.printf("\nInvalid Input.\n"
    + "Terminate Coordinates program."); 
    break; 
}

System.out.printf("Enter the y coordinate: ");
y = console.nextInt();
System.out.println();



x = Integer.parseInt(ans);
System.out.printf("The Coordinates you entered are " + x + " and " + y);

if (x == 0 && y == 0)
System.out.printf("\nThe Point is the Origin.\n\n");    


if (x != 0 && y == 0)
System.out.printf("\nThe point is located" + "\n on the x-axis.\n\n");


if (x == 0 && y != 0)
System.out.printf("\nThe point is located" + "\n on the y-axis.\n\n");


if (x > 0 && y > 0)
System.out.printf("\nThe point is located" + "\n on the First Quadrant.\n\n");


if (x < 0 && y > 0)
System.out.printf("\nThe point is located" + "\n on the Second Quadrant.\n\n");


if (x < 0 && y < 0)
System.out.printf("\nThe point is located" + "\n on the Third Quadrant.\n\n");


if (x > 0 && y < 0) 
System.out.printf("\nThe point is located" + "\n on the Fourth Quadrant.\n\n");



}
} 
}

Edited by ZekeDragon, 17 May 2010 - 02:36 AM.
Use [code] tags (the # button) when posting code.


#6
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks a lot.

Charlote