0

I've been trying to code the following program and make it so that when the user inputs "stop" or "s", the program would terminate but until he inputs that word, it would keep on asking the user to input numbers to square root them. This is my code so far, in terms of functionality it is good but I just cannot seem to get this right. Thank you all.

import java.util.Scanner;

public class Question5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double input, var = 0.0, var2 = 0.0; //initialising variables
        String x;

        System.out.println("Input the number to calculate square root on");
        input = sc.nextDouble(); //inputting by the user


        while(!x.equals("stop")) {

            x = sc.next();
            if (input < 0) { //negative numbers cannot be entered
                System.out.println("Negative numbers are not computable in this case");
                System.exit(1);
            }
            double RepeatedInp = input;

            while (true) {
                //calculation of square root of number inputted
                var = ((RepeatedInp + input / RepeatedInp) / 2.0);
                RepeatedInp = var;

                if (var2 == var) {
                    break;
                }


                var2 = var;
            }
            System.out.println(var); //printing


        }


    }
}
Redent
  • 33
  • 7

1 Answers1

0

You can use Scanner or BufferReader. many details here: Scanner vs. BufferedReader

with Scanner: it waits end of line (you have to press return).

// INPUT LOOP

System.out.println("TYPE YOUR FORMULA (s ou stop to STOP):");

Scanner s = new Scanner(System.in);
String line;

while ((line=s.nextLine())!=null)
     {
    System.out.println("LINE:'"+line+"'"); 


        // IF STOP: 
        if (line.contentEquals("stop"))
            break;

        if (line.contentEquals("s"))
            break;

        // do what you want

         System.out.println("TYPE YOUR FORMULA (s ou stop to STOP):"); 

         } 
        // while ((line=s.nextLine())!=null)

 // safe
 s.close();

To get characters directly, see this: How to read a single char from the console in Java (as the user types it)?

  • What I do not like is the fact that I have to press enter to display the first syst.out statement and I did the "IF STOP" bit at the end but it still won't function well – Redent Apr 01 '18 at 14:49
  • @Redent. I extended my answer. – guillaume girod-vitouchkina Apr 01 '18 at 15:01
  • I somehow managed to get it to work but not as smooth as it could be. It asks the user whether he wishes to exit the program or not before he could input a number – Redent Apr 01 '18 at 15:07
  • @Redent you dont have to. The user just types numbers or s/stop . I improved the code. – guillaume girod-vitouchkina Apr 01 '18 at 15:10
  • Ah yes I noticed, thank you very much. However, I may be wrong but you forgot to add the line = sc.nextLine(); just before the if statements – Redent Apr 01 '18 at 15:19
  • I used Scanner and I had to add the line previously mentioned as it did not request for any input without it. Nonetheless, thank you loads as my program functions as it was intended. – Redent Apr 01 '18 at 16:01