-2

I'm working on a project for school and I've run into a wall where I need to get a user inputted Int and double from my main method to a required non-static method. this non-static method is supposed to be used for the calculation and printing of the answer based on the price (double) and the amount paid (int).

public static void main (String[] args){
   Scanner check = new Scanner(System.in);// import of the scanner
   char cents = '\u00a2';//Unicode of cent symbol
   System.out.print("Your item costs (25" + cents + " minimum, Increments of 5" + cents + "): ");
   Double price = check.nextDouble(); //price of the item
   System.out.println("You paid (whole dollars only): ");
   int paid = check.nextInt(); //amount paid
   VendingChange newVend = new VendingChange();//creates a copy of the class 
   newVend.Secondary();// calls the nonstatic

that's the main method

public void Secondary () {
   System.out.println("your change is " +/*this is where the equation is supposed to go*/ );

and this is the non-static I've tried adding an extra static method, which eliminated the errors, but the int and double still wouldn't go through. meaning I can't use the int and double in any other method, because it doesn't recognize the names.

Sigma
  • 1
  • 1
  • 4
    I recommend taking a structured approach when learning a new language by e.g. reading a tutorial. For java, there is e.g. [this one from `oracle.com`](https://docs.oracle.com/javase/tutorial/). – Turing85 Jan 04 '22 at 23:21
  • Could you clarify what you mean by the int/double not "going through?" – lawgik Jan 04 '22 at 23:26
  • 2
    For this specific question you may want to look into the [difference between static and instance methods](https://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods) – Ewan Brown Jan 04 '22 at 23:28
  • By the way, why would payment be `int` (whole number) if the price is `double` (fractional number)? – Basil Bourque Jan 05 '22 at 03:49

1 Answers1

1

try replacing

Double price = check.nextDouble();

with

double price = check.nextDouble();

or even

String price1 = check.nextLine(); // gets input in form of string
double price = Double.parseInt(price1); // converts to double

Similarly, integers can also be changed:

int paid = check.nextInt();

to

String paid1 = check.nextLine();
int paid = Integer.parseInt(paid1);

I would also recommend reading (as mentioned in the comments) tutorials and really understanding the difference with static and non-static methods (instance).

(uppercase represents the class, while lowercase represents an instance)

Let's say we have a Person class. A static method would be like

Person.getPopulation(). That would return the population. This method is not instance (person) specific, rather it applies to the type of thing a person is.

An instance method would be better when you have something like person.changeName(). This would change the name of a person. An instance of a person. However, if you create it static, like Person.changeName(), which person's name would you be changing?

Think of the class Person like a type of object. People are objects (in this analogy). Person class is a type. Every person instance is every person that lives (in the program).

This same 'type of thing' vs actual 'thing' that exists under that type can be applied to a lot of things in programming, and is the basis of Object Oriented Programming (OOP).

  • My problem is that there is something that needs to stay in the main method, and I need to use it in my other method. – Sigma Jan 05 '22 at 00:28
  • @Sigma You need to learn about passing parameters to a method. As others said, you need to study your course textbook and read tutorials and examples. This kind of Java 101 question, without a specific technical focus, is not appropriate to Stack Overflow. – Basil Bourque Jan 05 '22 at 03:52
  • @Basil Bourque , there is no text book. – Sigma Jan 06 '22 at 13:13
  • Oracle provides a vast set of tutorials, free of cost: [*Passing Information to a Method or a Constructor*](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – Basil Bourque Jan 06 '22 at 16:17