Simple JAVA program error , using multiple classes :

package work1; public class classyclass { public static void main(String[] args) { int num; num = 0; num++; System.out.println(" blah blah blah " + num); Coffee latte = new Coffee(); Coffee capuccino = new Coffee(); latte.price = 5; capuccino.price = 11; latte.beverage(); capuccino.beverage(); } } :

package work1; public class Coffee { int price; String coffeeType; void beverage() { if (coffeeType == "latte" ) { System.out.println("The price of latte is " + latte.price ); } else if(coffeeType == "capuccino") { System.out.println("The price of a cappuccino is " + capuccino.price); } } }

As an amateur C programmer venturing into Java ,I am getting really confused with using these classes and objects. What I want is to take the value from the classyclass class into the coffee class and then execute on that data back again in the main method. I am mixing stuff up , please help


change string comparison

coffeeType == "latte"



that is how java compares string values. Make the changes in rest of your code with respect to these changes.

There are several things wrong or not logical in your code:

1) If you only use num to print the value 1, why not change it to:

int num = 1;


System.out.println(" blah blah blah " + 1);

or even

System.out.println(" blah blah blah 1");

2) You make a new object of the type/class Coffee, but you never set the variable String coffeeType in the objects. The standard way to do this is to make a constructor like so:

in main():

Coffee latte = new Coffee("latte");

and in your Coffee class make the constructor:

public Coffee(String coffee){
this.coffeeType = coffee;

3) You have to compare Strings with the .equal() method:

if("latte".equals(this.coffeeType)){ System.out.println("\nThis is latte coffee"); }

Some further notes as mentioned by npinti: use getter and setter methods to set variables en retrieve them:

