Reputation: 15
So I'm supposed to write a method that only prints out the even indexes of a string. So for instance if I input “Hiejlzl3ow” into the console/scanner I would want to return "Hello". I got the solution by using a for loop and charAt(i), however I am having trouble returning the result as a String (as it is a char). I tried converting the charAt(i) using String.valueOf but it only prints out the first (or rather the 0) index value (in this case H). Does anyone have a quick fix for this? And is there a simpler solution? (Note: This is beginning java so only methods, for loops, string methods, and scanners are allowed.)
//This method prints out the even indexes of a string
public static String decrypt(String question, Scanner console)
{
System.out.print(question + " ");
String s = console.nextLine();
for (int i = 1; i < s.length()-1; i=i+2)
{
char x = (s.charAt(i));
s = String.valueOf(x);
}
return s;
}
Upvotes: 1
Views: 4937
Reputation: 1
Since this is beginning Java, you might try this:
//This method prints out the even indexes of a string
public static String decrypt(String question, Scanner console)
{
System.out.print(question + " ");
String s = console.nextLine();
for (int i = 0; i < s.length(); i+=2)
{
System.out.print(s.charAt(i));
}
}
Upvotes: 0
Reputation: 1211
Your problem is obvious as you are a beginner. Let me explain you a bit
ZERO(0)
but you are using int i=1
in your for loop.i < s.length()
or i <= s.length()
You can read more about StringBuffer and StringBuilder and how to use them in Java
Try out this code -
public static String decrypt(String question, Scanner console)
{
System.out.print(question + " ");
String s = console.nextLine();
String result ="":
for (int i = 0; i < s.length(); i=i+2)
{
result += s.charAt(i);
}
return result;
}
Follow this link to read other interesting Java String stuff
Upvotes: 0
Reputation: 15334
String blah = "hbellalho";
String evenIndices;
for (int i = 0; i < blah.length; i++) {
if (i % 2 != 0) {
evenIndices += blah.getCharAt(i);
}
}
return evenIndices;
Your main issue is that you were modifying the original string. You need another (or even better, a StringBuilder, though I didn't include this for simplicity) to hold only even values.
When you do += with a String in Java, remember that it will create a new String object each time, so this is not great to have in loops with large iterations.
Upvotes: 0
Reputation: 76
First of All, String index is start from 0. So in for loop you need to set i value to 0. Second mistake in your code is your modifying the string 's' value in for loop. that's why your getting 'i' as output.
Try following code, you will get expected output
//This method prints out the even indexes of a string
public static String decrypt(String question, Scanner console)
{
String str = "";
System.out.print(question + " ");
String s = console.nextLine();
for (int i = 0; i < s.length(); i=i+2)
{
char x = (s.charAt(i));
str = str + String.valueOf(x);
}
return str;
}
Upvotes: 1
Reputation: 1961
You should use i=o
to initialize array and i < s.lenght()
as condition to iterate through complete string.
Upvotes: 0
Reputation: 69460
Array's in java are zero based. So you should start the index with 0:
for (int i = 0; i < s.length()-1; i=i+2)
^^
Upvotes: 0
Reputation: 5868
Try following code:
public static String decrypt(String question, Scanner console)
{
System.out.print(question + " ");
String s = console.nextLine();
StringBuilder builder=new StringBuilder("");
for (int i = 0; i < s.length(); i++)
{
if(i%2==0){
builder.append(s.charAt(i));
}
}
return builder.toString();
}
Input :
Hello World
Output :
HloWrd
For more on StringBuilder
visit this link.
Upvotes: 0
Reputation: 117597
You can use a StringBuilder
:
StringBuilder sb = new StringBuilder();
// ...
sb.append(x);
// ...
String result = sb.toString();
Upvotes: 0