0

I am beginner in java. While doing this simple program(The program is pretty long, please dont mind) I found out :

This doesn't work.

import java.util.*;
class store
{
    store()
    {
        System.out.println("RESLUTS OF STUDENTS");
        System.out.println();
    }
    void stu1 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu2 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu3 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu4 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu5 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
}
class stu
{
    public static void main(String[] args) {
        Scanner v = new Scanner (System.in);
        System.out.println("Enter the name of first student");
        String p = v.nextLine();
        System.out.println("Enter the marks for first student");
        int q = v.nextInt();
        System.out.println("Enter the name of second student");
        String r = v.nextLine();
        System.out.println("Enter the marks for second student");
        int s = v.nextInt();
        System.out.println("Enter the name of third student");
        String t = v.nextLine();
        System.out.println("Enter the marks for third student");
        int u = v.nextInt();
        System.out.println("Enter the name of fourth student");
        String w = v.nextLine();
        System.out.println("Enter the marks for fourth student");
        int x = v.nextInt();
        System.out.println("Enter the name of fifth student");
        String y = v.nextLine();
        System.out.println("Enter the marks for fifth student");
        int z = v.nextInt();
        store st = new store ();
        st.stu1(p,q);
        st.stu2(r,s);
        st.stu3(t,u);
        st.stu4(w,x);
        st.stu5(y,z);   
    }
}

But this does.The nextLine() and nextInt() are separated along with print statements.

import java.util.*;
class store
{
    store()
    {
        System.out.println("RESLUTS OF STUDENTS");
        System.out.println();
    }
    void stu1 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu2 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu3 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu4 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
    void stu5 (String name,int marks)
    {
        System.out.println("The name of student is : "+ name+"  and he got :"+marks+"marks");
    }
}
class stu
{
    public static void main(String[] args) {
        Scanner v = new Scanner (System.in);
        System.out.println("Enter the name of first student");
        String p = v.nextLine();
        System.out.println("Enter the name of second student");
        String r = v.nextLine();
        System.out.println("Enter the name of third student");
        String t = v.nextLine();
        System.out.println("Enter the name of fourth student");
        String w = v.nextLine();
        System.out.println("Enter the name of fifth student");
        String y = v.nextLine();
        System.out.println("Enter the marks for first student");
        int q = v.nextInt();
        System.out.println("Enter the marks for second student");
        int s = v.nextInt();
        System.out.println("Enter the marks for third student");
        int u = v.nextInt();
        System.out.println("Enter the marks for fourth student");
        int x = v.nextInt();
        System.out.println("Enter the marks for fifth student");
        int z = v.nextInt();
        store st = new store ();
        st.stu1(p,q);
        st.stu2(r,s);
        st.stu3(t,u);
        st.stu4(w,x);
        st.stu5(y,z);   
    }
}

Why is this? Isn't it basically the same?Or is there any other way to do this?

Pramesh Bajracharya
  • 1,642
  • 2
  • 25
  • 39

1 Answers1

0

Thats because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine

Your question has already been answered here: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
Overholt
  • 757
  • 1
  • 6
  • 22