My Java program refuses to take a string input [duplicate]










-3















This question already has an answer here:



  • Scanner is skipping nextLine() after using next() or nextFoo()?

    14 answers



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









share|improve this question















marked as duplicate by Ole V.V. java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 6:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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.
    – Ole V.V.
    Nov 11 at 7:17















-3















This question already has an answer here:



  • Scanner is skipping nextLine() after using next() or nextFoo()?

    14 answers



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









share|improve this question















marked as duplicate by Ole V.V. java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 6:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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.
    – Ole V.V.
    Nov 11 at 7:17













-3












-3








-3








This question already has an answer here:



  • Scanner is skipping nextLine() after using next() or nextFoo()?

    14 answers



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









share|improve this question
















This question already has an answer here:



  • Scanner is skipping nextLine() after using next() or nextFoo()?

    14 answers



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




This question already has an answer here:



  • Scanner is skipping nextLine() after using next() or nextFoo()?

    14 answers







java string input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 16:18









Bilesh Ganguly

1,73811533




1,73811533










asked Nov 11 at 6:11









Rohan Khatua

35




35




marked as duplicate by Ole V.V. java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 6:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Ole V.V. java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 6:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 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.
    – Ole V.V.
    Nov 11 at 7:17
















  • 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.
    – Ole V.V.
    Nov 11 at 7:17















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.
– Ole V.V.
Nov 11 at 7:17




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.
– Ole V.V.
Nov 11 at 7:17












2 Answers
2






active

oldest

votes


















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







share|improve this answer




























    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





    share|improve this answer




















    • There is no need to store the reference of sc.nextLine() at all.
      – Nicholas K
      Nov 11 at 6:28

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









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







    share|improve this answer

























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







      share|improve this answer























        0












        0








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







        share|improve this answer












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








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 6:18









        Nicholas K

        5,17251031




        5,17251031























            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





            share|improve this answer




















            • There is no need to store the reference of sc.nextLine() at all.
              – Nicholas K
              Nov 11 at 6:28















            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





            share|improve this answer




















            • There is no need to store the reference of sc.nextLine() at all.
              – Nicholas K
              Nov 11 at 6:28













            0












            0








            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





            share|improve this answer












            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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 6:20









            Akshit Gupta

            136




            136











            • There is no need to store the reference of sc.nextLine() at all.
              – Nicholas K
              Nov 11 at 6:28
















            • There is no need to store the reference of sc.nextLine() at all.
              – Nicholas K
              Nov 11 at 6:28















            There is no need to store the reference of sc.nextLine() at all.
            – Nicholas K
            Nov 11 at 6:28




            There is no need to store the reference of sc.nextLine() at all.
            – Nicholas K
            Nov 11 at 6:28



            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo