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

• February 20th, 2012, 01:08 AM
tai8
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.
• February 20th, 2012, 04:28 AM
Mr.777
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.
• February 20th, 2012, 09:22 AM
tai8
Re: I don't get why this code isn't working
How do you clear the buffer?
• February 20th, 2012, 09:39 AM
KevinWorkman
Re: I don't get why this code isn't working
Did you read the API for Scanner?
• February 20th, 2012, 09:41 AM
tai8
Re: I don't get why this code isn't working
Quote:

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?
• February 20th, 2012, 09:54 AM
KevinWorkman
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
• February 20th, 2012, 10:05 AM
tai8
Re: I don't get why this code isn't working
Quote:

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.
• February 20th, 2012, 10:12 AM
KevinWorkman
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.
• February 20th, 2012, 10:30 AM
tai8
Re: I don't get why this code isn't working
Quote:

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.
• February 20th, 2012, 10:48 AM
KevinWorkman
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.
• February 20th, 2012, 10:51 AM
tai8
Re: I don't get why this code isn't working
Quote:

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...
• February 20th, 2012, 10:57 AM
KevinWorkman
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.
• February 20th, 2012, 11:02 AM
tai8
Re: I don't get why this code isn't working
Quote:

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?
• February 20th, 2012, 11:13 AM
KevinWorkman
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.
• February 20th, 2012, 11:18 AM
tai8
Re: I don't get why this code isn't working
Quote:

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?
• February 20th, 2012, 11:30 AM
KevinWorkman
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?
• February 20th, 2012, 11:37 AM
tai8
Re: I don't get why this code isn't working
Quote:

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.
• February 20th, 2012, 11:38 AM
KevinWorkman
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.
• February 20th, 2012, 11:45 AM
tai8
Re: I don't get why this code isn't working
Quote:

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?
• February 20th, 2012, 11:48 AM
KevinWorkman
Re: I don't get why this code isn't working
• February 20th, 2012, 11:52 AM
tai8
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.
• February 20th, 2012, 12:03 PM
KevinWorkman
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.
• February 20th, 2012, 12:12 PM
tai8
Re: I don't get why this code isn't working
Quote:

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.
• February 20th, 2012, 12:38 PM
KevinWorkman
Re: I don't get why this code isn't working
I used the command prompt.