Results 1 to 4 of 4
  1. #1
    Mrlowrider is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Java Basic stringing help

    Hello everyone. I just started a java class and now we are starting strings. I have my program where you are supposed to enter a string of characters and it counts how many there are. However, in the output it seems to skip over words with spaces. The program will work for single words like "the" but when it becomes "The lord of the rings" it still only counts the first word. Here is what it looks like
    Java Code:
    #   System.out.println("**Test String Operations**");                                     
    #     System.out.println("");  
    #     System.out.println("Enter a string of Characters:");  
    #         String stringOne=keyboard.next(); // Here i think is the first problem. keyboard.next seems to only get the first string and not the spaces and words after that.  
    #         int stringLength = stringOne.length();  
    #     System.out.println("The length of the string"+" "+stringOne +" "+ "is" +" " +stringLength);  
    #     System.out.println("Enter an integer between 0 and"+" " + stringLength+":"); //also get an error here when the stringOne Input is with spaces  
    #         int index = keyboard.nextInt();  
    #     System.out.println("The character at Index"+" "+index +" "+ "of the string"+" " + stringOne+ " " + "is"+ " " +stringOne.charAt(index));  
    #     System.out.println("Enter another string of Characters:");  
    #         String stringTwo = keyboard.next();  
    #     System.out.println("The first Occurance of the String"+" "+stringTwo+" "+ "in the string"+" "+ stringOne+ " "+"is at position"+" "+stringOne.indexOf(stringTwo));
    Here is what the output needs to look like

    Enter a string of characters: The Lord of the Rings
    The length of string "The Lord of the Rings" is 21
    Enter an integer between 0 and 20: 10
    The character at index 10 of string "The Lord of the Rings" is 'f'
    Enter another string of characters: Lord
    The first occurrence of string "Lord" in string "The Lord of the Rings" is at position 4

    any help would be appreciated thanks

  2. #2
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Try using a Scanner to do nextLine(); rather than keyboard

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Fortu View Post
    Try using a Scanner to do nextLine(); rather than keyboard
    Im pretty sure keyboard is a scanner object.

    @op: you want to use nextLine, and not next(). When you call next() it gets a string of every character up to the space. So if you want a string from multiple words you need nextLine();

  4. #4
    Mrlowrider is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 07:17 PM
  2. Basic java help =)
    By Xycose in forum New To Java
    Replies: 1
    Last Post: 06-30-2010, 03:20 AM
  3. Basic java
    By santa in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 11:16 AM
  4. basic java
    By vijay24805 in forum New To Java
    Replies: 25
    Last Post: 04-14-2009, 03:46 AM
  5. basic java help
    By adred in forum New To Java
    Replies: 0
    Last Post: 03-08-2008, 01:36 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
  •