if i have four words in a string and they are all seperated by an "*" how can i get the position of all three * between the four words?
Printable View
if i have four words in a string and they are all seperated by an "*" how can i get the position of all three * between the four words?
Check out the String API. There are methods there that will help you. If you still have questions after doing this, come on back and let us know which methods you think should help you the most.
Good luck!
Yes, read the Java docs and give a try. If you are stuck on something come back here and ask your question with what you have. As a hint, you have to deal with the index.
ok i thought i had a good way to figure out the locations of the remaining *
but i keep receiving an error message can you please take a look at my code thanks
import javax.swing.JOptionPane;
public class Project2
{
public static void main(String[]args) // main method header
{
//variable declarations
String word,word1,word2,word3,word4;
int ast1,ast2,ast3;
int word1Num,word2Num,word3Num,word4Num;
int totalLen;
char word1Last; // displays the last character in word1
String part2;
// user input
word = JOptionPane.showInputDialog("Enter four words, seperate each word with an * symbol "); // assignment statements
// assignments
totalLen = word.length();
ast1 = word.indexOf('*'); // stores position of the first asteric as an integer
word1 = word.substring(0,ast1); // creates substring that stores the first word as a string
part2 = word.substring(ast1 + 1); // creates a substring to store everything after the first asteric
ast2 = part2.indexOf('*'); // stores the second location of the * within the new string part2
word2 = part2.substring(ast1 ,ast2 - 1); // stores the second word in a string location word2
word1Num = word1.length(); // stores the length of characters in word1 as word1Num
word2Num = word2.length(); // stores the length of characters in word2 as word2Num
// print output
System.out.println("total length is " + totalLen);
System.out.println(word1 + " length is " + word1Num);
System.out.println();
System.out.println(word2 + " length is " + word2Num);
}
}
1) What are the error messages?
2) Where are they being called from? Which lines?
3) When posting code, please use code tags so that your code retains its formatting and is readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom.
ok it says theres an error in line 31 , its not showing the lines in eclispe for some reason but the error message displayed saysCode:import javax.swing.JOptionPane;
public class Project2
{
public static void main(String[]args) // main method header
{
//variable declarations
String word,word1,word2,word3,word4;
int ast1,ast2,ast3;
int word1Num,word2Num,word3Num,word4Num;
int totalLen;
char word1Last; // displays the last character in word1
String part2;
// user input
word = JOptionPane.showInputDialog("Enter four words, seperate each word with an * symbol "); // assignment statements
// assignments
totalLen = word.length();
ast1 = word.indexOf('*'); // stores position of the first asteric as an integer
word1 = word.substring(0,ast1); // creates substring that stores the first word as a string
part2 = word.substring(ast1 + 1); // creates a substring to store everything after the first asteric
ast2 = part2.indexOf('*'); // stores the second location of the * within the new string part2
word2 = part2.substring(ast1 ,ast2 - 1); // stores the second word in a string location word2
word1Num = word1.length(); // stores the length of characters in word1 as word1Num
word2Num = word2.length(); // stores the length of characters in word2 as word2Num
// print output
System.out.println("total length is " + totalLen);
System.out.println(word1 + " length is " + word1Num);
System.out.println();
System.out.println(word2 + " length is " + word2Num);
}
}
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
at java.lang.String.substring(Unknown Source)
at Project2.main(Project2.java:31)"
im not sure what that means or how to go about fixing it yet
You are try to accessing an invalid index in the string.
Also, have a look at the String methods again. There's more than one indexOf(...) method, and you may get some benefit by using the other one as well.
Here in this line you have done the mistake.
Did you read the Java doc. If you are using this overloaded method, first argument is the starting index of the string and it is zero base. Second argument is the end index and it's not zero base. That's why you get an exception here.Code:word2 = part2.substring(ast1 ,ast2 - 1);
for (int i = 0; i < string.length(); i++){
if(string.charAt(i) == '*')
//do something then substring it
}
Use StringTokenizer class. The below link will help
javaadda.blogspot.com/2008/10/how-to-break-string-into-tokens-with.html