Results 1 to 8 of 8
  1. #1
    bdmchamp is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Unhappy Applet Wont Show Supposed Output, Please Help!!!!

    Hi I've been trying to write an applet that suppose to show a price for a user input of guest at a event. It should have 2 int arrays( one for the prices and one for the users input of the guest). I having horrific trouble, I've been trying for 3 days now to complete this thing, but i cant! I'm not asking for the answer, but a step in the rite direction would be greatly, greatly appreciated. I'm sure its prolly an easier way to write this, but i'm new to java and i like it and everything but i'm getting really frustrated as i dont know how to produce my wanted results. Please Help
    here is the code that i have so far:




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


    public class JPartyPlanner extends JApplet implements ActionListener
    {



    JLabel greeting1 = new JLabel("Give A Hand");
    JLabel greeting2 = new JLabel("Event Planning");
    JLabel question = new JLabel("How many people will be attending?");
    JLabel output = new JLabel(" ");
    JTextField answer = new JTextField(6);
    JButton calperson = new JButton("Calculate per person");
    JButton calcost = new JButton("Calculate total cost");
    Container con = getContentPane();
    Font font1 = new Font("Times New Roman", Font.ITALIC, 60);
    Font font2 = new Font("Ariel", Font.BOLD, 30);
    Font font3 = new Font("Teen", Font.BOLD, 15);


    public void init()
    {


    question.setFont(font3);
    greeting1.setFont(font1);
    greeting2.setFont(font1);
    con.setLayout(new FlowLayout());
    con.setBackground(Color.GREEN);
    con.add(greeting1);
    con.add(greeting2);
    con.add(question);
    con.add(answer);
    con.add(calperson);
    con.add(calcost);
    calperson.addActionListener(this);
    calcost.addActionListener(this);


    }



    public void actionPerformed(ActionEvent e)
    {
    Object source = e.getSource();
    int x;
    int[] numberPeople = {24, 49, 99, 199, 499, 999, 1000};
    double[] costPerson = {27.00, 25.00, 22.00, 19.00, 17.00, 14.00, 11.00};
    String[] numberPeople1 = new String[7];
    final int ITEMS = 7;
    String costPerson1;






    if(source == calperson)

    {con.add(greeting1);
    con.add(greeting2);
    con.add(question);
    con.add(answer);
    con.add(calperson);
    con.add(calcost);
    con.add(output);
    con.setBackground(Color.RED);
    con.setLayout(new FlowLayout());
    greeting1.setFont(font1);
    greeting2.setFont(font1);
    output.setText(" ");
    output.setFont(font2);
    answer.setText(" ");

    for(x = 0; x<ITEMS; ++x)
    numberPeople1[x] = Integer.toString(numberPeople[x]);

    if( answer.getText() == numberPeople1[x])
    {costPerson1 = Double.toString(costPerson[x]);
    output.setText(costPerson1);}






    validate();}
    }

    public static void main(String[] args)
    {
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,407
    Rep Power
    25

    Default

    i dont know how to produce my wanted results
    Can you explain what the program does and how that is different from what you want it to do.

    When posting code, please wrap it in code tags. See:BB Code List - Java Forums

    First problem is that you did post all of your code. This code does not compile.
    Last edited by Norm; 06-26-2011 at 09:51 PM. Reason: Comment on: Need all of code

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Original poster, please abide by the rules that you agreed to on joining the forum and post your question once only; otherwise you needlessly split the discussion of your question which is inconsiderate to the folks helping you. I've locked your duplicate post.

  4. #4
    bdmchamp is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    I'm sorry, I'm new to this forum. To answer your question, when i run the applet everythings is there and correct. The problem is when i enter a number and press the "calculate per person" button, the price thats suppose to show doesnt, however, everythings else happens as its suppose when i press the button. The background color changes, Text field resets, etc... Just doesnt show the suppose output. So I'm guessing maybe the "For" or "If" loops are wrong? idk or maybe the part where I tried to changed the double arrays to Int arrays, But its compiling so i'm totally confused

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,407
    Rep Power
    25

    Default

    Just doesnt show the suppose output.
    Try debugging your code.
    To check where the execution flow is going, add some printlns to the code that show the values of variables as then are computed as well as show where the code is being executed.

    You have some bad coding techniques:
    Its better if there is no code on the line following a {
    The ending } should be on a line by itself and in column with the statement with the leading }

    You have buried {s and }s in the code and make it hard to follow the logic.

    Remember to add code tags. See:BB Code List - Java Forums

  6. #6
    bdmchamp is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    Once again sorry for the sloppy code, I have so much to learn, but trying ok let me try to explaini alittle better
    This part of the code is where the problem is, or so i think.
    Java Code:
     
    for(x = 0; x<ITEMS; ++x)
    numberPeople1[x] = Integer.toString(numberPeople[x]);
    
    if( answer.getText() == numberPeople1[x])
    {costPerson1 = Double.toString(costPerson[x]);
    output.setText(costPerson1);}
    with the "for" statement
    Java Code:
    for(x = 0; x<ITEMS; ++x)
    numberPeople1[x] = Integer.toString(numberPeople[x]);
    i was trying to make a String array from the Int array. Same values but convert from Int to String so i could compare the users input(String) with the "numberPeople" array(Int).
    Here's how i declared the String array.
    Java Code:
    String[] numberPeople1 = new String[7];
    Its seems wrong, but i dont know another way to do it. In this "If" statement
    Java Code:
    if( answer.getText() == numberPeople1[x])
    {costPerson1 = Double.toString(costPerson[x]);
    output.setText(costPerson1);}
    I wanted the program to search the String array that the "For" statement made and compare the user input number with the numbers in the array, and if found, i wanted the "costPerson" array, which is parallel with the "numberPeople" array,
    Java Code:
    int[] numberPeople = {24, 49, 99, 199, 499, 999, 1000};
    double[] costPerson = {27.00, 25.00, 22.00, 19.00, 17.00, 14.00, 11.00};
    to convert the correct Price from double to string so it can be displayed
    Java Code:
    output.setText(costPerson1);
    I'm pretty sure i declared all the variables used for the action right.
    Java Code:
    Object source = e.getSource();
    int x;
    int[] numberPeople = {24, 49, 99, 199, 499, 999, 1000};
    double[] costPerson = {27.00, 25.00, 22.00, 19.00, 17.00, 14.00, 11.00};
    String[] numberPeople1 = new String[7];
    final int ITEMS = 7;
    String costPerson1;
    I have no clue. The output(price) wont show.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,407
    Rep Power
    25

    Default

    Have you tried this yet?
    Try debugging your code.
    To check where the execution flow is going, add some printlns to the code that show the values of variables as they are computed as well as show where the code is being executed.

    What was printed on the Java console?

    Java Code:
    {costPerson1 = Double.toString(costPerson[x]);
    This is a hidden {
    move the code to the next line.

    Why are you posting comments about what the code is supposed to do here on the thread?
    When I read your code, the comments should be there so I can see them and the code at the same time. Not have to jump from the code to here and back.
    Last edited by Norm; 06-26-2011 at 11:44 PM.

  8. #8
    bdmchamp is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Graphics wont show up(paintComponent)
    By TheBreadCat in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 06:00 PM
  2. wont show entire JLabel
    By Billaguana in forum AWT / Swing
    Replies: 6
    Last Post: 02-03-2011, 10:18 PM
  3. output to applet
    By shivi24 in forum New To Java
    Replies: 6
    Last Post: 06-11-2010, 08:23 PM
  4. Formatting this output to show "#.##"
    By twiggy62 in forum New To Java
    Replies: 9
    Last Post: 02-06-2010, 05:40 PM
  5. Help interpreting what a class is supposed to do
    By bornwithnoname in forum New To Java
    Replies: 2
    Last Post: 11-20-2008, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •