Results 1 to 4 of 4
- 09-24-2008, 07:04 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Why do I need to declare a new Scanner object here?
I've been working on a lab assignment for my introductory java class at ASU.
I will post the full source code for the small program below, but the segment that's causing me problems is:
Java Code://# Part C: //1. Get a string from the user. Find and print if the 3rd character in that string is a capital letter. Scanner scan2 = new Scanner(System.in); String usermsg,CAPusermsg; System.out.print(username+", please enter a string: "); usermsg = scan2.nextLine(); CAPusermsg = usermsg.toUpperCase(); char msgthird = usermsg.charAt(2); char capthird = CAPusermsg.charAt(2); if (msgthird == capthird) { System.out.println(username+", the third character in the string, \""+msgthird+"\", is a capital letter!"); } else { System.out.println(username+", the third character in the string, \""+msgthird+"\", is NOT a capital letter!"); }
Right after:
Java Code:System.out.print(username+", please enter a string: ");
Thanks.
Java Code:// Import the Scanner class from the util package. import java.util.Scanner; public class lab4 { public static void main(String[] args) { // Part A: // Print the string "Please enter your name: ". Use System.out.print and not System.out.println. System.out.print("Please enter your name: "); // Instantiate a Scanner object. Scanner scan = new Scanner(System.in);a // Get a string from the user, and save it in a string variable called "username". String username = scan.nextLine(); // Print a welcome message using the username you obtained. System.out.println("Welcome "+username+"."); //# Part B: //1. Prompt the user to enter two numbers, and print "the larger of the two numbers is: " ... int num1, num2; System.out.print(username+", please enter an integer: "); num1 = scan.nextInt(); System.out.print(username+", please enter another integer: "); num2 = scan.nextInt(); if (num1 > num2) { System.out.println("The larger of the two numbers is: " + num1); } else if (num1 < num2) { System.out.println("The larger of the two numbers is: " + num2); } else { System.out.println("The numbers are the same!"); } //# Part C: //1. Get a string from the user. Find and print if the 3rd character in that string is a capital letter. Scanner scan2 = new Scanner(System.in); String usermsg,CAPusermsg; System.out.print(username+", please enter a string: "); usermsg = scan2.nextLine(); CAPusermsg = usermsg.toUpperCase(); char msgthird = usermsg.charAt(2); char capthird = CAPusermsg.charAt(2); if (msgthird == capthird) { System.out.println(username+", the third character in the string, \""+msgthird+"\", is a capital letter!"); } else { System.out.println(username+", the third character in the string, \""+msgthird+"\", is NOT a capital letter!"); } //# Part D: //1. Print all the numbers from 1 to 20. int count = 1; while (count <= 20) { System.out.print(count+" "); count++; } System.out.println(); //2. Print all the even numbers from 1 to 20. int count2 = 1; while (count2 <= 20) { if (count2*2 <= 20) { System.out.print(count2*2+" "); count2++; } else count2 = 21; } } }
-
Your problem likely stems form the fact that just prior to getting this String, you were getting ints using the nextInt() method. Now this Scanner method will get the next token available and will not throw a fit if it's an int, but it doesn't eat the end of line character. For that you need a call to nextLine():
Java Code://# Part C: //1. Get a string from the user. Find and print if the 3rd character in that string is a capital letter. //scan = new Scanner(System.in); scan.nextLine(); // swallow the end of line character String usermsg,CAPusermsg; System.out.print(username+", please enter a string: "); usermsg = scan.nextLine();
- 09-24-2008, 09:16 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
That fixed it. Thank you!
-
Similar Threads
-
Declare methods in a class
By Adiel224 in forum New To JavaReplies: 5Last Post: 09-19-2008, 11:38 AM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 05:27 PM -
Where is it best to declare swing components?
By MacNstuff in forum AWT / SwingReplies: 1Last Post: 02-06-2008, 01:59 AM -
Regarding Scanner Object -- Cannot Resolve the Symbol
By pascal45 in forum New To JavaReplies: 1Last Post: 12-21-2007, 12:12 PM -
How would I declare the variable numbers as global?
By barney in forum New To JavaReplies: 1Last Post: 08-06-2007, 03:17 AM
Bookmarks