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 24 of 24

Thread: I don't get why this code isn't working

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I don't get why this code isn't working

    Ok so I have a problem and it is:

    The Fibonacci numbers Fn are defined as follows. F0 is 1, F1 is 1, and
    Fi+2 = Fi + Fi+1

    i = 0, 1, 2, ... . In other words, each number is the sum of the previous two numbers. The first few Fibonacci numbers are 1, 1, 2, 3, 5, and 8. One place where these numbers occur is as certain population growth rates. If a population has no deaths, then the series shows the size of the population after each time period. It takes an organism two time periods to mature to reproducing age, and then the organism reproduces once every time period. The formula applies most straightforwardly to asexual reproduction at a rate of one offspring per time period. In any event, the green crud population grows at this rate and has a time period of five days. Hence, if a green crud population starts out as 10 pounds of crud, then in five days there is still 10 pounds of crud; in ten days there is 20 pounds of crud, in fifteen days 30 pounds, in twenty days 50 pounds, and so forth. Write a program that takes both the initial size of a green crud population (in pounds) and a number of days as input, and outputs the number of pounds of green crud after that many days. Assume that the population size is the same for four days and then increases every fifth day. Your program should allow the user to repeat this calculation as often as desired.

    For my code I have this:

    import java.util.*;
    public class Assignment36 {


    public static void main(String a[]) {
    int initialWeight,numberOfDays;
    boolean check = true;
    Scanner sc = new Scanner(System.in);

    while(check)
    {
    System.out.print("Enter the initial size of a green crud population (in pounds):");
    initialWeight = sc.nextInt();
    System.out.print("Enter the number of days:");
    numberOfDays = sc.nextInt();
    System.out.println("The size of the population after "+numberOfDays+" days is "+calculateFinal(initialWeight,numberOfDays)+" pounds.");
    System.out.println("Do you want to repeat the calculation with different values? (y/n):");
    if(sc.next().toUpperCase().charAt(0)=='N')
    check=false;
    }
    }
    public static int calculateFinal(int initialWeight,int numberOfDays)
    {
    int fLength = numberOfDays/5;
    int one=1,two=1,three=0;
    for(int i=1;i<=fLength-1;i++)
    {
    three = one+two;
    one = two;
    two =three;
    }
    System.out.println(three);
    return initialWeight*three;
    }
    }

    But when I run it nothing is showing up. I'm using Netbeans IDE, maybe that has something to do with it?

    An example of what is supposed to show up is something like:


    Enter the initial size of a green crud population (in pounds): 10
    Enter the number of days: 24
    The size of the population after 24 days is 50 pounds.
    Do you want to repeat the calculation with different values? (y/n): n

    But so far all I'm getting is:

    Enter the initial size of a green crud population (in pounds):

    If someone can help me with this it would be greatly appreciated. Thank you.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: I don't get why this code isn't working

    Your program doesn't clear the buffer, read Scanner class carefully to know how to clear the buffer.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    How do you clear the buffer?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    Did you read the API for Scanner?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    Did you read the API for Scanner?
    No why? So I'm guessing there's something wrong with the scanner I'm using?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    I'm not really sure what you mean by that. The API is basically a user manual for Java classes, and it tells you how to use them. You've already been told what you're doing wrong and how to fix it.

    Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    I'm not really sure what you mean by that. The API is basically a user manual for Java classes, and it tells you how to use them. You've already been told what you're doing wrong and how to fix it.

    Java Platform SE 6
    So I changed the java.util.* to java.util.Scanner and it's still not working. I'm looking at the link you gave me and I don't see on the Scanner page how to clear the buffer.. I'm sorry if this seems like a dumb question. I'm new to Java and this is my first time doing something like this.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    I'm not really sure why changing the import would do anything at all. The API is organized by class, and you should see a list of every class in the frame on the left of the page. One of those is Scanner. Click it for more information about Scanner, including how to clear the buffer.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    I'm not really sure why changing the import would do anything at all. The API is organized by class, and you should see a list of every class in the frame on the left of the page. One of those is Scanner. Click it for more information about Scanner, including how to clear the buffer.
    Ok so I think I know which one I'm supposed to use. I think it's nextLine() but how do I type that onto the code? Please if you can help me with this I would thank you 10 times.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    I'm not sure what you mean when you ask how to type nextLine() into the code. You type it in exactly how you would have typed any of the other methods.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    I'm not sure what you mean when you ask how to type nextLine() into the code. You type it in exactly how you would have typed any of the other methods.
    I typed it in and it's still not working. After initialWeight = sc.nextInt() (this was on line 25); I typed sc.nextLine(); on the next line which was line 26 for me. I did the same for numberOfDays. How come it's still not working? Because I'm pretty sure I just cleared the buffer...

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    Did you read the API for nextLine()? What does it do? If you want help, you'll have to provide an SSCCE with updated code that shows what you're doing now.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    Did you read the API for nextLine()? What does it do? If you want help, you'll have to provide an SSCCE with updated code that shows what you're doing now.
    Well it's pretty much the same thing I posted in the original post but here goes.

    import java.util.*;
    public class Assignment36 {


    public static void main(String a[]) {
    int initialWeight,numberOfDays;
    boolean check = true;
    Scanner sc = new Scanner(System.in);

    while(check)
    {
    System.out.print("Enter the initial size of a green crud population (in pounds):");
    initialWeight = sc.nextInt();
    sc.nextLine();
    System.out.print("Enter the number of days:");
    numberOfDays = sc.nextInt();
    sc.nextLine();
    System.out.println("The size of the population after "+numberOfDays+" days is "+calculateFinal(initialWeight,numberOfDays)+" pounds.");
    System.out.println("Do you want to repeat the calculation with different values? (y/n):");
    if(sc.next().toUpperCase().charAt(0)=='N')
    check=false;
    }
    }
    public static int calculateFinal(int initialWeight,int numberOfDays)
    {
    int fLength = numberOfDays/5;
    int one=1,two=1,three=0;
    for(int i=1;i<=fLength-1;i++)
    {
    three = one+two;
    one = two;
    two = three;
    }
    System.out.println(three);
    return initialWeight*three;
    }
    }

    This is what it says for nextLine (from the Scanner page):

    Advances this scanner past the current line and returns the input that was skipped.

    Isn't that clearing the buffer?

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    I might suggest that you write out the input(s) from the command line, then draw where the Scanner is before and after each call. What are you doing with the information returned from the Scanner?

    When you say "it's still not working", what exactly is it doing?

    PS- When posting code, don't forget the highlight tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    I might suggest that you write out the input(s) from the command line, then draw where the Scanner is before and after each call. What are you doing with the information returned from the Scanner?

    When you say "it's still not working", what exactly is it doing?

    PS- When posting code, don't forget the highlight tags.
    All it says is, "Enter the initial size of a green crud population (in pounds):".

    Is it supposed to work just fine?

  16. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    Your program seems to work fine for me. Are you sure this is the code you're running? Have you recompiled?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    Your program seems to work fine for me. Are you sure this is the code you're running? Have you recompiled?
    I'm not sure what recompiled means.

    What are you running this on? I'm using NetBeans IDE 7.1. Maybe it has something to do with that? And yes this is the exact code that I'm running.

  18. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    If you don't know what it means to compile something, I suggest you use the command line. That way you know netbeans isn't hiding something from you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    If you don't know what it means to compile something, I suggest you use the command line. That way you know netbeans isn't hiding something from you.
    And how do I do that?

  20. #20
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  21. #21
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    I'm not supposed to run it like that though... I'm required to submit a screenshot of the thing running on something like Netbeans or any java interface.

  22. #22
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    "Any Java interface" sounds to me like the command prompt would be okay. But even if it's not, you have to figure out whether the problem lies with your code or with netbeans, and taking netbeans out of the picture is the best way to do that. Once you figure out the problem, you can start using it again, if you really want to.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  23. #23
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I don't get why this code isn't working

    Quote Originally Posted by KevinWorkman View Post
    "Any Java interface" sounds to me like the command prompt would be okay. But even if it's not, you have to figure out whether the problem lies with your code or with netbeans, and taking netbeans out of the picture is the best way to do that. Once you figure out the problem, you can start using it again, if you really want to.
    Can you please tell me what you used? I can download whatever software you used and try it on there. Thank you.

  24. #24
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't get why this code isn't working

    I used the command prompt.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Code working only on ide
    By xeyos in forum Java Networking
    Replies: 0
    Last Post: November 17th, 2011, 05:40 PM
  2. my code isn't working an i can't figure out why
    By jack13580 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 12:21 PM
  3. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 06:17 AM
  4. Getting code working with gui
    By Nostromo in forum AWT / Java Swing
    Replies: 2
    Last Post: March 21st, 2011, 09:34 PM
  5. Code working, but not the way I thought...
    By JLogan3o13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2010, 01:34 PM