2

I have a java program where the following is what I wanted to achieve:

first input: ABC second input: xyz output: AxByCz 

and my Java program is as follows:

 import java.io.*; class DisplayStringAlternately { public static void main(String[] arguments) { String firstC[], secondC[]; firstC = new String[] {"A","B","C"}; secondC = new String[] {"x","y","z"}; displayStringAlternately(firstC, secondC); } public static void displayStringAlternately (String[] firstString, String[] secondString) { int combinedLengthOfStrings = firstString.length + secondString.length; for(int counter = 1, i = 0; i < combinedLengthOfStrings; counter++, i++) { if(counter % 2 == 0) { System.out.print(secondString[i]); } else { System.out.print(firstString[i]); } } } } 

however I encounter the following runtime error:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 AyC at DisplayStringAlternately.displayStringAlternately(DisplayStringAlternately.java:23) at DisplayStringAlternately.main(DisplayStringAlternately.java:12) Java Result: 1 

What mistake is in my Java program?

6
  • 1
    Both array length is same ? Commented Aug 1, 2014 at 16:01
  • 2
    Neither of your arrays have an index of 3. Both have indexes of 0, 1, and 2. Commented Aug 1, 2014 at 16:02
  • You are incrementing i once per iteration of the for-loop. It seems like you only want to increment it every two iterations. Commented Aug 1, 2014 at 16:02
  • yes @sᴜʀᴇsʜᴀᴛᴛᴀ. They are both same in lenght Commented Aug 1, 2014 at 16:02
  • 1
    @vishnu Added my answer. Hope that helps :) Commented Aug 1, 2014 at 16:06

6 Answers 6

2

If both arrays have same length for loop should continue while i < anyArray.length.

Also you don't need any counter to determine from which array you should print first. Just hardcode that first element will be printed from firstString and next one from secondString.

So your displayStringAlternately method can look like

public static void displayStringAlternately(String[] firstString, String[] secondString) { for (int i = 0; i < firstString.length; i++) { System.out.print(firstString[i]); System.out.print(secondString[i]); } } 

Anyway your code throws ArrayIndexOutOfBoundsException because each time you decide from which array print element you are incrementing i, so effectively you are jumping through arrays this way

 i=0 i=2 {"A","B","C"}; {"x","y","z"}; i=1 i=3 ^^^-here is the problem 

so as you see your code tries to access element from second array which is not inside of it (it is out of its bounds).

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

1 Comment

thank you. Though there are other acceptable answers, yours has more in depth explanation. thank you very much
2

As you commented, If both arrays length is same, you can simply do

 firstC = new String[] {"A","B","C"}; secondC = new String[] {"x","y","z"}; 

Then

 for(int i = 0; i < firstC.length; i++) { System.out.print(firstC[i]); System.out.print(secondC[i]); } 

1 Comment

This is the proper solution if the arrays are guaranteed to have the same length.
0

Using the combined length of the Strings is wrong, since, for example, secondString[i] would cause an exception when i >= secondString.length.

Comments

0

Try the below working code with high performance

public static void main(String[] arguments) { String firstC[], secondC[]; firstC = new String[] {"A","B","C"}; secondC = new String[] {"x","y","z"}; StringBuilder builder = new StringBuilder(); for (int i = 0; i < firstC.length; i++) { builder.append(firstC[i]); builder.append(secondC[i]); } System.out.println(builder.toString()); } 

Comments

0

public class concad {

public void main(String[] args) { String s1 = "RAMESH"; String s2 = "SURESH"; int i; int j; for (i = 0; i < s1.length(); i++) { System.out.print(s1.charAt(i)); for (j = i; j <= i; j++) { if (j == i) { System.out.print(s2.charAt(j)); } } } } 

}

1 Comment

What is the purpose of the second for loop? It has only one iteration ... always. And the value of j is always the same as in i.
0

I have taken two strings as mentioned.Then pass one counter variable in inner for-loop with second string,Then for every even position pass with code "counter%2".Check this out if any concern then comment below.

public class AlternatePosition { public static void main(String[] arguments) { String abc = "abcd"; String def = "efgh"; displayStringAlternately(abc, def); } public static void displayStringAlternately(String firstString, String secondString) { for (int i = 0; i < firstString.length(); i++) { for (int counter = 1, j = 0; j < secondString.length(); counter++, j++) { if (counter % 2 == 0) { System.out.print(secondString.charAt(i)); break; } else { System.out.print(firstString.charAt(i)); } } } } } 

1 Comment

it would be better if you could had some description in your answer. It would be more useful for future readers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.