-2

I am making a java calculator to find out the density and viscosity of aqueous sucrose solutions (chemistry stuff) and decided to go farther with it. I used Micheal Flanagan's Java library for Sucrose which is the:

import flanagan.physprop.Sucrose;

in the code. I wanted a way to continue entering equations without having to continue pressing run over and over so I created

public static void restart() {
    System.out.println("would you like to calculate another solution?(y/n)");
    String yn = new String(input.nextLine());
    String y = "y";
    String n = "n";
    System.out.println(yn);
    if (yn == y){
        tf = true;
    }
    else {
        if (yn == n) {
            tf = false;
        }
        else {
        System.out.println("ERROR: Please print 'y' or 'n'");
        restart();
        }
    }
    if (tf == true) {
        calc();
    }
    if (tf == false) {
        fin();
    }
}

I am using a scanner called 'import' which is defined in an earlier line.

static Scanner input = new Scanner(System.in);

So, anyway, what is happening is the code is not restarting, but instead triggering the error. I have tried fixing it by defining the readout as a string and making variables for "y" and "n". I am thinking that "yn" is not registering properly as a string, and cannot think of anything else.

flanagan.jar can be found here.

  • Does Scanner even have a `nextSymbol()` method? I looked at the [Scanner API](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html) and couldn't find it. Does this code even compile with this method? – Hovercraft Full Of Eels Apr 16 '17 at 01:21
  • In fact this whole statement is strange: `new String(input.nextSymbol())`. Why would you ever want to call `new String(...)` on anything? – Hovercraft Full Of Eels Apr 16 '17 at 01:22
  • Try posting a [MCVE](http://stackoverflow.com/help/mcve). What is calling the **restart()** methood? What exactly is the error (exception) you're getting? – DevilsHnd - 退職した Apr 16 '17 at 01:30
  • Hovercraft, I know it looks really weird, because it is all my last ditch attempt. Right now I'm using eclipse, which can't even compile `!yn == y && !yn == n` which should replace the double else. – Another Shrubbery Apr 16 '17 at 01:33

2 Answers2

0

Well, I got the answer by switching to numbers.

public static void restart() {
    System.out.println("would you like to calculate another solution?(1 for yes/0 for no)");
    int yn = input.nextInt();
    int y = 1;
    int n = 0;
    if (yn == y){
        calculator();
    }
    if (yn == n) {
        fin();
    }
}
0

Well all you need to do is to use a infinite cycle:

while(true) {

//code you want to repeat...

}