Results 1 to 4 of 4
  1. #1
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Question 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: ");
    is executed, an exception is thrown, reporting that the string at character 2 (which I try to assign to a variable a few lines down) is out of the range of the string. However, if I declare a new Scanner object (scan2), I don't get the error. What is wrong with my code that requires me to create another Scanner object?

    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;	
    	   }
    	   
        }
    }

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

    Default

    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();

  3. #3
    Chase is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default

    That fixed it. Thank you!

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

Similar Threads

  1. Declare methods in a class
    By Adiel224 in forum New To Java
    Replies: 5
    Last Post: 09-19-2008, 10:38 AM
  2. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  3. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 12:59 AM
  4. Replies: 1
    Last Post: 12-21-2007, 11:12 AM
  5. Replies: 1
    Last Post: 08-06-2007, 02:17 AM

Posting Permissions

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