Sending array from one method to another in Java?
I’m wanting to write an opportunity that results in a string array according to the user’s insight and directs that number to a further method that will adds a new “.” to help every element of the variety, and after that sends it returning to the most important method.I’m obtaining a java.lang.ArrayIndexOutOfBoundsException error to the two lines that we marked //error.Any idea what I’m executing wrong
scan java.util.Scanner;
scan java.util.*;
scan java.util.ArrayList;
scan java.lang.Stringed;
criminal court class CalendarDisplay
criminal court static emptiness main(String args)
System.out.println(“Please enter the quantity of words you choose in the array”);
Scanner input = different Scanner(System.in);
int word_count = Integer.parseInt(input.nextLine());
Stringed words = different String word_count;
for(int post = 0; post < word_count; i++)
System.out.println(“Enter your word (q to halt entering words):”);
Stringed entry = reviews.next();
in the event (entry.equalsIgnoreCase(“q”))
break up;
wordsi = entry;
Stringed myWords = brand new Stringword_count;
myWords = ReturnedValue(words, word_count); // error
int time-span = myWords.time-span;
System.out.println( “Number of arguments sent to method:” + length);
for(int j = 0; j < time-span; j++)
System.out.println(“Array component ” + (j+1) + “:” + myWordsj);
criminal court static String ReturnedValue(String terms, int word_count)
Stringed newArray = brand new Stringword_count;
int newLength = thoughts.length;
for(int nited kingdom = 0; nited kingdom <= newLength; k++)
newArrayk = wordsk + “.”; // error
returning newArray;
You want k < newLength, not less than or similar.
change your own function to be able to this
criminal court static String ReturnedValue(String terms, int word_count)
Stringed newArray = brand new Stringword_count;
int newLength = thoughts.length;
for(int nited kingdom = 0; nited kingdom <newLength; k++)
newArrayk = wordsk + “.”; // error
returning newArray;
Leave a Reply
You must be logged in to post a comment.