the_preface 0 Newbie Poster

Thank you! That helped me a lot.

the_preface 0 Newbie Poster

I'm writing a program which accepts the price of a tour. A tour is priced between 29.95 and 249.99. It's supposed to repeat until a valid price is entered... However, even when it is, the program continues to repeat. Here's what I have so far.

import java.util.Scanner;
import java.util.InputMismatchException;

public class TourPrices
{
   public static void main(String args[])
   {

   Scanner scan = new Scanner(System.in);
   int tourPrice = 0;
   boolean ok;

   do
   {
      ok = true;
      try
      {
         System.out.println("Enter a score");
         if(tourPrice < 29.95 || tourPrice >= 249.99)
	 tourPrice = scan.nextInt();
         ok = false;
      }
         catch(InputMismatchException e)
	 {
	    ok = false;
	    scan.nextLine();
         }
   }
   while(!ok);
      System.out.println("The valid price is " + tourPrice);

   }	
}

How can I modify it so it ends when a valid price is entered?

the_preface 0 Newbie Poster

I'm writing a program that reads information from three seperate classes. Here is my code:

public class Animal
{
   protected int id;
   protected String type;
   protected double mass;
 
    //------------------------------------------------------------------------
    //  Sets up an animal with the specified ID number, type and weight.
    //------------------------------------------------------------------------
 
   public Animal (int animalID, String animalType, double weight)
   {
      id = animalID;
      type = animalType;
      mass = weight;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this animal as a string.
   //------------------------------------------------------------------------
 
   public String toString()
   {
      String description = "ID: " + id + "\n";
 
      description += "Type: " + type + "\n";
      description += "Weight: " + mass + "\n"; 
 
      return description;  
   }
}
public class Pet extends Animal
{
   private String title;
   private String own;
 
   //------------------------------------------------------------------------
   //  Sets up a pet using the specified information.
   //------------------------------------------------------------------------
 
   public Pet (int animalID, String animalType, double weight, String name, String owner)
   {
      super (animalID, animalType, weight);
 
      title = name;
      own = owner;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this pet as a string.
   //------------------------------------------------------------------------
 
   public String toString()
   {
      String description = super.toString();
 
      description += "Name: " + title + "\n";
      description += "Owner: " + own + "\n"; 
 
      return description;  
   }
}
public class ZooAnimal extends Animal
{
   private int cage;
   private String train;
 
   //------------------------------------------------------------------------
   //  Sets up a zoo animal using the specified information.
   //------------------------------------------------------------------------
 
