Help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 5
Reputation: RATED3X is an unknown quantity at this point 
Solved Threads: 0
RATED3X RATED3X is offline Offline
Newbie Poster

Help

 
0
  #1
Mar 11th, 2006
I have the following program compiling and running. However, the program is supposed to print out stars equal to the user input, with each star representing 10. If the user inputs 100 the program should print out 10 stars. No matter what number I input the program is printing out 1 star. Can someone give me a clue as to what I am doing wrong??

import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.util.*;


public class GrowthofaFunction
{
//Method to calculate function
public static int calculateFunction(int x)
{
int result = 0;
final int X;
final int y1;
final int y2;
final int y;


System.out.print("Enter value for x: ");

Scanner stdin = new Scanner(System.in);

x = stdin.nextInt();
y1= x+ (x*x);
y2 = (int) (Math.pow(2,x));
y = y1 + y2;

return y;
}

//Method to printStars
public static void printStars(int numberofStars)
{
for (int i = 0; i <= numberofStars; i++)
{
for (int j = 0; j >=1; j++)
{
int x = 0;
numberofStars = (calculateFunction (x)/10);
System.out.print(" ");
}
}
}
public static void main(String[] args)
{
int x = 0;
int numberofStars = 0;

calculateFunction(x);

printStars(numberofStars);
System.out.println("*");




}

}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Help

 
0
  #2
Mar 11th, 2006
I don't feel like reading through the code so something like this should work:

  1. int x = 100;
  2.  
  3. int numStarts = x/10;
  4.  
  5. for (int i=0; i<numStars; ++i)
  6. {
  7. System.out.print("*");
  8. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: RATED3X is an unknown quantity at this point 
Solved Threads: 0
RATED3X RATED3X is offline Offline
Newbie Poster

Re: Help

 
0
  #3
Mar 11th, 2006
I tried that but all it does is print out ten stars. It does not allow the user to input a number and print out the stars acording to the number that is inputted by the user.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 4
Reputation: InNeedOfHelp is an unknown quantity at this point 
Solved Threads: 0
InNeedOfHelp InNeedOfHelp is offline Offline
Newbie Poster

Re: Help

 
0
  #4
Mar 12th, 2006
Originally Posted by RATED3X
I tried that but all it does is print out ten stars. It does not allow the user to input a number and print out the stars acording to the number that is inputted by the user.
I rewrote your code just so it would print the stars, 1 star for every 10 a user inputs, so 2 stars if input is 20, 10 if input is 100, etc... You should look over your code so you fully grasp what's going on, what's happening to variables you read from user input, etc...

  1. import java.util.Scanner;
  2.  
  3. public class GrowthofaFunction{
  4.  
  5. //Method to printStars
  6. public static void printStars(int numberofStars){
  7. for (int i = 10; i <= numberofStars; i+=10){
  8. System.out.print("*");
  9. }
  10. }
  11.  
  12. public static void main(String[] args){
  13. System.out.print("Enter value for x: ");
  14.  
  15. Scanner stdin = new Scanner(System.in);
  16.  
  17. int x = stdin.nextInt();
  18.  
  19. printStars(x);
  20. }
  21.  
  22. }
Ps. it still throws an exception if your input is incorrect, such as typing '.' instead of a number, but i'll leave handling exceptions to you
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Help

 
0
  #5
Mar 12th, 2006
Of course mine will only print ten starts. The variable was set to 100. Get the user input and go from there.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: RATED3X is an unknown quantity at this point 
Solved Threads: 0
RATED3X RATED3X is offline Offline
Newbie Poster

Re: Help

 
0
  #6
Mar 12th, 2006
I am getting the following errors when I try to compile the program:

----jGRASP exec: javac -g C:\Program Files\jGRASP\GrowthofaFunction1.java

GrowthofaFunction1.java:32: 'class' or 'interface' expected
public static void main(String[] args) {
^
GrowthofaFunction1.java:45: 'class' or 'interface' expected
}
^
GrowthofaFunction1.java:49: 'class' or 'interface' expected
^
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

import java.util.Scanner;
import java.io.*;
import java.math.*;
import java.util.*;


public class GrowthofaFunction1
{
//Method to calculate function
public static int calculateFunction(int x)
{
int result = 0;


System.out.print("Enter value for x: ");

Scanner stdin = new Scanner(System.in);

x = stdin.nextInt();
result=(x + (x*x) + (int)(Math.pow(2,x)));
return result;
}

//Method to printStars
public static void printStars(int numberofStars)
{
for (int i = 10; i <= numberofStars; i+=10)

System.out.print("*");
}
}
public static void main(String[] args) {

System.out.print("Enter value for x: ");

Scanner stdin = new Scanner(System.in);

int x = stdin.nextInt();

printStars(x);
}



}

}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Help

 
0
  #7
Mar 12th, 2006
Looks like you have too many commas. Please use code tags and indent your code.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: RATED3X is an unknown quantity at this point 
Solved Threads: 0
RATED3X RATED3X is offline Offline
Newbie Poster

Re: Help

 
0
  #8
Mar 12th, 2006
Not sure what you mean.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Help

 
0
  #9
Mar 12th, 2006
I meant too many braces or whatever those curly things are. It's hard to tell since all your code is lined up on the left.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: RATED3X is an unknown quantity at this point 
Solved Threads: 0
RATED3X RATED3X is offline Offline
Newbie Poster

Re: Help

 
0
  #10
Mar 12th, 2006
Thanks a lot for your help. I found the braces that you were referrring to and the program is compling and running fine now. I am a beginner at this and I am still learning the proper use of space and indentation to make my programs more readable. Appreciate the help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 2063 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC