Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Substring Retrieval Method Question

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Substring Retrieval Method Question

    Hows it going guys? Nuggets is back with another simple question for you java gurus here. The question is this.

    How would I return a space that has been entered as input using the indexOf method? For example, if someone were to input say "first name", how would I retrieve the space that has been entered in the string?

    System.out.println("Enter a variable name: ");
    		variableName = Input.nextLine();
    		String variableName1 = variableName.indexOf();


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Substring Retrieval Method Question

    I believe you can use the unicode escape '\u00A0' to represent a blank (space) character. Perhaps you should try using that escape.

    Also, this line of code is wrong:

    String variableName1 = variableName.indexOf();

    First of all, the <String>.indexOf() method returns the index of the specified character in the form of an int. You cannot assign that return value to a String. Secondly, when you call <String>.indexOf(), you have to use parameters. Here are all the possible parameters.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Substring Retrieval Method Question

    Alternatively, you could simply lookup the index of the String " "
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. The Following User Says Thank You to newbie For This Useful Post:

    snowguy13 (January 24th, 2012)

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Substring Retrieval Method Question

    Alternatively, you could simply lookup the index of the String " "
    Hahaha, I totally overlooked that indexOf works with Strings, too. xD Thanks, newbie.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #5
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Substring Retrieval Method Question

    Quote Originally Posted by newbie View Post
    Alternatively, you could simply lookup the index of the String " "
    When I try and use " ", it gives me an error.
    String variableName1 = variableName.indexOf(" ");

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Substring Retrieval Method Question

    Quote Originally Posted by Nuggets View Post
    When I try and use " ", it gives me an error.
    String variableName1 = variableName.indexOf(" ");
    1. Which Java Version are you using?
    2. Why do you need to find the index of space?
    3. What is variableName data type?
    4. indexOf() doesn't return String but an integer.

  8. #7
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Substring Retrieval Method Question

    Quote Originally Posted by Mr.777 View Post
    1. Which Java Version are you using?
    2. Why do you need to find the index of space?
    3. What is variableName data type?
    4. indexOf() doesn't return String but an integer.
    It's a program to check if an inputted variable name is proper or not. If the user enters say "street address", then the output should read "Illegal" because of the space.

    *Edit. My book does say indexOf can return a string also. public int indexOf(String str), Returns the start position of the first occurrence of the specified string.
    Last edited by Nuggets; January 25th, 2012 at 05:04 AM.

  9. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Substring Retrieval Method Question

    Returns the start position of the first occurrence of the specified string.
    How can you justify the position differentiated with the string instead of integer?
    Maps are exceptions.

  10. #9
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Substring Retrieval Method Question

    public int indexOf
    Uhm... This doesn't return a String... It returns an int. Look at the method declaration, it tells you everything... O_O
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  11. #10
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Substring Retrieval Method Question

    Quote Originally Posted by snowguy13 View Post
    Uhm... This doesn't return a String... It returns an int. Look at the method declaration, it tells you everything... O_O
    What's the difference between these two then if both return an integer?

    public int indexOf(int ch)
    Returns the position of the first occurrence of the specified character.

    public int indexOf(String str)
    Returns the start position of the first occurrence of the specified string.

  12. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Substring Retrieval Method Question

    Quote Originally Posted by Nuggets View Post
    What's the difference between these two then if both return an integer?

    public int indexOf(int ch)
    Returns the position of the first occurrence of the specified character.

    public int indexOf(String str)
    Returns the start position of the first occurrence of the specified string.
    indexOf(int ch)
    Returns the index within this string of the first occurrence of the specified character.
    indexOf(int ch, int fromIndex)
    Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
    indexOf(String str)
    Returns the index within this string of the first occurrence of the specified substring.
    indexOf(String str, int fromIndex)
    Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

  13. #12
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Substring Retrieval Method Question

    Can you guys help me out? My code seems to be printing the same output, no matter what I input. Here is the code,

    import java.util.Scanner;
     
    public class Assign4 {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
     
    		String variableName;
    		char ch;
    		boolean legal = true;
     
    		System.out.println("This program checks the properness of a proposed Java" +
    			" variable name.");
    		do {
    			System.out.println("Enter a variable name (q to quit): ");
    			variableName = Input.nextLine();
     
    			if (variableName.isEmpty()) {
    				System.out.println("Illegal.");
    			}
    			ch = variableName.charAt(0);
    			if (!(Character.isUpperCase(ch))); {
    				legal = false;
    				System.out.println("Legal, but uses poor style.");
    				}
    			if ((!(Character.isDigit(ch)))) {
    				legal = false;
    				System.out.println("Illegal.");
    			}
    			else {
    				System.out.println("Good.");
    			}
    			for (int i=1; i<variableName.length() && legal; i++) {
    				ch = variableName.charAt(i);
    				if(!(Character.isWhitespace(ch))) {
    					legal = false;
    				}
    			}
    			if (legal) {
    				System.out.println("Good.");
    			}
    				}while(!Input.equals("q")); {
    			System.out.println("End Program.");
    	}
    	}}

    The output keeps spewing out
    "Legal, but uses poor style.
    Illegal."

    Thanks again for you input guys.

  14. #13
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Substring Retrieval Method Question

    if (!(Character.isUpperCase(ch))); {
    				legal = false;
    				System.out.println("Legal, but uses poor style.");
    				}
    Is this supposed to function as if statement? It will not. What's semicolon ( doing in front of if statement?

Similar Threads

  1. XML InputSource class and the retrieval/browser type
    By dave0110 in forum Object Oriented Programming
    Replies: 2
    Last Post: December 3rd, 2010, 09:11 AM
  2. Content Based Image Retrieval
    By sanjay_jbp in forum AWT / Java Swing
    Replies: 9
    Last Post: March 1st, 2010, 08:55 PM
  3. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM
  4. Xml-Node Retrieval
    By prasb in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 4th, 2009, 12:44 PM
  5. MP3 Player: ID3 Tag Image Retrieval Problem for the Applet from internet
    By JavaJames in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 20th, 2009, 07:11 AM