-1

i'm developing a java program that shows area and perimeter of two rectangle with fixed width&length, current date and reads input from user to solve a linear equation. I was able to ask a user if they want to re-run the program or not. the problem is that if they input y or Y, the program runs, but if the user enters anything els the program quits. I'd like to check this input and see if:

1- it's a y or Y, re-run

2- it's n or N, quit

3- neither 1 nor 2, ask the user again if he/she wants to re-run the program of not.

Here's my code:

    public static void main(String[] args) {

    char ch = 'y'; 

    GregorianCalendar calendar = new GregorianCalendar();

    LinearEquation le = new LinearEquation(1.0,1.0,1.0,1.0,1.0,1.0);

    Rectangle rec1 = new Rectangle(4.0,40.0);
    Rectangle rec2 = new Rectangle(3.5,35.9);        

    Scanner input = new Scanner(System.in);

    Double a, b, c, d, e,f;

do{

    System.out.println("First rectangle info is: ");
    System.out.print(" width is: "+ rec1.getWidth() + 
                   "\n height is: " + rec1.getHeight()+ 
                   "\n Area is: "+ rec1.getArea() +
                   "\n Perimeter is: " + rec1.getPerimeter());

    System.out.println();
    System.out.println();

    System.out.println("Second rectangle info is: ");
    System.out.print(" width is: "+ rec2.getWidth() + 
                   "\n height is: " + rec2.getHeight()+ 
                   "\n Area is: "+ rec2.getArea() +
                   "\n Perimeter is: " + rec2.getPerimeter());

    System.out.println();

    System.out.println("Current date is: " + calendar.get(GregorianCalendar.DAY_OF_MONTH) +   
    "-" + (calendar.get(GregorianCalendar.MONTH) + 1)+ 
    "-" + calendar.get(GregorianCalendar.YEAR));

    System.out.println("Date after applying 1234567898765L to setTimeInMillis(long) is: ");

    calendar.setTimeInMillis(1234567898765L);
    System.out.println(calendar.get(GregorianCalendar.DAY_OF_MONTH) +   
    "-" + (calendar.get(GregorianCalendar.MONTH) + 1)+ 
    "-" + calendar.get(GregorianCalendar.YEAR));

    System.out.println();
    System.out.println("Please, enter a, b, c, d, e and f to solve the equation:");

    try{

        System.out.println("a: ");        
        a = input.nextDouble();

        System.out.println("b: ");
        b = input.nextDouble();

        System.out.println("c: ");
        c = input.nextDouble(); 

        System.out.println("d: ");
        d = input.nextDouble();

        System.out.println("e: ");
        e = input.nextDouble();

        System.out.println("f: ");
        f = input.nextDouble();

        le.setA(a);
        le.setB(b);
        le.setC(c);
        le.setD(d);
        le.setE(e);
        le.setF(f);

        if(le.isSolvable()){

            System.out.println("x is: "+ le.getX() + "\ny is: "+ le.getY());

        }else { 

            System.out.println("The equation has no solution.");
        }   

        //System.out.println("Would you like to re-run the program( y or n)");
        //ch = input.next().charAt(0);
}
catch (Exception ee) {

 System.out.println("Invalid input");
 //System.out.println("Would you like to re-run the program( y or n)");
 ch = input.next().charAt(0);     
  }

     System.out.println("Would you like to re-run the program( y or any other       input to quit)");
 ch = input.next().charAt(0);

  }while(ch == 'y' || ch == 'Y');

  }
1
  • Note that the do-while loop is considered as bad practice because to see the continuation condition, you have to scroll down to the very end of it. Rather use while. Commented Feb 25, 2017 at 21:24

2 Answers 2

1

You can add a do while when you asks the user whether if he wants to repeat the program.

Besides, instead of specifying both uppercase and lowercase in statement : while(ch == 'y' || ch == 'Y'); you can use Character.toLowerCase() method to reduce the number of tests to perform.

    do {
        System.out.println("Would you like to re-run the program( y or any other input to quit)");
        ch = input.next().charAt(0);
        ch = Character.toLowerCase(ch);
    } while (ch != 'y' && ch != 'n');

Now your code could look like that :

do {
      ....
        do {
            System.out.println("Would you like to re-run the program( y or any other input to quit)");
            ch = input.next().charAt(0);
            ch = Character.toLowerCase(ch);

           } while (ch != 'y' && ch != 'n');

   } while (ch == 'y');
Sign up to request clarification or add additional context in comments.

2 Comments

that worked!! Thanks
You are kind and are welcome :)
0

At the end of your loop (but still IN your loop), you could do something like

ch = 'x'; // Just a dummy value

while (ch != 'y' && ch != 'n') {
    System.out.prinln("Enter y or n:");
    ch = input.next().charAt(0);
}

if (ch == 'n') {
    break;
}

1 Comment

I didn't try your suggestion but REALLY THANKS FOR YOUR COMMENT!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.