Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
Hi, I have enrolled into Java classes and got this problem that i have mostly figured out how to do, but my application gives me an error when i run it. I understand that there is a mistake, but i can't figure out what is causing it and if it can be fixed.
The application is from a Java texbook, and its purpose is to calculate sales comissions.
Code:
at $200 base + 9% of sales = salary.
I'm using an array to feed the sales figures, then trying to do the math upon the values of the array and count how many salesmen fit into the ranges of salaries between:
Code:
200-299
300-399
....
1000 and more.
Here is my code:
Code:
public class Salary
{
public static void main(String[] args)
{
double salary[]={300, 200, 600, 700, 2000, 8000, 800, 800, 900, 6000, 1100};
int frequency []= new int [11];
System.out.printf("%10s: %6s:\n","Range", "Frequency");
for (int range = 0; range < (salary.length); range++)
++frequency[(int)Math.floor(((salary[range]*(.9)+200)*.01))];
for (int range1 =2; range1<frequency.length; range1++)
System.out.printf("$%4d-%4d: %10d\n", range1*100,range1*100+9,frequency[range1]);
}
}
Error:
Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at Salary.main(Salary.java:11)
I know that if i just feed the numbers without doing the math, the application seems to work, but when i start doing the math, it stops working. I'm using the Math.
floor in order to get an integer value, which can then be pushed into the frequency array.
The frequency array should collect the values between 0 and 10 and then print them (as number of people) next to the range of salary.
Thanks
Exercise 7.20 from Java How to Program book
Okay I got it figured out the most part but running into a problem with it. When I get it to run I get the error:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2: at TotalSales.totalsales.main(totalsales.java:39):mad : I have been working on this for two weeks now and the last day to get this turned in is Saturday by noon central time. Here is my code:
package TotalSales;
import java.util.Random;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author pdanahey
*/
public class totalsales
{
static Random rand = new Random();
private static int outer;
private static int inner;
public static void main(String[] args)
{
int sales[][]=
{
{1, 2, 3, 4, },
{0, 1, 2, 3, 4, 5},
};
int counter = 0;
int innerCount[] = new int[5];
System.out.printf(" Col 1 Col 2 Col 3 Col 4 \n");
for (int outer1=0; outer <sales.length; outer++)
{
counter = 0;
System.out.print(" ");
}
for ( int inner1=0; inner1 < sales[outer].length; inner1++)
{
System.out.printf("%s ", sales[outer][inner1]);
counter += sales[outer][inner1];
innerCount[inner1] += sales[outer][inner1];
}
System.out.printf("\t%s\n", counter);
System.out.printf(" %s %s %s\n", innerCount[0], innerCount[1], innerCount[2]);
}
}
If anyone can help me ASAP I will be greatly appreciate it.
Pam
same Error occurring while running
import java.net.* ;
import java.io.* ;
import java.lang.* ;
import java.util.* ;
class proxy {
public static void main(String args[]) throws IOException{
//parse arguments from command line
int localport = -1;
int remoteport = -1;
String remotehost = null;
boolean error = false;
int i = 0;
Integer parselocalport = new Integer(args[i]);
Integer parseremoteport = new Integer(args[i+2]);
Socket incoming, outgoing = null;
ServerSocket Server = null;
try
{
localport = parselocalport.parseInt(args[i], 10);
remotehost = args[i+1];
remoteport = parseremoteport.parseInt(args[i+2], 10);
}
catch(Exception e)
{
System.err.println("Error: " + e.getMessage() + "\n");
error = true;
}
// Check for valid local and remote port, hostname not null
System.out.println("Checking: Port" + localport + " to " + remotehost + " Port " + remoteport);
if(localport <= 0){
System.err.println("Error: Invalid Local Port Specification " + "\n");
error = true;
}
if(remoteport <=0){
System.err.println("Error: Invalid Remote Port Specification " + "\n");
error = true;
}
if(remotehost == null){
System.err.println("Error: Invalid Remote Host Specification " + "\n");
error = true;
}
//If any errors so far, exit program
if(error)
System.exit(-1);
//Test and create a listening socket at proxy
try{
Server = new ServerSocket(localport);
}
catch(IOException e) {
e.printStackTrace();
}
//Loop to listen for incoming connection, and accept if there is one
while(true)
{
try{
incoming = Server.accept();
//Create the 2 threads for the incoming and outgoing traffic of proxy server
outgoing = new Socket(remotehost, remoteport);
proxyThread thread1 = new proxyThread(incoming, outgoing);
thread1.start();
proxyThread thread2 = new proxyThread(outgoing, incoming);
thread2.start();
}
catch (UnknownHostException e) {
//Test and make connection to remote host
System.err.println("Error: Unknown Host " + remotehost);
System.exit(-1);
}
catch(IOException e){
System.exit(-2);//continue;
}
/* catch (IOException e) {
System.err.println("Error: Couldn't Initiate I/O connection for " + remotehost);
System.exit(-1);
}
*/
}
}
}
Please Help.....