public void setType(String type){
this.coffeeType = type;

public void setPrice(int price){
this.price = price;

public int getPrice(){
return this.price;

Try this code. I have tried to use java conventions for your understanding.

Your ClassyClass class:

import java.util.*;

public class ClassyClass
ArrayList<Coffee> myClub;
Scanner myInput;
public ClassyClass()
myClub = new ArrayList<Coffee>();
myInput = new Scanner(;

myClub.add(new Coffee("latte", 5));
myClub.add(new Coffee("capuccinno", 11));

System.out.println("Do you want to place an order? Enter 1 for yes and 2 for no.");
int choice = myInput.nextInt();


if (choice == 1)
else { }

public void myCoffeeClub()
System.out.print("Enter latte or capuccinno: ");
String item =;
int itemPrice = 0;

for (Coffee s : myClub)
if (s.cName.equals(item))
itemPrice = s.cPrice;
System.out.println ("The price of "+item+" is "+itemPrice+".");

Your Coffee class:

public class Coffee
String cName;
int cPrice;

public Coffee (String cName, int cPrice)
this.cName = cName;
this.cPrice = cPrice;

You should have set coffeeType while setting price and you would have got correct results.

public class classyclass {

public static void main(String[] args) {

int num;
num = 0;
System.out.println(" blah blah blah " + num);

Coffee latte = new Coffee();
latte.coffeeType = "latte";
latte.price = 5;

Coffee capuccino = new Coffee();
capuccino.coffeeType = "capuccino";
capuccino.price = 11;


public class Coffee {

int price;
String coffeeType;

void beverage() {

if (this.coffeeType.equals("latte")) {
System.out.println("The price of latte is " + this.price);
else if (this.coffeeType.equals("capuccino")) {
System.out.println("The price of a cappuccino is " + this.price);

In java the == operator compares an object reference. To compare a string you should use the string.equals("..") method.

Category:java Time:2018-12-17 Views:1
Tags: java class object

Related post

  • simple java program connecting to database server using JDBC has execution time varying all over the place 2012-03-05

    I've got a very simple java program (, see below) on my application server that successfully connects to an Oracle 11.2 database on a database server (both servers are Linux CentOS) using JDBC thin driver from Oracle. As you can see from the se

  • when to use multiple class libraries? 2011-03-17

    When should I use multiple class libraries in .NET. I have a situation where I need to use the functionalities of Microsoft Office Object Model to check certain attributes of Microsoft Office files. Should I use different class libraries to process d

  • Calling a mapreduce job from a simple java program 2012-03-24

    I have been trying to call a mapreduce job from a simple java program in the same package.. I tried to refer the mapreduce jar file in my java program and call it using the runJar(String args[]) method by also passing the input and output paths for t

  • How do I use multiple classes in PHP 2013-12-18

    I'm very new to oop in php, so far i'm using multiple classes that I made in one php file, such as: class Style { //stuff } class User { //other stuff } And many more, yet i'm having an issue on how to connect to mysql within these classes, if I use

  • How to Run a Simple Java Program 2010-11-12

    I dad left Java since so long as a result now it happens that sometimes I forget the simple things and used to behave like a Stupid. To run a Simple Java program say "Hello World" written in Notepad what do I have to Do? I know the commands javac "Fi

  • Running a Java Program (which uses HDFS JAVA API) outside $HADOOP_HOME 2011-07-30

    I have a simple Java program which simply reads and writes some text to a file on HDFS. I run it using hadoop HDFSReadWrite text. I want to run it from eclipse or just like any other java program and still be able to use the HDFS outside the Hadoop e

  • Using multiple classes in one element and specificity 2011-12-09

    Just wondering when you use multiple classes on the one element such as class="foo bar" and those classes are setup as below: .foo { margin-right: 10px; } .bar { margin-right: 0px; } Which class will have specificity? Will the margin be 10px or 0px?

  • How to overloading constructors if using multiple classes in one file 2012-04-12

    When I try to compile this, it says "cannot find symbol constructor Armor(int,int,int,java.lang.String)". However, when I change Armor(blunt, pierce, cut, name) to Armor(), it works perfectly fine. Am I not allowed to overload constructors if I'm usi

  • how to debug/step through a simple java 'main' application using netbean? 2010-01-06

    how to debug/step through a simple java 'main' application using netbean? I want to set breakpoints and analyze the value of variables etc. --------------Solutions------------- I find it hard to believe that there's nothing helpful in the NetBeans bu

  • Compilation error using scala classes in java maven project 2011-11-08

    I have a java maven project , i installed scala plugin for eclipse and added scala nature to my project . Now , i have written some custom scala Actors and some other utility classes .I am using these scala classes in my java code . while writing the

  • Using multiple classes java 2011-04-16

    I would like to know how to use multiples clas in Java. I know how use method from other class and also constructors but i would like know how create a new "object" for example. If i am making a PersonDirectory, then i can have class called Person wh

  • Java, opinion on using Object class? 2011-09-06

    I'm wondering what the general opinion is on using Java's Object class for something like this: private Car myCar; private String myCarBrand; public void set(String variableToSet, Object valueToSet){ if(myCarBrand.equals("AUDI")){ if(varibleToSet.equ

  • Java program that uses SQL - compile into .jar 2011-10-30

    My code compiles and runs fine in Eclipse, but when I try to make it into a .jar it gives me a bunch of errors. The code, the .bat file I use to compile into .jar and my error message can be found here: import java.sql.Connection; import java.sql.Dri

  • References and variable passing. Explain the output of this simple java program? 2010-03-07

    Quick summary of what I need help with: What's an immutable class? With Java's String class, why can you not 'point' one String variable at another (using C terminology) like you can with other classes? For example, String x=(String)y; copies y's val

  • How can I change my Java program to use a database instead of an arraylist? 2011-01-20

    In my java program, I had a book class and a library class. The library stores the book object in an array list and then I display it on the screen. I can add the book and remove the books using functions. I also use AbstractJtableModel for adding an

  • Need Java Help with Combining Multiple Classes in Gui 2011-02-22

    I have prepared 2 classes which perform a function that I would like to wrap up in one nice easy to use GUI. Here is the first class: import java.util.Scanner; import java.text.*; public class inputDecimal { Scanner input = new Scanner( );

  • Can Java Runtime.exec another java program that uses stdin? 2011-07-21

    I have run into an issue where , when using Java Runtime to run another java program, the program freezes because the other program requires stdin . There is a problem with handling the stdin after executing another java program with Runtime exec() .

  • Java application that uses multiple timers 2011-09-15

    Hi all and thanks for reading this. i have an application and i want to use multiple timers on it. it will read from a list of instructions some instructions and will tell you what to do, keeping track of time for each instruction's time and notifyin

  • simple java program that enables 2 Time-format input and count the time-difference 2011-10-06

    I'd like to know how to make a simple program where user can gave 2 input time(hh:mm) format ---and receive the elapsing time as the output. the program will run and enable user to write time-input.. maybe a simple program that run through command pr

Copyright (C), All Rights Reserved.

processed in 0.680 (s). 13 q(s)