I
IanH
Hi
I want to store values received from the user in array and then to
output the values on the screen. I'm new to java and am having
problems setting up the array and assigning the values to the array.
Here is my code so far not sure where to go with it. My code is working
at the moment but the values i type into the input boxes don't appear
on the screen.
Also, i have a method that calculates the daily parking fee - this
works fine.
import java.awt.Container;
import javax.swing.*;
public class PassArray extends JApplet {
// initialize applet
public void init()
{
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add( outputArea );
String name;
String address;
String creditCardNumber;
String hours;
int noOfHours;
String array[]; //declared array
array = new String[ 5 ]; //create array
String output = "Customer
Name\tAddress\tCreditCardNumber\tHours\tFee\n";
name = JOptionPane.showInputDialog("Enter your name");
address = JOptionPane.showInputDialog("Enter your address");
creditCardNumber = JOptionPane.showInputDialog("Enter your credit card
number");
hours = JOptionPane.showInputDialog("Enter total hours");
do {
for (int num = 0; num <= array.length; num++)
output += " " + array[num] + name + address + creditCardNumber +
hours;
//array[num] = name + address + creditCardNumber + hours;
} while ( name != "" );
// convert numberOfHours to NoOfHours
noOfHours = Integer.parseInt( hours );
double charges = calculateCharges(noOfHours);
// append original array elements to String output
for ( int counter = 0; counter < array.length; counter++ )
output += name + "\t" + "\t" + address + "\t" + creditCardNumber
+ "\t" + "\t" + hours + "\t" + charges + "\n";
outputArea.setText( output );
} // end method init
// multiply each element of an array by 2
public double calculateCharges(int hoursParked) {
// public void calculateCharges( int fee){
double fee = 0.00; //initialize the output of the method
int NoOfHours = 0;
if ( hoursParked <= 3 )
fee = 2.00;
else if ( hoursParked > 3)
fee = (hoursParked - 3 ) * .5 + 2.00;
else if ( hoursParked >=24 )
fee = 10.00;
return fee;
}
} // end class PassArray
I want to store values received from the user in array and then to
output the values on the screen. I'm new to java and am having
problems setting up the array and assigning the values to the array.
Here is my code so far not sure where to go with it. My code is working
at the moment but the values i type into the input boxes don't appear
on the screen.
Also, i have a method that calculates the daily parking fee - this
works fine.
import java.awt.Container;
import javax.swing.*;
public class PassArray extends JApplet {
// initialize applet
public void init()
{
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add( outputArea );
String name;
String address;
String creditCardNumber;
String hours;
int noOfHours;
String array[]; //declared array
array = new String[ 5 ]; //create array
String output = "Customer
Name\tAddress\tCreditCardNumber\tHours\tFee\n";
name = JOptionPane.showInputDialog("Enter your name");
address = JOptionPane.showInputDialog("Enter your address");
creditCardNumber = JOptionPane.showInputDialog("Enter your credit card
number");
hours = JOptionPane.showInputDialog("Enter total hours");
do {
for (int num = 0; num <= array.length; num++)
output += " " + array[num] + name + address + creditCardNumber +
hours;
//array[num] = name + address + creditCardNumber + hours;
} while ( name != "" );
// convert numberOfHours to NoOfHours
noOfHours = Integer.parseInt( hours );
double charges = calculateCharges(noOfHours);
// append original array elements to String output
for ( int counter = 0; counter < array.length; counter++ )
output += name + "\t" + "\t" + address + "\t" + creditCardNumber
+ "\t" + "\t" + hours + "\t" + charges + "\n";
outputArea.setText( output );
} // end method init
// multiply each element of an array by 2
public double calculateCharges(int hoursParked) {
// public void calculateCharges( int fee){
double fee = 0.00; //initialize the output of the method
int NoOfHours = 0;
if ( hoursParked <= 3 )
fee = 2.00;
else if ( hoursParked > 3)
fee = (hoursParked - 3 ) * .5 + 2.00;
else if ( hoursParked >=24 )
fee = 10.00;
return fee;
}
} // end class PassArray