1

Have to add extra line "String x=in.readLine();" after reading character "sec=(char)in.read();" otherwise program is not proceeding further to take more inputs, see comment below in code. Please note I don't want to use scanner class.

import java.io.*;
class marks
{
public static void main(String args[])
{
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    int rl,m1,m2,m3,tot=0;
    String nm,cl;
    nm=cl="";
    char sec='A';
    double avg=0.0d;
    try
    {
        System.out.println("Enter the information of the student");
        System.out.print("roll no:");
        rl=Integer.parseInt(in.readLine());
    System.out.print("class:");
    cl=in.readLine();
    System.out.print("section:");
    sec=(char)in.read();
    String x=in.readLine();  /* have to add this line only then marks of 3 subject can be inputed */
    System.out.println("marks of three subjects "+x);
    m1=Integer.parseInt(in.readLine());
    m2=Integer.parseInt(in.readLine());
    m3=Integer.parseInt(in.readLine());
    tot=m1+m2+m3;
    avg=tot/3.0d;
    System.out.println("total marks of the students = "+tot);
    System.out.println("avg marks of the students = "+avg);
    }
    catch (Exception e)
    {};
}
} 
  • Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Johannes Kuhn Jun 30 '18 at 19:31

3 Answers3

1

How about replacing:

sec=(char)in.read();

with:

sec = in.readLine().charAt(0);
Jared Stewart
  • 481
  • 2
  • 9
0

solved

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Marks {
    public static void main(String args[]) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
            int rl, m1, m2, m3, tot = 0;
            String nm, cl;
            nm = cl = "";
            char sec = 'A';
            double avg = 0.0d;
            System.out.println("Enter the information of the student");
            System.out.print("roll no:");
            rl = Integer.parseInt(in.readLine());
            System.out.print("class:");
            cl = in.readLine();
            System.out.print("section:");
            sec = in.readLine().charAt(0); //changes are here, instead of 
            // String x = in.readLine(); /* have to add this line only then marks of 3
            // subject can be inputed */
            System.out.println("marks of three subjects ");
            m1 = Integer.parseInt(in.readLine());
            m2 = Integer.parseInt(in.readLine());
            m3 = Integer.parseInt(in.readLine());
            tot = m1 + m2 + m3;
            avg = tot / 3.0d;
            System.out.println("total marks of the students = " + tot);
            System.out.println("avg marks of the students = " + avg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output

Enter the information of the student
roll no:21
class:10
section:c
marks of three subjects 
56
65
56
total marks of the students = 177
avg marks of the students = 59.0
Chirag
  • 503
  • 1
  • 4
  • 13
0

The problem is when you use in.read() according to documentation:"Reads a single character," but you are actually typing 'two' characters: one char and one '\n' which is stored in the InputStreamReader's buffer and will be read again when you use in.readLine();