Results 1 to 2 of 2
Thread: Help explaining the difference.
- 11-14-2010, 01:52 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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.
-
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
-
what the difference?!!
By Engineer in forum New To JavaReplies: 14Last Post: 08-29-2010, 04:57 PM -
What is the difference between
By arnab321 in forum New To JavaReplies: 2Last Post: 01-19-2009, 04:49 AM -
Constructors, Setter & Getter << need some explaining
By A.M.S in forum New To JavaReplies: 4Last Post: 01-01-2009, 12:03 PM -
what is the difference between jsp and jsf?
By makpandian in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 12-31-2008, 05:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks