1

Hey guys I'm currently making a program where it will reverse the input from the user. Currently my code is

import java.util.*;

public class Reverse {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        List<String> input = new ArrayList<>();
        while (in.hasNextLine())
            input.add(in.nextLine());
        for (int x = 0; input.size() >= x; x++) {
            String reverse = "";
            for (int z = input.get(x).length(); z > 0; z--) {
                reverse += input.get(x).charAt(z - 1);
            }

            System.out.println(reverse);
        }
    }
}

But I'm getting the error where it says

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at Reverse.main(Reverse.java:12)*

Any help would be greatly appreciated thanks!

1
  • You need to read about length and size of the array. Index of array starts from 0; So when you have 3 elements, they will be indexed 0,1,2; At the same time the size of the array is 3; You error is due to the equalOrGreater comparison. I will recommend reading about the foreach loop which will save you some trouble in the future! Good luck! Commented Apr 5, 2016 at 10:50

4 Answers 4

2

Change the bounds of your for loop:

for (int x=0; x < input.size(); x++) {
    String reverse = "";
    for (int z=input.get(x).length(); z > 0; z--) {
        reverse += input.get(x).charAt(z - 1);
    }

    System.out.println(reverse);
}

Accessing a List is zero-based, so in your outer loop, for a list of size 3, the greatest value you want for the x index is 2.

Sign up to request clarification or add additional context in comments.

Comments

0

On last iteraton, "x" is equal to input.size(), so element input.get(x) will not exist, at the second "for" loop.

Comments

0
input.size() >= x

At the last iteration, x is equal to the size of the list. Since list indices start at 0, this is an invalid index.

The idiomatic way of looping by index in Java is

for (int x = 0; x < size; x++)

You should use this idiomatic way.

But for such a loop, where you don't need the index, you should use a for-each loop:

for (String s : input) {

This way is safer, guaranteed to be efficient whatever the type of the list is, and is more readable too.

Comments

0
    package com.stackwork;

    import java.util.*;

    public class Reverse {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            List<String> input = new ArrayList<String>();
            while (in.hasNextLine())
                input.add(in.nextLine());
// prints the size of array
            System.out.println("the array size :" + input.size());
            for (int x = 0; input.size() > x; x++) {
//Prints the incremented x value at each step 
                System.out.println("the x value inside the for loop :" + x);
                String reverse = "";
                for (int z = input.get(x).length(); z > 0; z--) {
//prints the length of each string in the array
                    System.out.println("the value of z is :" + z);
                    reverse += input.get(x).charAt(z - 1);
//prints the value in each step of reverse
                    System.out.println("each step of reverse " + reverse);
                }

                System.out.println(reverse);
            }
        }
    }

1 Comment

Comments and an explanation would make it even clearer! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.