Results 1 to 2 of 2
  1. #1
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Help explaining the difference.

    Okay so I have two snippets of code, that to me should be doing the same thing, yet they act differently:

    Java Code:
                    System.out.println( "Please enter your course name:");
    		String nameOfCourse = input.nextLine(); // Input prompt
    		myGradeBook.setCourseName( nameOfCourse ); // Store name
    		
    		System.out.println( "Please enter your course name:");
    		String nameOfCourse2 = input.nextLine(); // Input prompt
    		myGradeBook2.setCourseName( nameOfCourse2 ); // Store name
    		
    		
    		System.out.println( "Please enter your course name:");
    		String nameOfCourse3 = input.nextLine(); // Input prompt
    		myGradeBook3.setCourseName( nameOfCourse3 ); // Store name

    and

    Java Code:
                    // Account 1
    		
    		System.out.println( "How much would you like to deposit? " );
    		depositAmount = input.nextDouble();
    		System.out.printf( "\nadding %.2f to account1 balance...\n\n",
    				depositAmount);
    		account1.credit( depositAmount );
    		
    		// Account 2
    		
    		System.out.println( "How much would you like to deposit? " );
    		depositAmount = input.nextDouble();
    		System.out.printf( "\nadding %.2f to account2 balance...\n\n",
    				depositAmount);
    		account2.credit( depositAmount );

    Notice the variable in the parameters in account 1 and 2.credit are the same. Why won't this work when using strings? It forces me to use different names in the parameter in the gradebook.set areas.

    If you need more info, just ask please.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Scanner#nextLine() swallows the end of line (EOL) token while Scanner#nextDouble() doesn't. If you grab the nextDouble token and also need to grab the EOL, then call input.nextLine() after calling input.nextDouble(). For example:
    Java Code:
    depositAmount = input.nextDouble();
    input.nextLine(); // swallow the EOL token

Similar Threads

  1. what the difference?!!
    By Engineer in forum New To Java
    Replies: 14
    Last Post: 08-29-2010, 04:57 PM
  2. What is the difference between
    By arnab321 in forum New To Java
    Replies: 2
    Last Post: 01-19-2009, 04:49 AM
  3. Replies: 4
    Last Post: 01-01-2009, 12:03 PM
  4. what is the difference between jsp and jsf?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-31-2008, 05:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •