1

I have developed a code to get the input details of the students marks and print the average as the output. This operation will be performed until the user gives an "n" as an input for the looping condition.But in my code, the user is unable to give the conditional input and the automatically performs the while loop

import java.util.Scanner;


class Student

 {

   String reg;

  int maths,phy,che,cse,avg;

void cal()

{

if(maths<50 | phy<50 | che<50 | cse<50)

{

System.out.println("The grade of the student will not be computed");

}

else

{

avg=((maths+phy+che+cse)/4);

if(avg>=91)

{

System.out.println("S");

}

if(avg>=81 && avg<=90)

{

System.out.println("A");

}



if(avg>=71  && avg<=80)

{

System.out.println("B");

}



if(avg>=61  && avg<=70)

{

System.out.println("C");

}


if(avg>=50 && avg<=60)

{

System.out.println("D");

}



}


}

}







 class Main1    //3rd

{

        public static void main(String args[]) throws Exception {

            Scanner w = new Scanner(System.in);
            String r = "Y", a = "Y";

            while (r.contains(a)) {

                Student s = new Student();
                System.out.println("Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first");
                s.reg = w.nextLine();
                s.maths = w.nextInt();
                s.phy = w.nextInt();
                s.che = w.nextInt();
                s.cse = w.nextInt();
                System.out.println("press 'Y' to continue and 'N' to exit");
                a = w.nextLine();
                s.cal();

            }

        }

    }

ACTUAL OUTPUT :

C:\Program Files\Java\jdk-12.0.2\bin>java Main1

Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first

100

100

100

100

100

press 'Y' to continue and 'N' to exit

S

Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first

Community
  • 1
  • 1

1 Answers1

1

Please add w.nextLine(); before System.out.println("press 'Y' to continue and 'N' to exit"); so the main method should looks like that:

public static void main(String args[]) throws Exception {
    Scanner w = new Scanner(System.in);
    String r = "Y", a = "Y";

    while (r.contains(a)) {

        Student s = new Student();
        System.out.println("Enter the marks for maths,physics,chemistry,CSE along with your Reg.no first");
        s.reg = w.nextLine();
        s.maths = w.nextInt();
        s.phy = w.nextInt();
        s.che = w.nextInt();
        s.cse = w.nextInt();
        w.nextLine(); //------> Added
        System.out.println("press 'Y' to continue and 'N' to exit");
        a = w.nextLine();
        s.cal();
Rafał Sokalski
  • 1,570
  • 1
  • 12
  • 25
  • what was the problem in the code ? – VishnuBalaji VB Sep 02 '19 at 11:05
  • nextInt does not read next line character only the number you enter that's why w.nextLine ( in line a = w.nextLine();) did that. More information here https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Rafał Sokalski Sep 02 '19 at 11:08