Java ArrayIndexOutOfBoundsException error when correct code is put together

<>

This question already has an answer here:

Answers


This line seems to be throwing the exception:

shortestLengths=(myWords[1]).length();

So myWords must not have a second element, meaning the string Sentence didn't contain a space (" ") to be split on. Since Sentence is coming from sc.nextLine(), the "problem" must be with your input - but the true issue is that you're not handling that case properly.

Just glancing at your logic, I don't see why shortestLengths couldn't start out as Integer.MAX_VALUE since you're looping over each word and seeing if its length is less than that.


Need Your Help

Coordinate transformation from earth to solar

3d physics coordinate-transformation

I am building a 3D model of the solar system and need to figure out the position of the pole stars of each planet in order to tilt the planets in the correct direction the correct amount. I've alre...

Angularjs - Form Post Data Not Posted?

javascript php angularjs

I must admit I am a little bit confused... I have never done this before and I am apparently missing something

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.