hi , i have this problem, which i cannot figure it out. This problem has been day now,and reached does not seem to help. How do u pass an integer value form one class to the other class. Here my code. The integer that summited by the user which is stored in the "amt" and amt is converted to integer, that is amount. Then the amount would be call be the class counter, so that is will be calculated until it reach zero. Also i am beginner in Java.ty

import javax.swing.*;

class input
{
int amount, num;
String amt;
public void userInput ()

{


JFrame frame = new JFrame();
amt = JOptionPane.showInputDialog(frame, "How much would u like?");
num= Integer.parseInt(amt);
amount = num;

counter obj = new counter();
obj.numberCounter();

}

}
class counter extends input
{

public void numberCounter ()
{
input count = new input();


int soda=10;
if (soda==0){
System.out.println("Out of soft drink");
}
else if (count.amount >1 && count.amount <10 ){
while (count.amount >1 && count.amount <10 )
{
input objj = new input();
objj.userInput();
}

}else {
soda=soda - count.amount;

}
}
}

class Test
{
public static void main (String args[]){
System.out.println("Hi");

input objj = new input();
objj.userInput();


}
}

I've no clue how "u" would do it. I do know how I would do it and that's described in every beginner's tutorial so go read one.

Your problem seems to be one of understanding how object oriented programming works and how conditionals work.

I found a few mistakes in your code as I was going through it. When picking up a new programming language I feel it's important to also pick up coding conventions such as class and method naming conventions.

This is the current flow of your program:
Lines 43-44: You create a new Input class instance, the you call userInput.
Lines 7-10: An input is requested from the user, then parsed.
Lines 12-13: A new instance of Counter is created, and numberCounter is invoked.
Line 20: Yet another Input class is instantiated with no connection to the first instance (this is where the problem lies).

class Input {
    
    int amount, num;
    String amt;
    
    public void userInput() {
        final JFrame frame = new JFrame();
        amt = JOptionPane.showInputDialog(frame, "How much would u like?");
        num = Integer.parseInt(amt);
        amount = num;
        
        final Counter obj = new Counter();
        obj.numberCounter();
    }
}

class Counter extends Input {
    
    public void numberCounter() {
        final Input count = new Input();
        
        int soda = 10;
        if (soda == 0) {
            System.out.println("Out of soft drink");
        } else if (count.amount > 1 && count.amount < 10) {
            
            while (count.amount > 1 && count.amount < 10) {
                final Input objj = new Input();
                objj.getUserInput();
            }
        } else {
            soda = soda - count.amount;  
        }
    }
}

class Test {
    
    public static void main(final String... args) {

        System.out.println("Hi");
        
        final Input objj = new Input();
        objj.userInput();
    }
}

I assume that this is for course work, so I won't provide a solution for you. I will say this, you have two classes too many. If you simplify the code using only one class, I am sure you will nail it right away.

Good luck!

This article has been dead for over six months. Start a new discussion instead.