0

I am a beginner and was making a small program to practice what i have learnt. I was writing code to check the grade of a student.

This is the code :

import java.util.*;

public class Grader {

 public static void main(String[] args) {

    String studentName;
    int rollNo = 0;
    Scanner inputter = new Scanner(System.in);

    System.out.println("Please enter the roll number of the student: ");
    rollNo = inputter.nextInt();

    System.out.println("Thank you. Now, please enter the student's name: " );
    studentName = inputter.next();

    for(int i=0; ; i++){
        System.out.println("Please enter a valid examination type, i.e FA or SA: ");
        String examType = inputter.next();
        examType = examType.toUpperCase();
        if(examType == "FA" || examType == "SA"){
            break;
        }
    }

}

}

The problem I am facing is that even though I enter a valid examType, the For loop doesn't break.

Aryan Srivastava
  • 23
  • 1
  • 1
  • 5
  • 2
    Please go through : http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Rahman Oct 10 '15 at 07:51
  • pass three argument's to for( int i= 0; i < blah; i++) – Abdul Muheet Oct 10 '15 at 07:52
  • @Mohit OP wants an infinite loop (as mentioned in comment to [this](http://stackoverflow.com/a/33051662/2773311) answer), so an empty `for` or `while(true)` would work – Arc676 Oct 10 '15 at 08:04

4 Answers4

3

You need to use String.equals().

Scanner.next() returns a String. Using == on a string doesn't give any errors but will test reference equality instead of value equality. It won't return true even if the strings are equal in value.

Correct code:

if(examType.equals("FA") || examType.equals("SA")){
    break;
}

EDIT
OP mentioned in a comment that the loop is to run without ending until hitting break. You can create an infinite loop in either of these two ways:

for(;;){
    //runs infinitely
}

OR

while(true){
    //runs infinitely
}

Both of these infinite loops are broken with break. Also, you use less memory (albeit a small and almost insignificant difference) because you don't have a counter variable. In the next-to-impossible case that the user enters invalid input so many times that the integer overflows, not having a variable eliminates this risk. You also save processor time because there isn't an instruction to allocate memory or add one to the number.

Arc676
  • 4,445
  • 3
  • 28
  • 44
1
public static void main(String args[]) {
  String studentName;
  int rollNo = 0;
  Scanner inputter = new Scanner(System.in);

  System.out.println("Please enter the roll number of the student: ");
  rollNo = inputter.nextInt();

  System.out.println("Thank you. Now, please enter the student's name: " );
  studentName = inputter.next();

  for(int i=0; ; i++){
    System.out.println("Please enter a valid examination type, i.e FA or SA: ");
    String examType = inputter.next();
    examType = examType.toUpperCase();
    if(examType.equals("FA") || examType.equals("SA")){
        break;
    }
  }
}

This is working fine.

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
0

You haven't set a limit, for example

for(int i = 0; i < somevalue; i++)

otherwise a while loop might be a better choice like:

while(examType.equals("FA") || examType.equals("SA")
Aven
  • 72
  • 10
  • Yes, that is because i'm looking to the run the loop infinitely. The loop will be exited when the input is valid. – Aryan Srivastava Oct 10 '15 at 07:53
  • You mean `while(!examType.equals("FA") && !examType.equals("SA"))`. Otherwise the loop will run while input is _valid_ – Arc676 Oct 10 '15 at 07:57
  • @Arc676 && is requiring both and I think he needs to check for a two letter capital string... I was just giving an easy example not the exact answer. – Aven Oct 10 '15 at 08:01
  • Alright, but I meant that the loop ends when the input is _either_ of the valid inputs. The while loop I gave in the comment will stop as soon as the input _is_ one of the valid inputs. – Arc676 Oct 10 '15 at 08:03
0

For best coding practice the usage of equals operation for Strings like below code snippet as per Sonar compliance standard.

("SA".equals (examType))

  • You could expand a bit why that is done.. ie null safety – Zavior Oct 10 '15 at 08:04
  • It is not about best practices, if you want to compare two strings, then you will always use equals() method. The `==` is reference comparison, which is completely different. – YoungHobbit Oct 10 '15 at 08:04