Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: What is wrong with this code?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation What is wrong with this code?

    //m a rookie at java programming..plz tel me why this doesn't works

    //Program to get input from user and find the average
    //this program terminates after first iteration of DO,WHILE Loop


    import java.util.Scanner;

    public class Avi {
    public static void main(String args[]){

    int flag=0, no;
    Scanner ip = new Scanner(System.in);
    Scanner st = new Scanner(System.in);

    do{
    System.out.println("Enter no : ");
    no = ip.nextInt();

    System.out.println("Wanna enter more numbers?(y/n) : ");
    String c = st.nextLine();

    if(c=="y" || c=="Y")
    flag+=1;
    }while(flag<0);

    }

    //Method to calculate average
    public static int average(int...numbers){
    int total=0;
    for(int x: numbers)
    total+=x;
    return total/numbers.length;
    }

    }
    Last edited by A19; August 11th, 2011 at 02:01 PM.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: What is wrong with this code?

    }while(flag<0);

    should be
    }while(flag<1);

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with this code?

    i did it but it continues asking me Wanna enter more numbers?(y/n) : irrespective of what i type!!

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong with this code?

    Hello there A19, welcome to JavaProgrammingForums.

    When posting code, please learn to surround your code in tags, such as the ones found in my signature.

    Your first issue is a very basic and common mistake, so if you take the time to quickly read the following article you will soon discover the problem.
    http://www.javaprogrammingforums.com...-mistakes.html

    Secondly, although it will pass into the while loop the first time, you should really be checking whether your 'flag' variable is less than OR equal to 0, and not only less than 0.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with this code?

    hey newbie...
    i read the article....

    but ur second thought....man it doesn't works either even if when i checked if "flag variable" is less than or equal to 0.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong with this code?

    Can you post your updated code on here then? Wrapped in code tags of course.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with this code?

    I'm still not 100% awake yet but shouldn't the condition be while(flag > 0). If the user enters "y" to continue and you increment flag by 1 (assuming this gets fixed) then flag will be greater than zero so you can keep looping and allowing user to enter more numbers. Also what happens when user enters "n"?
    Improving the world one idiot at a time!

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong with this code?

    It's true that (flag > 0) is just as, if not more viable than the alternative, but either way, there isn't much logic behind either approach with the current code.
    Really, the only real issue here is the == operator vs .equals() method.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with this code?

    Quote Originally Posted by newbie View Post
    the only real issue here is ....
    No there are 3 issues and they are all very real.

    1. The correct condition in the if statement
    2. The correct condition in the loop
    3. How to exit when user enters "n"
    Improving the world one idiot at a time!

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with this code?

    Just realised there is a 4th problem

    4. The average is never calculated and displayed.

    Even more

    5. Wrong return type for the average method.
    Improving the world one idiot at a time!

  11. #11
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong with this code?

    But you're thinking of the overall program.
    A19 Came here asking why his program didn't work.

    As the average() method is never called, it isn't part of the original issue A19 had.

    5. Wrong return type for the average method.
    Syntax is ok, therefore not a reason why his code "doesn't work".

    What his program is supposed to do is irrespective of how it does it.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with this code?

    So your plan is to have OP fix one problem at a time and then when they encounter the next to come back here and post their code, ask some vague question (as most do) and then fix that one. Rinse and repeat until they have a complete program. Or you can explain to them all the problems with their code so they can work on fixing the entire program.
    Improving the world one idiot at a time!

  13. #13
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: What is wrong with this code?

    Essentially yes, there isn't much point in telling the OP to do this, that and the other before fixing the more underlying problems which take his program nowhere.
    Obviously I realise it would then get repetitive, coming back and forth... but as the OP is obviously learning the basics, it's far better to chop the problem into smaller parts and deal with them one at a time.

    Of course that approach isn't for everyone, but It's what I find to make the most sense for beginners, until they get better problem solving skills.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with this code?

    Each to his/her own. If I see problems in the code then I will alert the person to all of them rather wasting my time doing all this back and forth dealing with one issue at a time.
    Improving the world one idiot at a time!

  15. #15
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with this code?

    Couple of comments and solution:
    1. If your intention is to continue the loop when the user enters "y" or "Y" for C, then you can replace the while loop as }while(c == 'y' || c == 'Y');

    2. Secondly, if you are doing String comparison, you should use .equals() method. What you are comparing is like memory addresses or simulation of pointers.

    3. I don't see any declaration for the variable "flag" although you don't need this anymore if you modify the while construct as explained above in #1.
    Good luck!
    Healthy Mind, Healthy Body and Happy Soul

  16. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What is wrong with this code?

    Quote Originally Posted by higv2000 View Post
    Couple of comments and solution:
    1. If your intention is to continue the loop when the user enters "y" or "Y" for C, then you can replace the while loop as }while(c == 'y' || c == 'Y');
    Since the variable c is a String that won't work.
    2. Secondly, if you are doing String comparison, you should use .equals() method. What you are comparing is like memory addresses or simulation of pointers.
    Already covered and solved.
    3. I don't see any declaration for the variable "flag" although you don't need this anymore if you modify the while construct as explained above in #1.
    Declared on the first line of the main method.

    The problems continue.

    6. Where do you store the values entered?
    Last edited by Junky; August 11th, 2011 at 10:12 PM.
    Improving the world one idiot at a time!

Similar Threads

  1. What is wrong in the code
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 29th, 2011, 12:09 PM
  2. want wrong with my code
    By drum in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:06 AM
  3. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM
  4. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM
  5. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM

Tags for this Thread