Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: name=scan.nextLine();

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default name=scan.nextLine();

    This is a simple students database that counts the number of students print their names,marks ....etc
    it's not finished yet but when i run the program (i use BlueJ) and when it comes to inputting the Student name
    it ignores the line and go to the next input sentence (aka in this program student number

    Output:
    Enter No.ofStudents:
    1
    Enter Student Name:
    Enter Student Number:
    f

    entered f to stop the loop, here's the code

    import java.util.*;
     
    public class StudentsDatabase
    {
        public static void main(String[]args)
        {
            String name; 
            int nos,sn,subnum,marks,sum,avg,i,j;
            Scanner scan=new Scanner(System.in);
            sum=0; 
            i=0; //Student number Counter
            j=0;//Subject numbers
            nos=0;//number of students
     
     
            System.out.println("Enter No.ofStudents: "); nos=scan.nextInt();
            while(j<=nos)
            {
                j=j+1;
                System.out.println("Enter Student Name: "); name=scan.nextLine();
                System.out.println("Enter Student Number: "); sn=scan.nextInt();
                System.out.println("Enter Subject Number: ");subnum=scan.nextInt();
                System.out.println("S.no: "+sn+" S.name: "+name+" Sub.No: "+subnum);
                  while (i<=subnum)
                  {
                    i=i+1;
                    System.out.println("Enter Subject Mark: "); marks=scan.nextInt();
                    System.out.println("M = "+marks);
                  }
     
            }
     
        }
    }



    although i solved it in another way:
     
    /**
     * Write a description of class sum here.
     * 
     * @author Amro Subihie 
     * @version 09.11.2010
     */
    import javax.swing.*;
    import java.util.*;
     
    public class StudentsJdatabase
    {
        public static void main(String[]args)
        {
            String name; 
            int n1,n2,n3,n4,sum,avg,i,j;
            Scanner scan=new Scanner(System.in);
            sum=0; 
            i=0; //Student number Counter
            j=0;//Subject numbers
            String nos,sn,subnum,marks;//number of students
     
            nos=JOptionPane.showInputDialog("Enter number of students"); n1=Integer.parseInt(nos);
            while(j<=n1)
            {
                j=j+1;
                name=JOptionPane.showInputDialog("Enter Student Name");
                sn=JOptionPane.showInputDialog("Enter Student Number");n2=Integer.parseInt(sn);
                subnum=JOptionPane.showInputDialog("Enter number of Subjects");n3=Integer.parseInt(subnum);
                  while (i<=n3)
                  {
                    i=i+1;
                    marks=JOptionPane.showInputDialog("Enter Mark");n4=Integer.parseInt(marks);
     
                    sum=sum+n4;
                  }
               avg=sum/n3;
     
               JOptionPane.showMessageDialog(null,"S.name: "+name+"  S.no: "+n2+"  Sub.No: "+n3+"   Average = "+avg, "Student Data",JOptionPane.PLAIN_MESSAGE);
     
            }
            System.exit(0);
        }
    }


    any idea on how to record the output in a table or something? (yeah i'm a noob)
    Last edited by Amro; November 10th, 2010 at 06:29 AM.

  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: name=scan.nextLine();

    what table do you want to record the output? u mean like you want to save it to a datasource so that the ur data will still be available after you have terminated your program? in that case u need a database like mysql or ms access and persist ur outputs on a table on that database.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Location
    Hyderabad
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Thumbs up Re: name=scan.nextLine();

    just change the below line as shown below

    System.out.println("Enter Student Name: "); name=scan.next();

  4. The Following User Says Thank You to sandeepatreyak For This Useful Post:

    Amro (November 13th, 2010)

  5. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: name=scan.nextLine();

    that actually worked heh dunno why it didn't with the "nextLine" so if you don't type the Type of the variable it's left free or is it recording it as a String? anyways that isn't my concern now

    as for recording it i meant if it could be shown in a table in the output but i guess that is still advanced to what i'm studying.

    thanks alot thou, both of ya

Similar Threads

  1. Question concerning nextLine() method in Scanner Class
    By r_sardinas in forum Java SE APIs
    Replies: 2
    Last Post: October 20th, 2010, 08:35 PM
  2. I need help! Re: strings, nextLine, while and more..
    By rockerade in forum What's Wrong With My Code?
    Replies: 19
    Last Post: October 5th, 2010, 03:51 PM
  3. Replies: 1
    Last Post: August 13th, 2010, 06:58 AM
  4. nextLine problems, any help appreciated
    By Schmitz14 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 5th, 2009, 01:23 AM
  5. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM