Jump to content

Help with Classes and String not print out --{ WHY }--

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
HI all moved on to classes and doing ok so far, just one problem and need to know why? i have constructed the class and a main, i can compile and run but when it get to the StudentName String on both students it skips it why? i will show my code and output;

Student Class:

// Practical 7A

// Student class

// gman


public class Student

{

	// instance variables

	private int studentID;

	private String studentName;

	private String course;

	private double printQuota;

		

	// constructor

	public Student()

	{

		studentID = 0;

		studentName = new String();

		course = new String();

		printQuota = 7.00;

	}

	

	// set student ID method

	public void setStudentID(int ID)

	{

		studentID = ID;

	}

		// get student ID method

		public int getStudentID()

		{

			return studentID;

		}

	

	// set student name method

	public void setStudentName(String sName)

	{

		studentName = sName;

	}

		// get student name method

		public String getStudentName()

		{

			return studentName;

		}

		

	// set student course method

	public void setCourse(String sCourse)

	{

		course = sCourse;

	}

		// get student course method

		public String getCourse()

		{

			return course;

		}

		 

	// set student ( TOP UP ) printQuota method

	public void setPrintQuota(double topUP)

	{

		printQuota = printQuota + topUP;

	}	

		// get student printQuota method

		public double getPrintQuota()

		{

			return printQuota;

		}

		


}


Main Method:

// gman

// 16/2/2012


import java.util.Scanner;

public class StudentTestQ2

{


	public static void main(String[] args)

	{

	

		Scanner kb = new Scanner(System.in);

			Student student1 = new Student();

				Student student2 = new Student();

		// student 1

		// ID		

		System.out.println("Enter Student ID: ");

			int sID1 = kb.nextInt();

				student1.setStudentID(sID1);

					// Name

					System.out.println("Enter Student Name: ");

						String sName1 = kb.nextLine();

							student1.setStudentName(sName1);

								// Course

								System.out.println("Enter Course: ");

									String sCourse1 = kb.nextLine();

										student1.setCourse(sCourse1);

		// student 2

		//ID		

		System.out.println("Enter Student ID: ");

			int sID2 = kb.nextInt();

				student2.setStudentID(sID2);

					// Name

					System.out.println("Enter Student Name: ");

						String sName2 = kb.nextLine();

							student2.setStudentName(sName2);

								// Course

								System.out.println("Enter Course: ");

									String sCourse2 = kb.nextLine();

										student2.setCourse(sCourse2);

		// Print Details								

		System.out.println("Student 1: \tStudent ID \t " + student1.getStudentID() + "\n \t\tName \t " 

										+ student1.getStudentName() + "\n \t\tCourse \t " + student1.getCourse());

		

		System.out.println("Student 2: \tStudent ID \t " + student2.getStudentID() + "\n \t\tName \t " 

										+ student2.getStudentName() + "\n \t\tCourse \t " + student2.getCourse());




			

	

	}


}


OutPut:
Attached File  StudentJavaSkipped.JPG   45.62K   24 downloads

Anyone tell me why this is happening :confused:

Gman

#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

when it get to the StudentName String on both students it skips it why?
Can you explain what you mean by "skips it"?
Post the program's output and add comments showing what the problem is.

Using the Scanner class can be tricky. The class will save the end of line character in its buffer and return that when you don't expect it.
Call the nextLine() method will read the line end character out of the buffer so that the next call to a read method will get the data that the user is currently entering, not the left over line end from the last time the user pressed Enter.

#3
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
i have a screen shot attached, when i say skipped it doesn't let me enter any " student name " as picture will show it just skips to the next part of program " course " also shown in image. Click on the image and it will get bigger. Asked the lecture today and he aslo said it must be something to do with the buffer?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Read Norm's post AGAIN.

#5
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Here's a suggestion; Write a small test program that uses the Scanner class to test using several of its methods in different combinations.
For example if you call nextInt() three times in a row, you can enter three numbers on one line before pressing Enter then the three nextInt() calls will read those three numbers. Print out the results after each nextInt() call.
Then with the same program, just enter one number on a line and press Enter.
What do the second and third nextInt() methods return now?

Also try the nextLine() method.

Also look at using some of the has...() methods to test if there is any input ready to be read and what type of input is ready.

#6
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Thanks Guys for all the help :) sorted.

added kb.nextLine(); to flush it

:)

Gman




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users