QuestionMy java code doesn't work.

Posts 13 of 3 · Page 1 of 1
My java code doesn't work.
I'm learning java but there is something not working and I don't know what it is.
Here is the code:


 
First class
package multiple_classes;

import java.util.Scanner;

public class Headclass {

public static void main(String[] args) {
Scanner x=new Scanner(System.in);
double number1, number2;
Sub subObject= new Sub();
String z;
System.out.println("What is z");
z=x.nextLine();
System.out.println("What is the first number?");
number1=x.nextDouble();
System.out.println("What is the second number?");
number2=x.nextDouble();
if(z=="+"){
subObject.plus(number1,number2);

}

}

}



 
Second class
package multiple_classes;

public class Sub {
public void plus(double number1, double number2){
double answer;
answer=number1+number2;
System.out.println(answer);
}

}




BTW why does mpgh always remove my Tabs and spaces before every sentence
The second class doesn't reference the first class at all, But not sure if that's your issue
try
Code:
using Sub;
also,
Code:
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
double number1, number2;
Sub subObject= new Sub();
makes no sense. You declared x as a input variable, And then proceed to double the number1, number 2; even though these variables done exist.
try
Code:
int number1;
number1 =  (Your number)
int number2;
number2 = (your number)
always make sure to declare your variables
Quote Originally Posted by OncePhoenix View Post
The second class doesn't reference the first class at all, But not sure if that's your issue
try
Code:
using Sub;
also,
Code:
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
double number1, number2;
Sub subObject= new Sub();
makes no sense. You declared x as a input variable, And then proceed to double the number1, number 2; even though these variables done exist.
try
Code:
int number1;
number1 =  (Your number)
int number2;
number2 = (your number)
always make sure to declare your variables
Fuck you're stupid.

  • Classes within the same package don't have to be imported, not to mention, "using _" isn't valid syntax within the Java language
  • Declaring variables before using them is entirely valid
  • This thread is a month old


It doesn't make sense because you're trying to give help for a language you know nothing about.

Even know this thread is dead, the reason his code doesn't work is because when you compare objects using the equals operator in Java, it compares the address of the underlying object (declaring an Object isn't declaring an Object, it's declaring a reference to an Object). You should use the equals method which is present in all objects due to them being a sub class of Object; which is a method that can optionally be overwritten in any class to do some kind of other comparison (e.g. comparing each character in a String).

Code:
if (z.equals("+")) {
...
}
Posts 13 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?