   public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
   {
      super (animalID, animalType, weight);
 
      cage = cageNumber;
      train = trainer;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this zoo …
the_preface 0 Newbie Poster

First of all the last else should be an if. Since you say that animal has to be [1000,2999] then it is possible the else to be executed even if that is not true. If ID is 100 for example. You should also have an if for the invalid IDs or put them all in else-if.

Also this will NEVER be true: (animalID <= 3000 && animalID > 7999) If ID = 4000:
animalID <= 3000: (4000<=3000) FALSE
animalID > 7999: (4000>7999) FALSE

But if you do this: (animalID >= 3000 && animalID <= 7999) If ID = 4000:
animalID <= 3000: TRUE
animalID > 7999: TRUE

Ah! Thank you very much.

the_preface 0 Newbie Poster

I'm writing a program that reads information from three seperate classes. Here is my code:

public class Animal
{
   protected int id;
   protected String type;
   protected double mass;
 
    //------------------------------------------------------------------------
    //  Sets up an animal with the specified ID number, type and weight.
    //------------------------------------------------------------------------
 
   public Animal (int animalID, String animalType, double weight)
   {
      id = animalID;
      type = animalType;
      mass = weight;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this animal as a string.
   //------------------------------------------------------------------------
 
   public String toString()
   {
      String description = "ID: " + id + "\n";
 
      description += "Type: " + type + "\n";
      description += "Weight: " + mass + "\n"; 
 
      return description;  
   }
}
public class Pet extends Animal
{
   private String title;
   private String own;
 
   //------------------------------------------------------------------------
   //  Sets up a pet using the specified information.
   //------------------------------------------------------------------------
 
   public Pet (int animalID, String animalType, double weight, String name, String owner)
   {
      super (animalID, animalType, weight);
 
      title = name;
      own = owner;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this pet as a string.
   //------------------------------------------------------------------------
 
   public String toString()
   {
      String description = super.toString();
 
      description += "Name: " + title + "\n";
      description += "Owner: " + own + "\n"; 
 
      return description;  
   }
}
public class ZooAnimal extends Animal
{
   private int cage;
   private String train;
 
   //------------------------------------------------------------------------
   //  Sets up a zoo animal using the specified information.
   //------------------------------------------------------------------------
 
   public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
   {
      super (animalID, animalType, weight);
 
      cage = cageNumber;
      train = trainer;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this zoo …
the_preface 0 Newbie Poster

I'm writing a program that reads information from three seperate classes. Here is my code:

public class Animal
{
   protected int id;
   protected String type;
   protected double mass;
	
    //------------------------------------------------------------------------
    //  Sets up an animal with the specified ID number, type and weight.
    //------------------------------------------------------------------------
	
   public Animal (int animalID, String animalType, double weight)
   {
      id = animalID;
      type = animalType;
      mass = weight;
   }

   //------------------------------------------------------------------------
   //  Returns information about this animal as a string.
   //------------------------------------------------------------------------
	
   public String toString()
   {
      String description = "ID: " + id + "\n";

      description += "Type: " + type + "\n";
      description += "Weight: " + mass + "\n"; 
		
      return description;  
   }
}
public class Pet extends Animal
{
   private String title;
   private String own;
	
   //------------------------------------------------------------------------
   //  Sets up a pet using the specified information.
   //------------------------------------------------------------------------
	
   public Pet (int animalID, String animalType, double weight, String name, String owner)
   {
      super (animalID, animalType, weight);
		
      title = name;
      own = owner;
   }
	
   //------------------------------------------------------------------------
   //  Returns information about this pet as a string.
   //------------------------------------------------------------------------
	
   public String toString()
   {
      String description = super.toString();
		
      description += "Name: " + title + "\n";
      description += "Owner: " + own + "\n"; 
		
      return description;  
   }
}
public class ZooAnimal extends Animal
{
   private int cage;
   private String train;

   //------------------------------------------------------------------------
   //  Sets up a zoo animal using the specified information.
   //------------------------------------------------------------------------
	
   public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
   {
      super (animalID, animalType, weight);
		
      cage = cageNumber;
      train = trainer;
   }

   //------------------------------------------------------------------------
   //  Returns information about this zoo …
the_preface 0 Newbie Poster

I'm writing a code segment that calculates and displays the number of holes upon which a golfer made an eagle. Which is two fewer than the par value for the hole. This is what I have come up with.

int[] par = new int[18];
      int[] strokes = new int[18];
        
      for (int i = 0; i < par.length; i++)
      {
              par[i] = (int)(Math.random() * 2) + 1; //calculates eagle

              strokes[i] = (int)(Math.random() * 5) + 1; //calculates number of strokes per hole                
      }
                
           for (int i = 0; i < strokes.length; i++)
                    
           if (par[i] > strokes[i])
              System.out.println(i + 1);

Is it correct or am I making an error somewhere?

the_preface 0 Newbie Poster

Basically I want my program to change the current price of an object with a new one, but I'm having trouble doing so. Here's my code:

//-----------------------------------------------------------------
   //  Updates the product's price.
   //-----------------------------------------------------------------
	
	public void setPrice(double newPrice)
	{
      this.newPrice = newPrice;
	}
//-----------------------------------------------------------------
   //  Changes the price of the product from the collection.
   //-----------------------------------------------------------------
		
	public void changePrice(int prodID, double newPrice)
	{
      int index = findProduct(prodID);
      if (index > -1)
         collection[index].setPrice(newPrice);
      else
         System.out.println("The product having the ID number " + prodID 
                            + " was not found.\n");	
	}
System.out.print("\nEnter the ID number of product to modify: ");
                        idNum = userInput.nextInt();
                        System.out.print("\nWhat is the new price? ");
                        price = userInput.nextDouble();
                        userInput.nextLine();
                        data.changePrice(idNum, price);

The error I get from my program is:

Product.java:70: cannot find symbol
symbol : variable newPrice
location: class Product
this.newPrice = newPrice;

Am I coding this incorrectly or is there an alternative for what I hope to acheive?

the_preface 0 Newbie Poster

As the title of the question states, my program is not able to store an integer from a seperate text file provided. For instance, instead of printing the desired output:

"$45.00 17 2222 Chuck Taylor All Star"

It prints:

"$45.00 17 0 Chuck Taylor All Star"

So even though I provided the price (double), quantity (integer) and name (string), my program can store all of it except for the blasted ID number (integer). If anyone is willing to guide me towards the light on this, I would greatly appreciate it. My code is below, and the last bit of data at the very end is what "productin.txt" contains.

import java.io.*;
import java.text.NumberFormat;

public class Product
{
   private String name;
	private double price;
	private int idNum;
	private int quantity;
	
	public Product (int id, String title, double cost, int total) 
	{
		id = idNum;
		name = title;
		price = cost;
		quantity = total;
	}

   //-----------------------------------------------------------------
   //  Returns the product's ID number.
   //-----------------------------------------------------------------

	public int getID()
	{
	   return idNum;
	}
	
	//-----------------------------------------------------------------
   //  Returns the product's name.
   //-----------------------------------------------------------------
	
	public String getName()
	{
	   return name;
	}
	
	//-----------------------------------------------------------------
   //  Returns the product's price.
   //-----------------------------------------------------------------
	
	public double getPrice()
	{
	   return price;
	}

	//-----------------------------------------------------------------
   //  Adds units to the quantity in inventory.
   //-----------------------------------------------------------------

	public void addQuantity(int units)
   {
      quantity += units;
   }

	//-----------------------------------------------------------------
   //  Returns the quantity in inventory.
   //-----------------------------------------------------------------

	
	public int getQuantity()
	{
	   return quantity;
	}
	
	//-----------------------------------------------------------------
   //  Subtracts units to the quantity in inventory.
   //-----------------------------------------------------------------
	
	public void subQuantity(int units)
   {
      quantity …
the_preface 0 Newbie Poster

I have two problems with my code. One being I can't get it to repeat the simple game until the user has no money in his or her bankroll, the game continues even when the bankroll equals 0. The other is I can't get my program to repeat asking the user how much he or she wishes to bet, even if his or her bankroll or bet is too low. Any suggestions?

import java.util.Random;
 
public class PlayingCard
{
   private int face;
   private int suit;
 
   //-------------------------------------------------------------------
   //  Sets up the playing card by drawing it initially.
   //-------------------------------------------------------------------
   public PlayingCard ()
   {
      drawCard();
   }
 
   //-------------------------------------------------------------------
   //  Draws the playing card by randomly choosing face and suit values.
   //-------------------------------------------------------------------
   public void drawCard ()
   {
      face = (int) (Math.random() * 13) + 2;
      suit = (int) (Math.random() * 4);
   }
 
   //-------------------------------------------------------------------
   //  Returns true if the current playing card is exactly the same as
   //  the card passed in as a parameter.
   //-------------------------------------------------------------------
   public boolean isEquals (PlayingCard c)
   {
      return (face == c.face && suit == c.suit);
   }
 
   //-------------------------------------------------------------------
   //  Returns the face value of the current playing card.
   //-------------------------------------------------------------------
   public int getFace()
   {  
      return face;
   }
 
   //-------------------------------------------------------------------
   //  Returns the suit value of the current playing card.
   //-------------------------------------------------------------------
   public int getSuit()
   {  
      return suit;
   }
 
   //-------------------------------------------------------------------
   //  Returns the current playing card as a string.
   //-------------------------------------------------------------------
   public String toString()
   {
      String cardName = null;
 
      switch (face)
      {
         case 2:   cardName = "Two";
                   break;
         case 3:   cardName = "Three";
                   break;
         case 4:   cardName …
the_preface 0 Newbie Poster
System.out.print("\n\nDo you think the second card will be higher or lower?");
      String choiceString = scan.nextLine(); // Asks input from user //
 
      choiceString = choiceString.toLowerCase();
 
      PlayingCard cardOne = new PlayingCard(); // Creates new object called cardOne //
      PlayingCard cardTwo = new PlayingCard(); // Creates new objects called cardTwo //

// The face value of first card is less than the face value of the second card and is equal to the input "higher" //

                          if (cardOne.getface() < cardTwo.getTwo.getface() && choiceString.equals("higher"))

                               do
                               {
                                    cardTwo.drawCard(); // Draws a random playing card //
                               }
                               while (cardOne.isEquals(cardTwo));
 
                if (choiceString > 0)
                     if (choiceString == cardTwo) // Input is equal to second card drawn //
                     {    

                          // If the if statement is true, the program will print the following statement //

                          System.out.println("Good job! You're right!");

                               bankroll = bankroll + bet; // Adds the money bet to the user's bankroll //
                               wonGames++;  // Adds 1 to integer wonGames //
                               totalGames++;  // Adds 1 to integer totalGames //
                     }

You may have to eye my first code for reference on what I'm doing. If that still confuses you, I can post the requirements of my program.

the_preface 0 Newbie Poster

Ah! Thank you! I accidently placed an operator in front of the oneCard object, which gave me a list of errors when I attempted to compile it. Might I add on one last question?

System.out.print("\n\nDo you think the second card will be higher or lower?");
      String choiceString = scan.nextLine();
 
      choiceString = choiceString.toLowerCase();
 
      PlayingCard cardOne = new PlayingCard();
      PlayingCard cardTwo = new PlayingCard();
 
                          if (cardOne.getface() < cardTwo.getTwo.getface() && choiceString.equals("higher"))
                               do
                               {
                                    cardTwo.drawCard();
                               }
                               while (cardOne.isEquals(cardTwo));
 
                if (choiceString > 0)
                     if (choiceString == cardTwo)
                     {    
                          System.out.println("Good job! You're right!");
                               bankroll = bankroll + bet;
                               wonGames++;
                               totalGames++;
                     }

How can I have my code read the choices 'Higher' or 'Lower' from the input provided by the user? Just so it can count whether or not the user won or lost the bet.

the_preface 0 Newbie Poster

I'm relatively new to Java and I'm writing a code that implements a simple "Hi-Lo" game using a standard deck of 52 playing cards. I could give you the entire description of the program's requirements, but I won't let you all suffer the same headache I do. :P Anyhow, here's my two chunks of code:

import java.util.Random;

public class PlayingCard
{
   private int face;
   private int suit;

   //-------------------------------------------------------------------
   //  Sets up the playing card by drawing it initially.
   //-------------------------------------------------------------------
   public PlayingCard ()
   {
      drawCard();
   }

   //-------------------------------------------------------------------
   //  Draws the playing card by randomly choosing face and suit values.
   //-------------------------------------------------------------------
   public void drawCard ()
   {
      face = (int) (Math.random() * 13) + 2;
      suit = (int) (Math.random() * 4);
   }

   //-------------------------------------------------------------------
   //  Returns true if the current playing card is exactly the same as
   //  the card passed in as a parameter.
   //-------------------------------------------------------------------
   public boolean isEquals (PlayingCard c)
   {
      return (face == c.face && suit == c.suit);
   }

   //-------------------------------------------------------------------
   //  Returns the face value of the current playing card.
   //-------------------------------------------------------------------
   public int getFace()
   {  
      return face;
   }

   //-------------------------------------------------------------------
   //  Returns the suit value of the current playing card.
   //-------------------------------------------------------------------
   public int getSuit()
   {  
      return suit;
   }

   //-------------------------------------------------------------------
   //  Returns the current playing card as a string.
   //-------------------------------------------------------------------
   public String toString()
   {
      String cardName = null;

      switch (face)
      {
         case 2:   cardName = "Two";
                   break;
         case 3:   cardName = "Three";
                   break;
         case 4:   cardName = "Four";
                   break;
         case 5:   cardName = "Five";
                   break;
         case 6:   cardName = "Six";
                   break;
         case 7: …