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

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

1. ## 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. ## 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. ## Re: I don't get why this code isn't working

How do you clear the buffer?

4. ## Re: I don't get why this code isn't working

Did you read the API for Scanner?

5. ## Re: I don't get why this code isn't working

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

6. ## 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

7. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
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. ## 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.

9. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
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. ## 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.

11. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
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. ## 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.

13. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
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. ## 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.

15. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
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. ## 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?

17. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
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. ## 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.

19. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
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?

21. ## 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. ## 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.

23. ## Re: I don't get why this code isn't working

Originally Posted by KevinWorkman
"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. ## Re: I don't get why this code isn't working

I used the command prompt.