Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Code bugs

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code bugs

    I'm new to Java, and I'm trying to write a program using BlueJ for a school assignment. I get several errors when I try to compile the source code. Please help!

    public double showPrice(String "Child", String "Adult")
    {
    if (showPrice == "Child")
    {
    return priceChild;
    }
    else if (showPrice == "Adult")
    {
    return priceAdult;
    }
    }
    What is wrong? (priceChild and priceAdult is fields declared before the method)


  2. #2
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code bugs

    public double showPrice(String "Child", String "Adult")
    {
    if (showPrice == "Child")
    {
    return priceChild;
    }
    else if (showPrice == "Adult")
    {
    return priceAdult;
    }
    }

    i think you wrote wrong code...

    why you put double quote in your parameter???
    and...in your return must bring same type with your methode data type. like....if your methode is double , you must bring double in your return too

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Code bugs

    There are a few things wrong with your code:
    1) You do not use quotes unless your are creating a String (like raizalnapi said). Although you are, in theory, attempting to create a String, you are just creating the String Object in your parameters. So, instead of this:
    public double showPrice(String "Child", String "Adult")
    public double showPrice(String Child, String Adult)
    You want:

    2) You want to return the type declared in the method (like raizalnapi said). Since you declared your method to return a double with the following code:
    public double showPrice(String "Child", String "Adult")
    you must return a double. I do not know if priceChild and/or priceAdult are doubles or not, you did not specify in your question.

    3) You should never compare String with the double equals (==). Really, the only thing you want to compare with the double equals is number types. Instead, for Strings and other Objects, you want to use the .equals() method. For Strings, there are two useful .equals() methods. The first is the .equals(String) method that returns a boolean if the two Strings have the same value (case sensitive). The second is the .equalsIgnoreCase(String) method that returns a boolean if the two String have the same value (not case sensitive).
    So instead of:
    else if (showPrice == "Adult")
    You would want to say:
    else if (showPrice.equals("Adult"))


    These are the things wrong with your code based on what you have provided. There are clearly large chunks of your code that you havent provided, and without providing us with those, we really cant help you out too much.

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code bugs

    Quote Originally Posted by raizalnapi View Post
    public double showPrice(String "Child", String "Adult")
    {
    if (showPrice == "Child")
    {
    return priceChild;
    }
    else if (showPrice == "Adult")
    {
    return priceAdult;
    }
    }

    i think you wrote wrong code...

    why you put double quote in your parameter???
    and...in your return must bring same type with your methode data type. like....if your methode is double , you must bring double in your return too
    The reason why I double quote is that I use the datatype String as parameter. The fields priceChild and priceAdult is both double, so thats not the problem.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code bugs

    Quote Originally Posted by aussiemcgr View Post
    There are a few things wrong with your code:
    1) You do not use quotes unless your are creating a String (like raizalnapi said). Although you are, in theory, attempting to create a String, you are just creating the String Object in your parameters. So, instead of this:
    public double showPrice(String "Child", String "Adult")
    public double showPrice(String Child, String Adult)
    You want:

    2) You want to return the type declared in the method (like raizalnapi said). Since you declared your method to return a double with the following code:
    public double showPrice(String "Child", String "Adult")
    you must return a double. I do not know if priceChild and/or priceAdult are doubles or not, you did not specify in your question.

    3) You should never compare String with the double equals (==). Really, the only thing you want to compare with the double equals is number types. Instead, for Strings and other Objects, you want to use the .equals() method. For Strings, there are two useful .equals() methods. The first is the .equals(String) method that returns a boolean if the two Strings have the same value (case sensitive). The second is the .equalsIgnoreCase(String) method that returns a boolean if the two String have the same value (not case sensitive).
    So instead of:
    else if (showPrice == "Adult")
    You would want to say:
    else if (showPrice.equals("Adult"))


    These are the things wrong with your code based on what you have provided. There are clearly large chunks of your code that you havent provided, and without providing us with those, we really cant help you out too much.
    I have created two classes, TicketMachine and User. The TicketMachine is supposed to print a ticket with adult tickets and child tickets, amount of money inserted (method), total income, balance etc. The User class is a user-friendly class with options to buy tickets and adjust ticketprices. Her is the java source code for the two classes:

    import java.lang.*;
    import java.util.*;
    /**
    *
    */
    public class TicketMachine
    {
    private double priceAdult;
    private double priceChild;
    private double balance;
    private double totalIncome;

    public TicketMachine()
    {
    priceChild = 0;
    priceAdult = 0;
    balance = 0;
    totalIncome = 0;
    }
    public double getOrderPrice(String Child, String Adult)
    {
    if (getOrderPrice.equalsIgnoreCase("Child"))
    {
    return priceChild;
    }
    else if (getOrderPrice.equalsIgnoreCase("Adult"))
    {
    return priceAdult;
    }
    }
    /*public double showPriceChild()
    {
    return priceChild;
    }
    public double showPriceAdult()
    {
    return priceAdult;
    }*/
    public double showBalance()
    {
    return balance;
    }
    public void insertMoney(double amount)
    {
    if(amount > 0)
    {
    balance = balance + amount;
    }
    else
    {
    System.out.println("Du kan berre sette inn eit positivt beløp: " + amount);
    }
    }
    public void adjustPrice(double child, double adult)
    {
    this.priceChild = child;
    this.priceAdult = adult;
    }
    public void buyTicket(int noChild, int noAdult)
    {
    double orderPrice = (noChild * priceChild) + (noAdult * priceAdult);
    double refund = balance - orderPrice;

    }
    public void showTickets()
    {
    if(balance >= orderPrice)
    {
    System.out.println("##################");
    System.out.println("# Billettautomat");
    //System.out.println("# Antall barn: " + noChild + "Antall voksne: " + noAdult);
    System.out.println("# Total pris på billettene er: " + orderPrice + "kroner");
    System.out.println("# Du har betalt: " + balance + "kroner");
    System.out.println("# Pengar tilbake: " + (balance - orderPrice) + "kroner");
    System.out.println("# Ha en fortsatt god dag og velkommen tilbake! ");
    System.out.println("##################");
    System.out.println();

    totalIncome = totalIncome + orderPrice;
    balance = balance - orderPrice;
    }
    else
    {
    System.out.println("Du må sette inn: " + (orderPrice - balance)
    + " fleire kroner!");
    }
    }
    public double refundBalance()
    {
    double amountToRefund;
    amountToRefund = balance;
    balance = 0;
    return amountToRefund;
    }

    }


    import javax.swing.JOptionPane;
    import java.lang.*;
    import java.util.*;

    public class User
    {
    private TicketMachine ticketMachine;

    public User()
    {
    ticketMachine = new TicketMachine();
    }

    public void adjustPrices()
    {
    String s = JOptionPane.showInputDialog("Skriv inn ny pris i kroner");
    int price = Integer.parseInt(s);
    }
    public void buyTicket()
    {
    String s1 = JOptionPane.showInputDialog("Antall barn: ");
    String s2 = JOptionPane.showInputDialog("Antall voksne: ");

    int child = Integer.parseInt(s1);
    int adult = Integer.parseInt(s2);

    double price = ticketMachine.getOrderPrice(child , adult);
    string s3 = JOptionPane.showInputDialog("Prisen for " + child + "barn og " + adult + " voksne er: " + price + "kroner" +
    "legg på beløp i kroner");

    double amountInserted = Double.parseDouble(s3);
    while (amountInserted < price)
    {
    String s4 = JOptionPane.showInputDialog("Innbetalt beløp er for lite, mangler " + (price - amountInserted) + " kroner" +
    "legg på beløp som mangler");
    amountInserted = amountInserted + Double.parseDouble(s4);
    }

    double refund = ticketMachine.buyTickets(child, adult, amountInserted);
    JOptionPane.showMessageDialog(null,"Kvittering for \n + child + barn \n" +
    price + "kroner ");
    }
    }
    Last edited by heptix; September 30th, 2010 at 03:45 AM.

Similar Threads

  1. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM