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()?

    15 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 '18 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 '18 at 7:17















-3
















This question already has an answer here:



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

    15 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 '18 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 '18 at 7:17













-3












-3








-3









This question already has an answer here:



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

    15 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()?

    15 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()?

    15 answers







java string input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 16:18









Bilesh Ganguly

1,74611533




1,74611533










asked Nov 11 '18 at 6:11









Rohan KhatuaRohan 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 6:18









        Nicholas KNicholas K

        6,62761132




        6,62761132























            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 '18 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 '18 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 '18 at 6:20









            Akshit GuptaAkshit Gupta

            136




            136












            • There is no need to store the reference of sc.nextLine() at all.

              – Nicholas K
              Nov 11 '18 at 6:28

















            • There is no need to store the reference of sc.nextLine() at all.

              – Nicholas K
              Nov 11 '18 at 6:28
















            There is no need to store the reference of sc.nextLine() at all.

            – Nicholas K
            Nov 11 '18 at 6:28





            There is no need to store the reference of sc.nextLine() at all.

            – Nicholas K
            Nov 11 '18 at 6:28



            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