Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,715 people online right now. Registration is fast and FREE... Join Now!




2 problem about java and java applet

 
Reply to this topicStart new topic

2 problem about java and java applet, a java.lang.ArrayIndexOutOfBoundsException error

yy885
17 Apr, 2007 - 02:53 AM
Post #1

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 5


My Contributions
Q.
A parking garage charges a 5euro minimum fee to park a car for up to three hours. The garage also charges an additional 2euro for each hour or part in excess of 3 hours. The maximum charge for any 24 hour period is 30euro.


A.
CODE
public class ParkingCharges{
  public static void main(String[] args){
    int hours,fee;
                hours = java.lang.Integer.parseInt(args[20]);
    
    if(hours > 24){
      fee = 300;
    }else if(hours >= 4){
      fee = (hours - 3 ) * 20 + 50;
    }else if(hours > 0){
      fee = 50;
    }else{
      fee = 0;
    }
    
    System.out.println("Hours of parking: " + args[24] + " Hours");
    System.out.println("Cost: " + fee + " euro");
  }
}


It said process completed.
QUOTE

--------------------Configuration: <Default>--------------------

Process completed.



But when i try to run it,it said:
QUOTE

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 24
at ParkingCharges.main(ParkingCharges.java:4)
Press any key to continue...


i am new for java,i try to work it out in the past few days
can someone please help?

........................................................................................

i try to write a applet like this...
IPB Image

but i realy dont have any idea how to do it,i tried my best but its still not working

CODE

<HTML>
<TITLE>ParkingCharges! Applet</TITLE>
<APPLET
CODE="ParkingCharges.class"
WIDTH=200
HEIGHT=100>
</APPLET>
</HTML>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ParkingCharges extends JApplet{

  public static void main(String[] args){
  
    int hours=0,fee=0;
    hours = java.lang.Integer.parseInt(args[20]);
    System.out.println("Entre hours");    
    if(hours > 24){
      fee = 300;
    }else if(hours >= 4){
      fee = (hours - 3 ) * 20 + 50;
    }else if(hours > 0){
      fee = 50;
    }else{
      fee = 0;
    }

    JLabel prompt1;
    JTextField input1;
    JButton check;
        
    Container c = getContentPane();
    c.setLayout(new FlowLayout());
                
    prompt1 = new JLabel("hours");
    input1 = new JTextField(5);
    c.add(prompt1);
    c.add(input1);
                        
    check = new JButton("Process Fee");
    c.add(check);
        

  }
}        



can someone please point out where is my mistake,many thanks

This post has been edited by yy885: 17 Apr, 2007 - 02:58 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: 2 Problem About Java And Java Applet
17 Apr, 2007 - 04:05 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,348



Thanked: 51 times
Dream Kudos: 25
My Contributions
Is there an args[24]? does the args array contain that many elements?
User is online!Profile CardPM
+Quote Post

PennyBoki
RE: 2 Problem About Java And Java Applet
17 Apr, 2007 - 04:08 AM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, maybe you don't need the main function.
Since you are new to JAVA.
Tutorial about Applets.
Also you should check Exceptions.
User is offlineProfile CardPM
+Quote Post

yy885
RE: 2 Problem About Java And Java Applet
20 Apr, 2007 - 06:39 AM
Post #4

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 5


My Contributions
Thanks guys
i slove the frist problem..



This post has been edited by yy885: 20 Apr, 2007 - 10:58 AM
User is offlineProfile CardPM
+Quote Post

yy885
RE: 2 Problem About Java And Java Applet
20 Apr, 2007 - 11:01 AM
Post #5

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 5


My Contributions
after i execute...
IPB Image

i found out that i forgot to put on the price box...

now i have 2 more question

how can i move the position of "Cost" to " X " like the image below?
and what code do i need for the price box?
IPB Image
User is offlineProfile CardPM
+Quote Post

Jayman
RE: 2 Problem About Java And Java Applet
20 Apr, 2007 - 11:41 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You can use a layout manager to position objects in your applet where you want them.

See the following link for a tutorial on layout managers.
http://java.sun.com/docs/books/tutorial/ui...yout/using.html
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month