Christine Yang
Christine Yang

Reputation: 1

My Java Program is 'Skipping' over statements

I'm trying to solve this problem with Java. (You'll only really need to look at the public static int[] findBase at the end)

import java.io.*;
import java.math.*;
public class palsquare {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    BufferedReader br = new BufferedReader(new FileReader("palsquare.in"));
    int base = Integer.parseInt(br.readLine());
    br.close();
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("palsquare.out")));
    for(int x = 1; x < 300; x++){
        boolean dat = true;
        int[] square = findBase(base, x * x);
        System.out.println("Finished, here is the result: ");
        for(int m = 0; m < square.length; m++){
            System.out.print(square[m] + " ");
        }
        System.out.print("\n\r");
        for(int j = 0; j < square.length/2; j++){
            if(square[j] != square[square.length - 1 - j]){
                dat = false;
            }
        }
        if(dat){
            System.out.println("///////");
            int[] wow = findBase(base, x);
            for(int j = 0; j < wow.length; j++){
                pw.print(wow[j]);
            }
            pw.print(" ");
            for(int j = 0; j < square.length; j++){
                pw.print(square[j]);
            }
            pw.print("\n");
        }
    }
    pw.close();
}

public static int[] findBase(int Base, int num){
    System.out.println("Handling " + num);
    int index = 0;
    while(Math.pow(Base, index) <= num){
        index++;
    }
    System.out.println("The number of digits: " + index);
    if(index < 1){
        index = 1;
    }
    int remaining = num;
    int[] thisOne = new int[index];
    for(int i = index - 1; i <= 0; i++){
        int thisDigit = 0;
        while(Math.pow(Base, i) * thisDigit < remaining){
            thisDigit++;
        }
        thisOne[i] = thisDigit;
        System.out.println("The current digit: " + thisDigit);
        remaining = remaining - (int) Math.pow(Base, i) * thisDigit;
        System.out.println("The amount remaining: " + remaining);
    }
    return thisOne;
}

}

My code.

When I'm running findBase with values larger than Base, findBase returns an array of the correct size, but it is filled with zeroes. I was trying to figure out why that happened, and the last two System.out.println's are not running. I'm also guessing the same with the updating mechanism. Does anyone know why my program is 'skipping' over this much code?

Upvotes: 0

Views: 78

Answers (2)

John Burger
John Burger

Reputation: 3672

This for loop is incorrect:

for(int i = index - 1; i <= 0; i++){

Take a look at it: You initialise i with a number greater than 1, then immediately test to see if it is less than or equal to zero. Since it isn't, the for loop is never entered. And you're incrementing i.

Did you mean the following:

for (int i=index-1; i>0; --i) {

Upvotes: 0

Luke Kot-Zaniewski
Luke Kot-Zaniewski

Reputation: 1161

 for(int i = index - 1; i <= 0; i++)

Should this read this instead:

  for(int i = index - 1; i >= 0; i--){

Since index appears to be a positive integer in context

Upvotes: 2

Related Questions