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?
Code :
System.out.println("Enter a variable name: ");
variableName = Input.nextLine();
String variableName1 = variableName.indexOf();
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.
Re: Substring Retrieval Method Question
Alternatively, you could simply lookup the index of the String " " :D
Re: Substring Retrieval Method Question
Quote:
Alternatively, you could simply lookup the index of the String " "
Hahaha, I totally overlooked that indexOf works with Strings, too. xD Thanks, newbie. :)
Re: Substring Retrieval Method Question
Quote:
Originally Posted by
newbie
Alternatively, you could simply lookup the index of the String " " :D
When I try and use " ", it gives me an error.
String variableName1 = variableName.indexOf(" ");
Re: Substring Retrieval Method Question
Quote:
Originally Posted by
Nuggets
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.
Re: Substring Retrieval Method Question
Quote:
Originally Posted by
Mr.777
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.
Re: Substring Retrieval Method Question
Quote:
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.
Re: Substring Retrieval Method Question
Quote:
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
Re: Substring Retrieval Method Question
Quote:
Originally Posted by
snowguy13
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.
Re: Substring Retrieval Method Question
Quote:
Originally Posted by
Nuggets
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.
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,
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.
Re: Substring Retrieval Method Question
Code :
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?