+ 8
Java Help Needed
public class Program { public static void main(String[] args) { String str = "sololearn"; for(int k=0;k<str.length();k++) { if(k%2==0) System.out.print(str[k]); else System.out.print("+"); } } } why this program is not running?
13 Respostas
+ 12
@Ajay You can use str.charAt() and there are many more available:D
+ 12
@Ajay Yes, it outputs places....I never knew you wanted characters:)
+ 11
public class Program
{
public static void main(String[] args)
{
String str = "sololearn";
for(int k=0;k<str.length();k++)
{
if(k%2==0)
System.out.print(String.valueOf(k));
else
System.out.print("+");
}
}
}
Hope this helps! đ
+ 9
@Dayve
is their any alternative available for this problem? just for first print line.
+ 9
@Dayve String.valueOf(k) is printing places of k, not characters.
+ 9
@Dayve
sorry, I forgot to tell that. đ
+ 8
Thanks guys, this looks something appropriate for me.âș
+ 8
@Nivedita Vaidya
if I declare String str="sololearn";
then why it's not working?
+ 4
Replace str[k] with str.charAt(k)
+ 4
This is another way to get your answer
public class Program
{
public static void main(String[] args)
{
String str[] = {"s","o","l","o","l","e","a","r","n"};
for(int k=0;k<str.length;k++)
{
if(k%2==0)
System.out.print(str[k]);
else
System.out.print("+");
}
}
}
+ 4
str is a string variable and you are accessing it as an array as str[k] hence it is not working.
0
I like you
0
i like you