user3220565
user3220565

Reputation: 125

Printing alternate elements of a string array

I am having trouble executing the code for Printing alternate elements of a string array.

I have declared a string "welcome" and I want to read the alternative elements like "W, l, o, etc.

//Print alternate elements of a string array.

public class AlternateStringArray {

   public static void main(String[] args) {

       String str[]= new string[] {"Welcome"};

       for (int i=0; i<7; i+2){

       System.out.println(str[i]);

    }
  }
}

Receiving the below error:

Type mismatch: cannot convert from string[] to String[]

Type mismatch: cannot convert from String to string

Syntax error on token "+", invalid AssignmentOperator

Please help.

Upvotes: 0

Views: 19901

Answers (9)

Kushwaha
Kushwaha

Reputation: 918

I am pretty late but may this answer help someone. I used java 8 and you can do this in one line of code.

Let's say we have List of String as below (you can take String array and create a list of it)

special

List<String> names = Arrays.asList("A","B","C","D","E","F","G");

In such case always get range of IntStream add filter like below

IntStream.range(0, names.size()).filter(f -> f%2 == 1)

if you print above line of code, it is going to print odd element (elements on 1, 3, 5 etc.) as filter condition is f%2 == 1 and if you want to get even elements, then change filter as f%2 == 0, this will give you elements at 0, 2, 4 etc.

Now it's just matter of getting elements from the list, to do this, you can use function mapToObj as below

To get Odd element from the list

IntStream.range(0, names.size()).filter(f -> f%2 == 1).mapToObj(names::get).forEach(System.out::println);

To get even element from the list

IntStream.range(0, names.size()).filter(f -> f%2 == 0).mapToObj(names::get).forEach(System.out::println);

Your specific problem

List<String> welcome = Arrays.asList("Welcome".split(""));
IntStream.range(0, newA.size()).filter(f -> f%2 == 0).mapToObj(newA::get).forEach(System.out::println);

This will print exactly what you want!

Happy coding!

Upvotes: 0

Ankush kumar
Ankush kumar

Reputation: 61

public class PrintAlternativeCharacters {

public static void main(String[] args)
{

    String str = "ROCKSTAR";
    String newStr="";

    //EXPECTED OUT PUT :: OR-KC-TS-RA

    char[] a = str.toCharArray();

    System.out.println("*****************"+a.length);

    for(int i = 0 ; i < a.length ; i=i+2)
    {
        newStr=newStr+str.charAt(i+1)+str.charAt(i);
    }


    System.out.println(newStr);
}

}

Upvotes: 0

Vedant Terkar
Vedant Terkar

Reputation: 4682

as every one stated:

change:

String str[]= new string[] {"Welcome"};

to:

String str[]= new String[] {"Welcome"};

but why?

so here's my answer:

java is strictly binded object oriented programming language. everything here is either class, object or method.

In Java, when you do:

String xyz = new String("abc");

You force the creation of a new String object of String class, this takes up some time and memory at time of creation.

but string on the other hand is treated as literal; which can't have its objects. and thus the error.

for the second error :

we know that the syntax of for statement is,

for(initialization; condition;increment/decrement)

so as you note third condition isn't satisfiable in i+2, so change it to i=i+2 or in short i+=2.

thus you're getting that error.

Also you're creating array of String objects. in which str[0]th element is your string "Welcome". to access character from string at certain position we use charAt(int position) method for string objects.

so here to access characters from 0th string of str array, we'll use:

str[0].charAt(position).

Thus your final working code'll be:

public class AlternateStringArray {

   public static void main(String[] args) {

       String str[]= new String[] {"Welcome"};  //note change 
       for (int i=0; i<7; i=i+2){  //note change 
           System.out.println(str[0].charAt(i)); //note change
    }
  }
}

also instead of using fixed length i<7, i'll suggest to get the length of string dynamically using .length() method. as:

i<str[0].length().

so the code now is :

public class AlternateStringArray {

   public static void main(String[] args) {

       String str[]= new String[] {"Welcome"};  //note change 
       for (int i=0; i<str[0].length(); i+=2){  //note change 2
           System.out.println(str[0].charAt(i)); //note change
    }
  }
}

always do understand your code before writing it :-)..

hope it'll help you.... cheers !!

Upvotes: 1

FlarrowVerse
FlarrowVerse

Reputation: 315

Change your line

String str[]=new string[]{"Welcome"};

to-

String str[]=new String[]{"Welcome"};

Upvotes: 0

Vinayak Pingale
Vinayak Pingale

Reputation: 1315

This is what you are expecting to do.

String str[] = new String[] { "Welcome" };
for (int i = 0; i < 7; i += 2) {
        char c = str[0].charAt(i);
        System.out.println(c);
}

But whatever approach you are using is not at all valid for many reasons. As Vakh has said you can use that kind of approach , clean and easy to understand.

OR something like this.

String str = new String("Welcome");
    for (int i = 0; i < str.length(); i += 2) {
        System.out.println(i + "::" + str.charAt(i));
    }

Upvotes: 0

PaolaG
PaolaG

Reputation: 814

Replace

String str[]= new string[] {"Welcome"};

to: 
String str[]= new String[] {"Welcome"};

For the follow error:

Syntax error on token "+", invalid AssignmentOperator

for (int i=0; i<7; i++){

if(i%2 == 0) 
{
   System.out.println(str[i]);
}

}

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53839

Try:

String str = "Welcome";
char[] strChars = str.toCharArray();
for(int i = 0; i < strChars.length; i += 2) {  // go through all the characters
    System.out.println(strChars[i]);
}

Upvotes: 3

Alexander_Winter
Alexander_Winter

Reputation: 314

You have an error.

 String str[]= new string[] --> String str[]= new String[]

Upvotes: 0

sadhu
sadhu

Reputation: 1479

String str[]= new string[] {"Welcome"};

should be

String str[]= new String[] {"Welcome"};

Upvotes: 0

Related Questions