473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Im sure its a really easy answer

I can get my program to compile but when I try to run it I get this message:

java Max1 2 51 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Max1.main(Max1.java:11)


here is my program:
Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] args){
  2.  
  3.         int max = Integer.MIN_VALUE;
  4.         int var = Integer.parseInt(args[args.length]);
  5.         int x[] = new int[var];
  6.  
  7.         for (int i = 0; i < args.length; i++) {
  8.  
  9.             if(x[i] > max)
  10.                 max = x[i];
  11.  
  12.         System.out.print(max);

I know that it means im trying to reach an index that is unavailable but I just dont know how to make it work so that a user types in however many numbers he wants, and the program outputs the largest number value.

I know this is probably really simple, but help?
Nov 4 '09 #1
4 1543
pbrockway2
151 Expert 100+
That code won't compile.

I think you have a typo (a missing couple of }), but it's a really code idea to cut and paste your code and use the code button so that we can see exactly what the real line 11 is.

You have

Expand|Select|Wrap|Line Numbers
  1. int var = Integer.parseInt(args[args.length]);
  2.  
That line is always going to generate an AIIOOBE because args.length is the length of the array of arguments so that array runs from 0 to args.length-1.

In fact you don't have to parse anything to get the desired length of the x array: just make it have length args.length. What you do have to parse is each of the string elements of the args array so that you get int values that you can put into the x array.

Only once you have populated the x array with int values can you use the for loop to figure out the maximum value.
Nov 4 '09 #2
Expand|Select|Wrap|Line Numbers
  1. public class Max1 {
  2.  
  3.     public static void main(String[] args){
  4.  
  5.         int max = Integer.MIN_VALUE;
  6.         int var = Integer.parseInt(args[args.length]);
  7.         int x[] = new int[var];
  8.  
  9.         for (int i = 0; i < args.length; i++) {
  10.  
  11.             if(x[i] > max)
  12.                 max = x[i];
  13.  
  14.         System.out.print(max);
  15.     }
  16. }
  17.  
  18. }
  19.  
That is my full program that I am working on. I have tried running it without parsing but then I get another error about it being incompatible.
Nov 4 '09 #3
pbrockway2
151 Expert 100+
args[args.length] will throw an AIOOBE. And it will do so for any array args. Every time.

The solution is to initialise x to be an int array of length args.length

If you are getting "another error" then you have to fix that other error.
Nov 4 '09 #4
Frinavale
9,735 Expert Mod 8TB
I think what pbrockway2 is getting at is that arrays are 0 bound.
Say you have the following array:
Expand|Select|Wrap|Line Numbers
  1. String myArray[] = {"firstElement","secondElement","thirdElement"};
Since myArray has 3 elements, myArray.length will return 3.

The thing is that arrays in Java are 0 based. This means that the first element in the array resides at index 0.

So,
  • myArray[0] will give you "firstElement",
  • myArray[1] will give you "secondElement",
  • and myArray[2] will give you "thridElement"

If you try to access myArray[3] you will get an error because the array index 3 doesn't exist.

Therefore if you have:
Expand|Select|Wrap|Line Numbers
  1. String theLastElement = myArray[myArray.length];
You will get an exception. To retrieve the last element properly (without exception) you should be accessing the element at myArray.length-1 (which is the index of the last element):
Expand|Select|Wrap|Line Numbers
  1. String theLastElement = myArray[myArray.length-1];
So, to fix your problem, change your code to:

Expand|Select|Wrap|Line Numbers
  1. int var = Integer.parseInt(args[args.length-1]);
This will give you the last element in the "args" array.

(Please be aware that if the last element in the args array is not an Integer you will experience a different type of error....)

-Frinny
Nov 4 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Oliver Spiesshofer | last post by:
Hi, I want to make a standard-function that handles "Are you sure"-type questions. In my idea it should look something like this: <code> Function deleteitem($item) {
21
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds...
3
by: besbello | last post by:
Hi all, I want to submit a form when the user closes the browser, to keep a count of the number of users accesing my application at each moment. To do this, I use the onbeforeunload() event, that...
37
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
15
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
10
by: Stan R. | last post by:
Hi. I'm an old programmer whose been finally reading up on xml the past week. The concepts of xml, dtd, and xsl seem pretty straight forward to me. I understand that xsl (as xslt for...
56
by: tasteless | last post by:
Hi guys, I need really hard questions (about 10) about PHP programming (some of elements OOP as well, but no MySQL questions - this is different part), this questions needs to be very hard, but...
6
by: SAL | last post by:
Hi, VS2005 post I'm opening a window using the following syntax: Protected Sub lbEstValue_Click(ByVal sender As Object, ByVal e As System.EventArgs)...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.