Powers of 10 generator (Cannot find variable i) - java

I am working on the "Powers of" generator and I get an error that variable i cannot be found. I clearly declared it in the for loop
public class EP63
{
private static int answer;
public static int PowerGenerator(double aFactor)
{
for(int i = 1; i < 12; i++);
{
answer = Math.pow(aFactor,i);
nextPower();
return answer;
}
}
public static double nextPower()
{
System.out.println(answer);
}
}
Can someone explain to me how to fix this problem?

It's this line:
for(int i = 1; i < 12; i++);
Change to:
for(int i = 1; i < 12; i++)
The body of the for loop is a single expression. ; is interpreted as an no-op expression, so the { } block is not part of the loop, therefore i is not defined there.

Because you have a ; after the for loop.
That means the for loop have only 1 empty statement.
i.e. your code is just the same as
for(int i = 1; i < 12; i++) {
// nothing
}
{
answer = Math.pow(aFactor,i);
nextPower();
return answer;
}

Related

How do I use a variable from another method? (cannot find symbol - variable evenNum)

I am making a code that only prints the even numbers of an array. But I am having a problem sending evenNum variable from countEm to getAllEvens.
private static int countEm(int[] array)
{
int evenNum = 0;
for (int i = 0; i <array.length; i++)
{
if (i%2 == 0)
{
evenNum++;
}
}
return evenNum;
}
public static int[] getAllEvens(int[] array)
{
int[] evens = new int [evenNum];
int c = 0;
for(int i = 0; i < array.length; i++)
{
if(array[i]%2==0)
{
evens[c] = array[i];
c++;
}
}
return evens;
}
How do I use a variable from another method?
In that way, gettAllEvents will never get acces to evenNum.
You should declare it outside the function countEm

Get array sum and return the value through a method in JAVA

