Output of this byte code is not working properly

import java.util.*;
 
public class Code3 {
 
/**
* @ param args the command line arguments
*/
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
System.out.println("enter your %age");
float a=s1.nextFloat();
System.out.println("enter your location");
String b=s1.next();
if(a>90 && b=="chandigarh"){
System.out.println("selected");
}
else if(a>95&&b!="chandigarh"){
System.out.println("selected");
}
else if(a>=80&&a<90&&b=="chandigarh"){
System.out.println("waiting");
}
else if(a>90&&a<95&&b!="chandigarh"){
System.out.println("waiting");
}
else if(a<80&&b=="chandigarh"){
System.out.println("rejected");
}
else if(a<90&&b!="chandigarh"){
System.out.println("rejected");
}
}
}

Replies

  • Manish Goyal
    Manish Goyal
    Basic approach for debugging any program is to analyse what will the input and what will be the output, after this comes the logic.

    You should better print value of a and b and check if it is taking correct input
  • Anoop Kumar
    Anoop Kumar
    1. what is not working?? You didn't provided input and output and what are your expected output.

    2. Comparing string like
     b=="chandigarh"
    is wrong way compare strings, If you want to compare strings you should always use
    string1.equals(string2)
    .


    run the following code you will understand.

    String a = new String("ABC");
            String b = "ABC";
            String c = "ABC";
            if (a==b){
                System.out.println(true+"a==b");
            }
         
            if (b==c){
                System.out.println(true+"b==c");
            }
    
  • Rits rishi
    Rits rishi
    okay! i got it! finally..πŸ˜€ my program is running... now i am actually having confusion at function calling..
    my new program:-
    import java.util.*;
    public class pro {
      int a,b;
    static Scanner s1=new Scanner(System.in);
    public static void main(String args[]){
    pro p1=new pro();
     
    int o=p1.menu();
    System.out.println("o="+o);
    p1.input();
    p1.choice(o);
     
    }
    public int menu() //first function
    { int num;
    System.out.println("welcome to my calculator");
    System.out.println("choose your operation: \n enter 1 for addition \n enter 2 for subtraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulus");
    System.out.println("enter your choice number:");
    int c=s1.nextInt();
    num=s1.nextInt();
    return num;
    }
    public void input() //2nd function
    {
    System.out.println("enter first number:");
    a=s1.nextInt();
    System.out.println("enter second number:");
    b=s1.nextInt();
     
    }
    public int add(int x,int y) //3rd function
    {
    int z=x+y;
    return z;
    }
    public int sub(int x,int y) //4th function
    {
    int z=x-y;
    return z;
    }
    public int mul(int x,int y) //5th function
    {
    int z=x*y;
    return z;
    }
    public int divide(int x,int y) //6th function
    {
    int z=x/y;
    return z;
    }
    public int mod(int x,int y) //7th function
    {
    int z=x%y;
    return z;
    }
    public int choice(int c) //8th function
    {
      int f=0;
    if(c==1)
    f=add(a,b);
    else if (c==2)
        f=sub(a,b);
    else if (c==3)
        f=mul(a,b);
    else if (c==4)
        f=divide(a,b);
    else if (c==5)
        f=mod(a,b);
     
    return f;
    }
     
    }
    //this calculator is not returning values..KINDLY help me in configuring my mistakes.. i want to be "code confident!". 😐
  • simplycoder
    simplycoder
    Rits rishi
    okay! i got it! finally..πŸ˜€ my program is running... now i am actually having confusion at function calling..
    my new program:-
    import java.util.*;
    public class pro {
      int a,b;
    static Scanner s1=new Scanner(System.in);
    public static void main(String args[]){
    pro p1=new pro();
     
    int o=p1.menu();
    System.out.println("o="+o);
    p1.input();
    p1.choice(o);
     
    }
    public int menu() //first function
    { int num;
    System.out.println("welcome to my calculator");
    System.out.println("choose your operation: \n enter 1 for addition \n enter 2 for subtraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulus");
    System.out.println("enter your choice number:");
    int c=s1.nextInt();
    num=s1.nextInt();
    return num;
    }
    public void input() //2nd function
    {
    System.out.println("enter first number:");
    a=s1.nextInt();
    System.out.println("enter second number:");
    b=s1.nextInt();
     
    }
    public int add(int x,int y) //3rd function
    {
    int z=x+y;
    return z;
    }
    public int sub(int x,int y) //4th function
    {
    int z=x-y;
    return z;
    }
    public int mul(int x,int y) //5th function
    {
    int z=x*y;
    return z;
    }
    public int divide(int x,int y) //6th function
    {
    int z=x/y;
    return z;
    }
    public int mod(int x,int y) //7th function
    {
    int z=x%y;
    return z;
    }
    public int choice(int c) //8th function
    {
      int f=0;
    if(c==1)
    f=add(a,b);
    else if (c==2)
        f=sub(a,b);
    else if (c==3)
        f=mul(a,b);
    else if (c==4)
        f=divide(a,b);
    else if (c==5)
        f=mod(a,b);
     
    return f;
    }
     
    }
    //this calculator is not returning values..KINDLY help me in configuring my mistakes.. i want to be "code confident!". 😐
    First of all this code lacks documentation.
    You should document each and every logic that is used describing the logic on higher level of abstraction.

    When debugging, try to use break-points, watch variables, stack-trace, it will give you immense knowledge about your own program.

    If you are using normal notepad and command prompt, try debugging by printing the values.

    something like this: int c=s1.nextInt();
    System.out.println("c = "+c);

    I notice that you are not using c anywhere.
    int c=s1.nextInt();
    num=s1.nextInt();

    do you think this might be a problem.
    Try inputing same value twice.
  • rahul69
    rahul69
    #-Link-Snipped-# just comment the following line and see if it works:
    int c=s1.nextInt();
  • nareshkumar6539
    nareshkumar6539
    Rits rishi
    okay! i got it! finally..πŸ˜€ my program is running... now i am actually having confusion at function calling..
    my new program:-
    import java.util.*;
    public class pro {
      int a,b;
    static Scanner s1=new Scanner(System.in);
    public static void main(String args[]){
    pro p1=new pro();
     
    int o=p1.menu();
    System.out.println("o="+o);
    p1.input();
    p1.choice(o);
     
    }
    public int menu() //first function
    { int num;
    System.out.println("welcome to my calculator");
    System.out.println("choose your operation: \n enter 1 for addition \n enter 2 for subtraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulus");
    System.out.println("enter your choice number:");
    int c=s1.nextInt();
    num=s1.nextInt();
    return num;
    }
    public void input() //2nd function
    {
    System.out.println("enter first number:");
    a=s1.nextInt();
    System.out.println("enter second number:");
    b=s1.nextInt();
     
    }
    public int add(int x,int y) //3rd function
    {
    int z=x+y;
    return z;
    }
    public int sub(int x,int y) //4th function
    {
    int z=x-y;
    return z;
    }
    public int mul(int x,int y) //5th function
    {
    int z=x*y;
    return z;
    }
    public int divide(int x,int y) //6th function
    {
    int z=x/y;
    return z;
    }
    public int mod(int x,int y) //7th function
    {
    int z=x%y;
    return z;
    }
    public int choice(int c) //8th function
    {
      int f=0;
    if(c==1)
    f=add(a,b);
    else if (c==2)
        f=sub(a,b);
    else if (c==3)
        f=mul(a,b);
    else if (c==4)
        f=divide(a,b);
    else if (c==5)
        f=mod(a,b);
     
    return f;
    }
     
    }
    //this calculator is not returning values..KINDLY help me in configuring my mistakes.. i want to be "code confident!". 😐
    In main method you are just calling the choice() method you are not storing the return value from choice()
    p1.choice(o); change the statement to below
    System.out.println("result is :"+p1.choice(o));( OR )
    int result=p1.choice(o);
    Systme.out.println("result is: "+result);
  • Rits rishi
    Rits rishi
    nareshkumar6539
    In main method you are just calling the choice() method you are not storing the return value from choice()
    p1.choice(o); change the statement to below
    System.out.println("result is :"+p1.choice(o));( OR )
    int result=p1.choice(o);
    Systme.out.println("result is: "+result);
    thankyou so much!!!!! it is returning values now.. πŸ˜€ but i think i am still confused in function calling with arguements..... i don't actually realize when and where to pass arguements and when do we have to store a function into some variable?? can you please tell me any link to a website that clears my confusion or any tutorial that could help me??
  • Rits rishi
    Rits rishi
    rahul69
    #-Link-Snipped-# just comment the following line and see if it works:
    int c=s1.nextInt();
    thankx for the help.. but even after comment it was not returning the values.
  • Rits rishi
    Rits rishi
    simplycoder
    First of all this code lacks documentation.
    You should document each and every logic that is used describing the logic on higher level of abstraction.

    When debugging, try to use break-points, watch variables, stack-trace, it will give you immense knowledge about your own program.

    If you are using normal notepad and command prompt, try debugging by printing the values.

    something like this: int c=s1.nextInt();
    System.out.println("c = "+c);

    I notice that you are not using c anywhere.
    int c=s1.nextInt();
    num=s1.nextInt();

    do you think this might be a problem.
    Try inputing same value twice.
    thankyou.. but i think i am still confused in function calling with arguements..... i don't actually realize when and where to pass arguements and when do we have to store a function into some variable?? can you please tell me any link to a website that clears my confusion or any tutorial that could help me??
  • simplycoder
    simplycoder
    Rits rishi
    thankyou.. but i think i am still confused in function calling with arguements..... i don't actually realize when and where to pass arguements and when do we have to store a function into some variable?? can you please tell me any link to a website that clears my confusion or any tutorial that could help me??
    Before writing the function, think whether you would like to use the result some where. If yes, then return the variable and store the returned value from the function and use it wherever you like.
    If not, then don't.

    Example,
    1.)Write a program (WAP) with functions, to check if a number is prime. Print the answer from a method dedicated only to display the result, all function calling has to be done from main.

    2.) WAP with functions a program to check if a number is an Armstrong number, Print the answer from a method dedicated only to display the result, all function calling has to be done from main.

    3.)WAP with function to display the following in these scenarios
    If user enters
    1, then print "Hello Java."
    2, then print "Hello C"
    3, then print "Hello C++"
    4, then print "CE is awesome!"

    Before writing the program, think if you need to store the result of the function some where and code accordingly.

You are reading an archived discussion.

Related Posts

Guru Gobind Singh Indraprastha University​, Delhi conduct MCA program have various colleges some government and more private there are about 1,040 seats, entrance exam CET is conducted every year. ​...
Myself Keerthana.R, in the 'Department of Civil Engineering' from Tamilnadu, pursuing 3rd year in engineering, I do Gardening, watching Television, Try new recipes. I joined CrazyEngineers to know different new...
Have been unable to locate through several searches. Expect there is a typo in the info received. Understand a previous spec version was HS 1-1350, and unable to locate as...
What is switch on the fault scheme? when it is used? and why it is instantaneous ?
how to calculate the total load of your home in terms of amperes?