-3

I have an array of strings - name[]. When I try to take an input from the user using the Scanner class the program seems to ignore the statement and go on to the next.

import java.util.Scanner;
class Student { //start of class
    public static void main(String[] args) { //start of method main
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of students: ");
        int n = sc.nextInt();
        String name[] = new String[n];
        int totalmarks[] = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.println("Student " + (i + 1));
            System.out.print("Enter name: ");
            name[i] = sc.nextLine();
            System.out.print("Enter marks: ");
            totalmarks[i] = sc.nextInt();
        }
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum = sum + totalmarks[i]; //calculating total marks
        }
        double average = (double) sum / n;
        System.out.println("Average is " + average);
        for (int i = 0; i < n; i++) {
            double deviation = totalmarks[i] - average;
            System.out.println("Deviation of " + name[i] + " is " + deviation);
        }
    } //end of method main
} //end of class
Bilesh Ganguly
  • 3,000
  • 2
  • 31
  • 50
  • Please indent your code properly (your IDE can do that for you). [I downvoted because if we cannot read your code, we cannot help you.](http://idownvotedbecau.se/unreadablecode/) – Ole V.V. Nov 11 '18 at 07:17

2 Answers2

0

That's because the sc.nextInt() method does not read the newline character in your input and so you need to call sc.nextLine()

From the docs

Advances this scanner past the current line and returns the input that was skipped.

This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

You code will now look like :

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of students: ");
    int n = sc.nextInt();
    sc.nextLine();                         // <----- observe this
    String name[] = new String[n];
    int totalmarks[] = new int[n];
    for (int i = 0; i < n; i++) {
        System.out.println("Student " + (i + 1));
        System.out.print("Enter name: ");
        name[i] = sc.nextLine();
        System.out.print("Enter marks: ");
        totalmarks[i] = sc.nextInt();
        sc.nextLine();                     // <----- observe this  
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + totalmarks[i];
    }
    double average = (double) sum / n;
    System.out.println("Average is " + average);
    for (int i = 0; i < n; i++) {
        double deviation = totalmarks[i] - average;
        System.out.println("Deviation of " + name[i] + " is " + deviation);
    }
}
Nicholas K
  • 14,118
  • 7
  • 25
  • 49
0

Try this.. Your sc.nextLine() is reading empty String after you input integer value

import java.util.Scanner;
class Student
    {//start of class
        public static void main(String[] args)
        {//start of method main
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of students: ");
        int n = sc.nextInt();
        String emp = sc.nextLine();
        String name[] = new String[n];
        int totalmarks[] = new int[n];
        for (int i=0;i<n;i++)
        {
        System.out.println("Student " + (i + 1));
        System.out.print("Enter name: ");
        name[i] = sc.nextLine();
        System.out.print("Enter marks: ");
        totalmarks[i] = sc.nextInt();
        emp = sc.nextLine();
        }
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
        sum = sum + totalmarks[i];//calculating total marks
        }
        double average = (double) sum / n;
        System.out.println("Average is " + average);
        for (int i = 0; i < n; i++)
        {
        double deviation = totalmarks[i] - average;
        System.out.println("Deviation of " + name[i] + " is " + deviation);
        }
        }//end of method main
        }//end of class