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 9 of 9

Thread: Basic Code Problem

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Basic Code Problem

    project directions:

    1. Create a class called Student that has name, grade, house as instance variables

    2. Create a constructor that take these (3) items as parameters and sets them to your private instance/global variables

    3. Next, create a test class StudentTest


    4. Inside of StudentTest create an ArrayList called myList


    5. Create a loop that runs 5 times. It asks the user for the (3) pieces of information (name, grade, house). Take that info and create and Student object that will be added to the ArrayList


    Test class

    public class StudentTest {
     
    	/**
    	 * main
    	 * @param args
    	 */
    	public static void main(String[] args)
    	{
     
    		Scanner keyboard = new Scanner(System.in);
    		ArrayList<Student> myList = new ArrayList<Student> ();
     
    		for (int i = 0; i < 5; i++)
    		{
     
    			System.out.println("What is the name of the student?");
    			String name = keyboard.nextLine();
     
    			System.out.println("What is his/her house?");
    			String house = keyboard.nextLine();
     
    			System.out.println("What is his/her grade? Numbers only, please.");
    			int grade = keyboard.nextInt();
     
    			myList.add(i, new Student(name, grade, house));
     
     
    		}
     
     
    	}
     
     
    }


    Student class

    public class Student {
     
    	//instance variables
    	String name;
    	int grade;
    	String house;
     
     
     
    	/**
    	 * student constructor
    	 * @param name
    	 * @param grade
    	 * @param house
    	 */
    	public Student(String name, int grade, String house)
    	{
    		this.name = name;
    		this.grade = grade;
    		this.house = house;
     
    	}		
     
     
    }



    Console:

    What is the name of the student?
    Bob
    What is his/her house?
    Random
    What is his/her grade? Numbers only, please.
    9
    What is the name of the student?
    What is his/her house?



    I couldn't type in the name of the second student because the next line "What is his/her house?" came up with it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Basic Code Problem

    Scanner methods can be tricky. The Scanner buffers input and can block waiting for input. It will save the lineend character generated by pressing Enter in its buffer and return it with the nextLine() call

    For example if you enter: A word to the wise <PRESS ENTER>
    and use next() only "A" is read, and "word to the wise" is left in the buffer.
    Your next attempt to get something from Scanner will be to get "word".
    nextInt() will fail here.
    To clear the buffer, use the nextLine() method.
    To test if the next token is an int, use the hasNextInt() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Basic Code Problem

    Hello msinc210!
    I think this is because when you hit Enter after the grade input, the nextLine() method takes that blank as input. To understand what's going on you should print the value of the inputs after you get them. ie for the name variable System.out.println("Name "+i+" = " +name);
    To solve this you could try the Scanner's next() method instead of nextLine().
    Hope this helps.

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

    msinc210 (June 3rd, 2012)

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Basic Code Problem

    How do you handle it then so that it would do two or more words and not go mess up with the hitting Enter?

  6. #5
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Basic Code Problem

    You were right andreas90. The program thought the blanks were inputs. Changed everything to the next() method and it works fine now.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Basic Code Problem

    nextLine() method takes that blank as input.
    It's the lineend character in the Scanner's buffer that is returned as an empty String, the blanks/spaces in the input are skipped over.
    If you added this: System.out.println("Name "+i+" =" +name +"<");
    you'd see no blanks between the = and the < showing that name held an empty String.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    andreas90 (June 3rd, 2012)

  9. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Basic Code Problem

    Quote Originally Posted by msinc210 View Post
    You were right andreas90. The program thought the blanks were inputs. Changed everything to the next() method and it works fine now.
    Read post#2 and try to understand it and what's going on because using Scanner can indeed be a little tricky as Norm said.

  10. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Basic Code Problem

    Ok, so this is how you could avoid the thing:

       import java.util.Scanner;
     
       public class TrickTheScanner
       {
     
     
          public static void main(String[] args)
          {
     
     
             Scanner scan = new Scanner(System.in);
     
     
             for (int i=0; i < 5; i++)
             {
                System.out.println("Input stuffs:");
     
                String input = scan.nextLine();
     
     
                System.out.println(input);
                String input2 = "";
                try
                {
                   input2 = scan.nextLine();
     
                   int value = Integer.parseInt(input2);
                }
     
                   catch(java.util.InputMismatchException ime)
                   {
     
     
                   }
     
     
             }
     
     
     
     
          }
     
     
     
     
       }

    Wow, I either didn't realize that about the Scanner either before, or had forgotten about it.
    Last edited by javapenguin; June 3rd, 2012 at 04:02 PM.

  11. #9
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Basic Code Problem

    Quote Originally Posted by Norm View Post
    It's the lineend character in the Scanner's buffer that is returned as an empty String, the blanks/spaces in the input are skipped over.
    If you added this: System.out.println("Name "+i+" =" +name +"<");
    you'd see no blanks between the = and the < showing that name held an empty String.
    My bad. Not very well expressed, by blank I meant an empty String. Thanks for pointing that out.

Similar Threads

  1. Problem with fairly basic recursive method
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2011, 09:58 PM
  2. having bad time with basic calculator code
    By hashey100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 09:29 PM
  3. Very Basic Java input Problem
    By coke32 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 9th, 2011, 06:54 PM
  4. [SOLVED] Very basic code problem
    By frederick213 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 12:33 PM
  5. Basic Math Expression Java Problem
    By andyluvskrissy in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 02:46 PM