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.

3
  • 2
    Please go through : stackoverflow.com/questions/513832/… Commented Oct 10, 2015 at 7:51
  • pass three argument's to for( int i= 0; i < blah; i++) Commented Oct 10, 2015 at 7:52
  • @Mohit OP wants an infinite loop (as mentioned in comment to this answer), so an empty for or while(true) would work Commented Oct 10, 2015 at 8:04

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! :)
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.

Comments

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")

4 Comments

Yes, that is because i'm looking to the run the loop infinitely. The loop will be exited when the input is valid.
You mean while(!examType.equals("FA") && !examType.equals("SA")). Otherwise the loop will run while input is valid
@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.
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.
0

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

("SA".equals (examType))

2 Comments

You could expand a bit why that is done.. ie null safety
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.