0

This is what I have so far and I tried to use the while (yorn=="yes") to cancel but it keeps going regardless and I am also trying to find a way to make it so the outputs go in one group together rather than being seperated.

import java.util.Scanner;
class Main {
  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    String no = "no";
    String yes = "yes";
    String yorn="yes";

    CollegeStudent Student1 = new CollegeStudent();

    while (yorn=="yes") {
      System.out.println("continue? yes/no:");
      yorn=scan.next();
      Student1.setname();
      Student1.setcourseTitle();
      Student1.setcredits();
      Student1.setcourseCode();
      System.out.println(Student1.toString());
    }
  }
}
FilipRistic
  • 2,661
  • 4
  • 22
  • 31
Esha Rice
  • 59
  • 4

1 Answers1

4

You need to use the equals method for string comparing in the java:

while (yorn.equals("yes"))
{
    System.out.println("continue? yes/no:");
    yorn=scan.next();
    Student1.setname();
    Student1.setcourseTitle();
    Student1.setcredits();
    Student1.setcourseCode();
    System.out.println(Student1.toString());
}

Since after you answer exists the logic in the cicle, after yes typing code has been executing anyway (one time). Simply solution for you will be:

System.out.println("Do you need to add a student? yes/no:");
yorn=scan.next();
while (yorn.equals("yes"))
{
    Student1.setname();
    Student1.setcourseTitle();
    Student1.setcredits();
    Student1.setcourseCode();
    System.out.println(Student1.toString());
    System.out.println("continue? yes/no:");
    yorn=scan.next();
}
Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59