1

I am totally new to Java and I've started with some simple console applications.

This is my current application's code:

Scanner sc = new Scanner(System.in);
boolean ExitLoop = false;
ArrayList<Integer> IDs = new ArrayList<Integer>();
ArrayList<Double> averages = new ArrayList<Double>();
while(!ExitLoop)
{
    System.out.println("StudentID: ");
    IDs.add(sc.nextInt());
    System.out.println("Average: ");
    averages.add(sc.nextDouble());
    System.out.println("Do you want to register another student? [y/n] ");
    ExitLoop = (sc.next() == "n");
}

Sorry to ask such a silly question but I am really stuck in this, I hit "n" but the while loop does not stop, and keeps working. Have I done anything wrong? what should I do to finish the loop when user enters "n" meaning no?

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94
  • 3
    FYI, `do { ... } while(!sc.next().equals("n"));` is a direct construct for what you're trying to do, rather than needing the `ExitLoop` flag. – Mark Peters Dec 28 '12 at 18:10
  • 2
    as convention start your variable-Names with a lower case: `exitLoop` and `ids`. Otherwise someone could think these are classes. – Simulant Dec 28 '12 at 18:12
  • +1 @MarkPeters good opinion, your solution deserves to be the answer. Add it as an answer so that I can accept it. – Mahdi Tahsildari Dec 28 '12 at 18:12
  • 1
    @mahdi: I don't think it's an answer as it doesn't actually answer the question you've asked as Nambari's answer does. The problem is with your use of `==`; what I suggested will be functionally equivalent to the code you presented but cleaner. – Mark Peters Dec 28 '12 at 18:15
  • @MarkPeters so thanks anyway for reminding me about do ... while (..==..) – Mahdi Tahsildari Dec 28 '12 at 18:28

6 Answers6

10

One issue is:

sc.next() == "n"

should be

sc.next().equals("n")

String comparison should use equals() instead of == (except String literal comparison) and it is always better to follow java code conventions.

kosa
  • 65,990
  • 13
  • 130
  • 167
3

Change it to

sc.next().equals("n")

apart from that check java coding convention, variable name follows camel case

jmj
  • 237,923
  • 42
  • 401
  • 438
2

Try this

   Scanner sc = new Scanner(System.in);
            boolean ExitLoop = false;
            ArrayList<Integer> IDs = new ArrayList<Integer>();
            ArrayList<Double> averages = new ArrayList<Double>();
            while(!ExitLoop)
            {
                System.out.println("StudentID: ");
                IDs.add(sc.nextInt());
                System.out.println("Average: ");
                averages.add(sc.nextDouble());
                System.out.println("Do you want to register another student? [y/n] ");
                 if(sc.next().equals("n")){
                         ExitLoop = true;
                 }
            }

Also note that in java, if you want to compare Strings by their values use the .equals("somevaluhere") if you want to compare their reference use ==

KyelJmD
  • 4,682
  • 9
  • 54
  • 77
  • 2
    Why would you need a ternary operator on a boolean expression whose result is assigned to a boolean variable? This is beyond redundant. – A--C Dec 28 '12 at 18:10
1

I would be cautious in using the .equals("n") mainly because it compares the entire string. What if the user typed the entire word "no"? This would result in a continuation of the loop as well. There is nothing wrong with using the == operator you were using before, you just need to make sure as to what it is comparing. It works great for char, not so much for char vs String or String vs String. To me, a better implementation would be:

exitLoop = sc.next().charAt(0) == 'n';

or even better:

exitLoop = (sc.next().charAt(0) == 'n' || sc.next().charAt(0) == 'N');

Also, now would be a good time to start figuring out input validation.

And don't forget to close the scanner.

0

You want to get out of the loop if the user answers "n" so you should just write

 ExitLoop=sc.next().equals("n");

and keep in mind that the user may answer "N". the above method returns a boolean value for a boolean variable so you are ok and your code is as simple as possible.

0

This would check if the input string starts with n ExitLoop = sc.next().toLowerCase().startsWith("n"); // or you might want to try ignoring case by doing ExitLoop = sc.next().toLowerCase().equals("n");

Ayodeji
  • 579
  • 2
  • 8
  • 25