wayoo
wayoo

Reputation: 125

Loop for grabbing certain char's in a string

If I have a string and an int, I want to be able to create a loop that will print the first char of the string, followed by each the char at value of that int.

e.g. If I have the word "Miracle" and the int 2, the result should be "Mrce". My code does this, but stops a char short for certain words.

  System.out.println(str.charAt(0));


  while (n <= str.length())
  {
    System.out.println(str.charAt(n));
    n = n+n;
  }

This works for strings like "abcdefg" and int 3. It prints "adg", but if the string is "miracle" and int 2, it prints "mrc" and not "mrce".

I'm pretty sure the problem is in the "n= n+n" statement.

Because if the int is 3 and the string is greater than 3 it will loop, but in the n=n+n statement it will loop enough that n will be greater than str length and it halts.

How can I correct this?

Upvotes: 2

Views: 129

Answers (7)

Pshemo
Pshemo

Reputation: 124225

n = n+n; means that in each iteration you are multiplying your n by 2, so

iteration | n 
----------+-------
1         | 3
2         | 3+3=6
3         | 6+6=12

and so on.

What you need is temporary variable (iterator) which will use n but will not change it.

Generally more readable way to write it would be with for loop like

for (int i = 0; i < str.length(); i = i+n){//or `i += n`
     ^^^^^^^^^  ^^^^^^^^^^^^^^^^  ^^^^^^^
//   start at   continue when     in next step
    System.out.print(str.charAt(i));
}

Upvotes: 3

Bubletan
Bubletan

Reputation: 3863

There are two things wrong in your code:

  • If n == str.length(), it will throw an exception, as it tries to access str.charAt(n), which in that case doesn't exist.

  • Another thing, n = n + n will change the value of n every time, so you will add a bigger number every time instead of the same.

You could use a for-loop for a cleaner approach though:

for (int i = 0; i < str.length(); i += n) {
    System.out.println(str.charAt(i));
}

Upvotes: 0

rasmeta
rasmeta

Reputation: 475

First of all, you have an error here:

n <= str.length()

It should be

n < str.length()

because strings are indexed from 0 to length-1. Also make sure you are indexing from 0, not from 1. Another thing is that you are adding a bigger number each time. So yes - you are right about n+n - it's wrong. You should do something like this:

n = ...;
for (int i = 0; n * i < str.length(); ++i)
{
    int index = n * i;
    System.out.println(str.charAt());
}

This way you have n * 0, n * 1, n * 2, ..., which is what you want.

Upvotes: 0

cosmin.danisor
cosmin.danisor

Reputation: 963

I think this is what you need

int n = 0;
int skip = 2;
while (n < str.length())
{
  System.out.println(str.charAt(n));
  n+=skip;
}

Upvotes: 0

Ra&#250;l
Ra&#250;l

Reputation: 1552

It is working for first few instances as 2+ 2 = 4, but after that - its doing 4 + 4 = 8, while you need is 4 + 2 = 6.

Take a new var (v), assign it the initial value, & instead of doing n = n + n, do

n = n + v

Upvotes: 0

glglgl
glglgl

Reputation: 91017

I'll answer with a counterquestion: where is this mentionned int? What walue should it have, and what value does it have?

In short, you should have one variable which has the step value and another variable which acts as a cursor.

Something like

int cursor = 0;

while (cursor <= str.length()) {
    System.out.println(str.charAt(cursor));
    cursor += stepValue;
}

Here you see that it is necessary to have two distinct variables here.

Upvotes: 0

Lrrr
Lrrr

Reputation: 4817

You are right, your problem is with n=n+n because it multiple n with 2 in every step so you must change that.

change your code like this :

int m = 0;
while (m < str.length())
{
   System.out.println(str.charAt(m));
   m = m+n;
}

Upvotes: 5

Related Questions