I have this code sample with a method which returns the sum of array values. But instead of the sum, I'm getting 0.
This is my code sample
class Calculation {
int answer;
public int SumOfArrays(int data[], int size) {
answer = 0;
for (int i = data[0]; i < size; i++) {
answer += data[i];
//System.out.println(data[i]);
}
return answer;
}
class Main {
public static void main(String[] args) {
Calculation cal = new Calculation();
int data[]={10,20};
System.out.println(cal.SumOfArrays(data, 2));
}
}
can anyone tell me what;s wrong with my code?
The loop doesn't run at all - the condition data[0] < size is false, so the initial value of answer = 0 is returned.
To make it work, change the statement to (int i = 0; i < size; i++). Also, size might be replaced with data.length.
This is your code with minor corrections. Try it.
class Calculation {
public int SumOfArrays(int data[]) {
int answer = 0; // this is just better
for (int i = 0; i < data.length; i++) {
answer += data[i];
}
return answer;
}
class Main {
public static void main(String[] args) {
Calculation cal = new Calculation();
int data[]={10,20};
System.out.println(cal.SumOfArrays(data));
}
}
The counter i for the for loop is incorrectly initialized. You have initialized it to the value of first element in the array instead of 0. Initialize it to 0 as shown below:
for (int i = 0; i < size; i++) {
answer += data[i];
//System.out.println(data[i]);
}
As you can see from your loop
for(int i = data[0]; i < size; i++)
the variable i is equals to the first element in the data array which means that is greater then the size variable ..so the loop will not begin at all
to fix the problem change your loop to this
for (int i = 0; i < size; i++) {
answer += data[i];
//System.out.println(data[i]);
}

Not sure how to print this?

I'm supposed to modify code that I've written for an assignment:
public class ToweringStrings2 {
public static final int H = 2; //constant for the tower
public static void main(String[] args) {
drawTowers(H);
}
public static void drawTowers(int H) {
for (int i = 1; i <= H; i++) {
System.out.print(" ");
for (int j = 1; j <= i; j++) {
System.out.print("+");
}
System.out.println();
}
for (int k = 1; k <= H + 2; k++) {
System.out.print("#");
}
System.out.println();
}
}
so that it prints sequential numbers starting at 1, instead of +s. Currently, it prints:
This is what the new code is supposed to print:
and so on.
For some reason I'm just really stuck and can't figure it out.
You can create an extra variable to print and increment
Like that:
public class ToweringStrings2 {
public static final int H = 10; //constant for the tower
public static void main(String[] args) {
drawTowers(H);
}
public static void drawTowers(int H) {
int count = 1;
for (int i = 1; i <= H; i++) {
System.out.print(" ");
for (int j = 1; j <= i; j++) {
System.out.print(count++ + " ");
}
System.out.println();
}
for (int k = 1; k <= H + 2; k++) {
System.out.print("# ");
}
System.out.println();
}
}
Step 1 - make it print a variable rather than a hard-coded string. Instead of System.out.print("+"), System.out.print(counter).
For this to work you need to have declared counter somewhere in the same scope as the statement: int counter = 0.
Run this. You'll see it print "0" where it used to print "+".
Step 2 - Now you need to make counter increase by one every time it prints.
Find the right place to add:
counter = counter + 1;
Run it and see it work.
Further notes
A more concise alternative to var = var + 1 is var++.
You can even do this at the same time as you use the value of a variable:
System.out.println(var++);
This can be used to express some algorithms very concisely -- but it can be confusing for beginners, so feel free to not use this technique until you're comfortable with the basics.

Unable to return variable. "java: cannot find variable"

The goal is to find the largest numerical palindrome created by multiplying two three digit numbers.
public static void main (String[]args)
{
ArrayList<Integer> answers = new ArrayList<Integer>();
int temp = 0;
int result = 0;
for (int i = 100; i <= 999; i++){
for (int z = 100; z <= 999; z++){
int prod = i * z;
result = pal(prod);
answers.add(result);
}
}
int answer = Collections.max(answers);
System.out.println(answer);
}
public static int pal(int n){
String temp2 = String.valueOf(n);
for (int i = 0; i < temp2.length()/2; i++){
if (temp2.charAt(i) != temp2.charAt(temp2.length()-1-i)) {
int palindrome = Integer.parseInt(temp2);
return palindrome;
}
}
return palindrome;
}
}
palindrome is not visible outside the loop's if-statement in pal:
public static int pal(int n){
String temp2 = String.valueOf(n);
for (int i = 0; i < temp2.length()/2; i++){
if (temp2.charAt(i) != temp2.charAt(temp2.length()-1-i)) {
int palindrome = Integer.parseInt(temp2); // defined here
return palindrome;
}
}
// palindrome is not defined here
// return something else instead
return palindrome;
}
Move the palindrome variable to a visible block for the return statement . Like this:
public static void main (String[]args)
{
ArrayList<Integer> answers = new ArrayList<Integer>();
int temp = 0;
int result = 0;
for (int i = 100; i <= 999; i++){
for (int z = 100; z <= 999; z++){
int prod = i * z;
result = pal(prod);
answers.add(result);
}
}
int answer = Collections.max(answers);
System.out.println(answer);
}
public static int pal(int n){
int palindrome; //You could place it here.
String temp2 = String.valueOf(n);
for (int i = 0; i < temp2.length()/2; i++){
if (temp2.charAt(i) != temp2.charAt(temp2.length()-1-i)) {
palindrome = Integer.parseInt(temp2);
return palindrome;
}
}
return palindrome;
}
}
Check this: Block scope variables
EDIT:
Initialize the variable or you'll get an error.
In addition to Reut Sharabani's answer, it also appears that you wish to return the int n provided to your pal method every time pal is called.
if (temp2.charAt(i) != temp2.charAt(temp2.length()-1-i)) { //if it is not a palindrome
int palindrome = Integer.parseInt(temp2); //you return it
return palindrome;
}
This means you will add all numbers tested to your ArrayList.
I suggest changing the method's return type to boolean, so you can return true or false depending on whether or not the number is a palindrome. Then, you check if pal(n) == true, and if so, you add it to the ArrayList.

Syntax error on token "return", invalid Type?

I'm trying to complete this exercise from codingbat.com in Java. I get this error "Syntax error on token "return", invalid Type" on this little bit of code and I'm having trouble figuring out why. I'm trying to return the number of times the word "hi" appears in a given String. Thanks!
public int countHi(String str) {
int count = 0;
for(int i = 0; i < str.length() - 1; i++){
if(str.substring(i, i + 2).equals("hi"));
count++;
}
}
return count;
}
public int countHi(String str) {
int count = 0;
for(int i = 0; i < str.length() - 1; i++){
if(str.substring(i, i + 2).equals("hi")); // 1
count++;
} // 2
}
return count;
}
The issue is that you have an ; rather than a { after your if condition (1), which essentially means the if body is empty. The } after the count++ line (2) is, in turn, treated as the end of the for loop (rather than the if as it should be), and the } that should end the for loop instead ends the method.
That leaves your return count; and final } hanging in the middle of a class definition where it's not valid syntax.
return count; is outside the method and you have a ; after your if that shouldn't be there, after good indentation and removing this ;, you'll get:
public int countHi(String str) {
int count = 0;
for(int i = 0; i < str.length() - 1; i++) {
if(str.substring(i, i + 2).equals("hi")) {
count++;
}
}
//Return should be here
} //Method countHI ends here
return count; //??
}
Now you see why it's very important to put braces even if the body contains only one line?
You don't have an opening bracket after your if() condition.
There is a semicolon after your conditional in your if statement.
if(str.substring(i, i + 2).equals("hi"));

Categories