user85352
user85352

Reputation: 29

Starting with the last character and iterating backwards by 3

I am trying to create a code that takes any string and relays it back to me backwards missing every 3rd character and including the very last character.

EX: "123456789" should return "963" & "Hello, World!" should return "!r lH"

import java.util.Scanner;

public class cypher {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      String str = scnr.nextLine(); 
      String reverse = "";
      System.out.println(str.length());
      for (int i = str.length() - 1; i >= 0; --i) {
        reverse = reverse + str.charAt(i - 3);
      }
      System.out.println(reverse);
   }
}

The code above is what I have so far. However when I run this code I get this error message: "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1"

I don't understand because the string length is 10 so why is it not able to do this? Could someone explain this to me and give me a suggestion on how to fix this?

Upvotes: 0

Views: 643

Answers (5)

sarthak97
sarthak97

Reputation: 21

Changed your code a little and this works fine now.

You were getting the java.lang.StringIndexOutOfBoundsException because your for loop decremented by a single step every time instead of the required step of 3. As a result when the string has less than 3 characters left the value of variable i becomes negative and an exception is thrown when the charAt(i) function is called with a negative value.

import java.util.Scanner;

public class cypher {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      String str = scnr.nextLine(); 
      String reverse = "";
      System.out.println(str.length());
      int i = str.length() - 1;
      while(i >= 0)
      {
        reverse = reverse + str.charAt(i);
        i-=3;
      }
      System.out.println(reverse);
   }
}

Upvotes: 0

Karun sajeev
Karun sajeev

Reputation: 1

I tried this code in my system and it works fine. Try this:

import java.util.Scanner;

public class cypher {


public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  String str = scnr.nextLine(); 
  String reverse = "";
  System.out.println(str.length());
  for (int i = str.length() - 1; i>= 0; i-=3) {
    reverse = reverse + str.charAt(i);
  }
  System.out.println(reverse);


}}

Upvotes: 0

Thanthu
Thanthu

Reputation: 5098

According to your current logic str.charAt(i - 3) here when value of i becomes less than 3, your code will try to access and index that is a -ve number and thus throws this exception.

You'll have to check if i - 3 >= 0 before using str.charAt(i - 3) else break out of the loop.

Upvotes: 0

Mark Melgo
Mark Melgo

Reputation: 1478

You current logic is str.charAt(i - 3) and continue while i >= 0. The str.charAt(i - 3) statement will generate java.lang.StringIndexOutOfBoundsException when i is less than 3, thus you should change your code to this.

public static void main(String args[]) {
    Scanner scnr = new Scanner(System.in);
    String str = scnr.nextLine();
    String reverse = "";
    System.out.println(str.length());
    for (int i = str.length() - 1; i >= 0; i = i - 3) {
        reverse = reverse + str.charAt(i);
    }
    System.out.println(reverse);
}

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522331

I suggest just iterating the characters in the string, starting from the last position, and moving backwards in increments of 3:

Scanner scnr = new Scanner(System.in);
String str = scnr.nextLine();
String reverse = "";

for (int i=str.length()-1; i >= 0; i=i-3) {
    reverse += str.charAt(i);
}

System.out.println(reverse);

Your current approach is failing because the loop just takes single, not triple steps. Also note that you might want to use StringBuilder instead of String to build the reverse string. This might be more efficient (though the JVM itself might substitute StringBuilder on its own).

Upvotes: 1

Related Questions