• Post Reply Bookmark Topic Watch Topic
  • New Topic

if - else - else if statement error  RSS feed

 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Experts,

I am a beginner to Java Programming. And my requirement is to write a Java Program to add two numbers

1)using constructor, Methods and Objects and to show the result from Method side

2) to show the result using Driver class

i wrote a sample program and it is throwing an error. Your help is highly appreciated.







Thanks in advance.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't need/want the empty constructor. Rearrange where you call the other constructor in your main() and pass in the two arguments.
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Rearrange where you call the other constructor in your main() and pass in the two arguments. How to do this ?
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that you use the constructor with A/B parameters, this needs some reworking as it will overwrite your current A/B values.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post. Always use the tags: doesn't it look better But you can see your indentation is inconsistent.
What CB means about noȁarguments constructors is that you don't want to set both values to 0. Delete that constructor altogether. Also get rid of the assignment to 0.0 in lines 7 and 16:
You will have problems because you have too many fields in that class. The sum field is not primary information; it is something calculated from the primary information. So you can delete it and calculate the result whenever you require it.

By the way: methods and variable names should never start with Capital Letters. Not A but a.
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cambell. I made the changes as per your request. But still the error is occuring. I am sure i went somewhere wrong; can you please correct me in layman language to understand the program better.

And how to paste the code in the same format as yours ?

Here is my program:

public class AddAandB {

private double a;
private double b;
private double sumAandB;

//public AddAandB() {
//}

//public AddAandB(double numberA, double numberB) {
//a = numberA;
//b = numberB;
//}


public void setSumAandB(double numberA, double numberB) {

a = numberA;
b = numberB;
sumAandB = a+b;
}

public double getSumAandB() {
return sumAandB;

}

public void result () {
System.out.printf("The sum of two number is : " + sumAandB );

}
}


import java.util.Scanner;

public class AddAandBDriver {

public static void main(String[] args) {



Scanner myscanner = new Scanner(System.in);

System.out.print("Enter the 1st Number: " );

double x = myscanner.nextDouble() ;
System.out.println("You entered " + x + " as the 1st Number" ) ;


System.out.print("Enter the 2nd Number: " );
double y = myscanner.nextDouble() ;

System.out.println("You entered " + y + " as the 2nd Number" ) ;
AddAandB R = new AddAandB(); // i also tried AddAandB R = new AddAandB(x,y);
R.getSumAandB();

R.result();

}

}
The output i get is :

Enter the 1st Number: 3.3
You entered 3.3 as the 1st Number
Enter the 2nd Number: 3.3
You entered 3.3 as the 2nd Number
The sum of two number is : 0.0
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added some comments to your code.

 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what CB means by transient sum is that you don't actually need to store the sum of A and B in a seperate variable. You can do something like this return (a + b);
Also the variables A and B inside the main() method have NO RELATIONSHIP to the variables named A and B inside AddAandB. Setting A inside main() does not set A in AddAandB. The two are different. You need to set the A and B inside AddAandB for your program to work. To set them you can pass values through the constructor: setSumAandB(double NumberA, double NumberB).
Hope I helped you out!
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:... To set them you can pass values through the constructor: setSumAandB(double NumberA, double NumberB).

You're mostly right except this is a method and not a constructor.
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million CB and Ashwin. It worked.

how do I paste the code in a proper format like what CB has pasted ?
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamesh Sai wrote:Thanks a million CB and Ashwin. It worked.

how do I paste the code in a proper format like what CB has pasted ?

In the editor, drag the mouse over the text you want formatted and then click on the [Code] button just above the edit screen.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experts,

I am trying to write an If -ElseIf statement and i am getting an error..can you please see where i went wrong.

error i get in the driver class is Cannot find symbol - vaiable aplusb




