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("*");


}

}

Recommended Answers

All 9 Replies

I don't feel like reading through the code so something like this should work:

int x = 100;

int numStarts = x/10;

for (int i=0; i<numStars; ++i)
{
   System.out.print("*");
}

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

import java.util.Scanner;

public class GrowthofaFunction{

    //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);
    }
    
}

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 ;)

Of course mine will only print ten starts. The variable was set to 100. Get the user input and go from there.

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.

Code:

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);
 }



}

}

Looks like you have too many commas. Please use code tags and indent your code.

Not sure what you mean.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.