public class GoodOrBad{ //class



private String a; // private variable declaration
private String b;
private String aplusb;





public GoodOrBad(String a, String b) {
this.a = a;
this.b = b;

}

public void setAplusB(String stringA, String stringB) { // setmethod
a = stringA;
b = stringB;
aplusb = a+b;

}

public String getAplusB(){ // getmethod
return aplusb;
}




public void result () {
System.out.println("The sum of two number is : " + getAplusB() );
System.out.println("The Number of character is : " + aplusb.length() );
}

}

import java.util.Scanner;
import java.lang.String;

public class GoodOrBadDriver {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.println("Enter 1st Name: ");
String x = input.nextLine();

System.out.println("Enter 2nd Name: ");
String y = input.nextLine();

if (aplusb.length() = 0) {
System.out.println(" Type or print a word ");
} else if (aplusb.length() <= 10) {
System.out.println(" It's a small words ");
}
else {
System.out.println(" It's a Big Sentence ");
}
GoodOrBad S = new GoodOrBad(x,y);

S.setAplusB(x,y);


S.result();


}

Regards,
Kamesh
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aplusb is defined in a different class than the main method that uses it. Where are you expecting the value to get set before checking it in the if statement?
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for looking into it, Jeanne.

I am a very beginer to Java and i am trying to write an pgm using the If..else...

Based on your response i rewrote the program, still gives error




 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please post the error you get rather than just the fact that you got an error.

In this case,t here is a subtle one:


Arrays have a length attribute (no parens). Strings have a length() method (with parens).
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne and Bear.

I will use the code tags going forward.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see another potential error.
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the output for adding both the string and counting the total number of characters.

But the if - else if -else condition is not working. It is always pointing to the very first condition.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamesh Sai wrote:

How do you think, in which circumstances your else block would be executed?
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda ! Thanks for looking into it.

I tried all other possible ways in the if-else if - else combination, and it is always pointing to the first Option. What i have provided in the screen is just 1 example.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have 2 classes. "aplusb" which you're expecting to operate on is private instance variable of a class "GoodOrBad". Whats is mentioned in your book about private instance variables?
In class "GoodOrBadDriver" you have another "aplusb" variable (line 42) which has length of 0. So this variable cause you a first if statement to be executed.
On a positive side: your program does exactly what it has to do




 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, go through your comments in a code and think, what purpose of setters and getters methods is?
Setter method gave you some profit already, how getter method could be useful in this case?
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda, Thank You for being patient with me. This is my 3rd program overall with Java.

I reran the code using my getter,but this time it went further worst. Can you please correct me what are all the mistakes i am doing here. (Sorry again)

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamesh Sai wrote:
I reran the code using my getter,but this time it went further worst. Can you please correct me what are all the mistakes i am doing here. (Sorry again)


That's not a good way to learn. How about this? Tell us what compiler errors you are having, why do you think are the cause of those errors, and any confusion you are encountering ... and if there is something mistaken, we can give you a hint in the right direction.

Henry
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamesh, you are make most of the same errors as you made in the other thread you posted other thread
I suggest you go back to that thread and review the suggestions that were made.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamesh Sai wrote:Can you please correct me what are all the mistakes i am doing here. (Sorry again)

You don't need to say "sorry" as you haven't done nothing wrong - you just learning, which is a good thing.
Try to concentrate on a problem now, don't shuffle code lines as usually it is rather confusing, than solving problem.
Stop for a moment, go method by method and try to convince us, what each method does, for what purpose you created them?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamesh Sai wrote:I got the output for adding both the string and counting the total number of characters. . . .
Not using the code I showed, you didn't. Please use the code I showed you, exactly, and see what happens.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Kamesh, . . . the other thread you posted other thread . . .
Thank you for noticing. I think there is enough similarity between the two threads that I ought to join the two into one. Trying merging.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merged! Success!
 
Kamesh Sai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie,

Thanks for taking time and educating me on the mistake i did. Really appreciate it.

I got to know where i went wrong and the changes really did work. Thank you.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you understand why you were told not to declare an aPlusB field?

And … “you're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!