Link to home
Start Free TrialLog in
Avatar of ukapu2005
ukapu2005

asked on

convert java code into PHP. can some one convert this java code to PHP

public class TestClass {


public static void main(String[] args) {

for(int i=0;i<100;i++) {

int j = i-7;

int k = j%10;

if ( k == 0) {

System.out.println("Actual I value reminder by 7 is : " +i);

}

String str = Integer.toString;

if (str.length() > 1) {

if (str.substring(0,1).equals(str.substring(1))) {

System.out.println("BUZZ " +i);

}

}

}

}

}

ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, strlen should be there.

<?php
class TestClass {

    function main() {
        for($i=0;$i<100;$i++) {
            $j = $i-7;

            $k = $j % 10;

            if ($k == 0){
                echo "Actual I value reminder by 7 is : ".$i;
            }

            //String str = Integer.toString;

            if (strlen($str) > 1){
                if (substring($str,0,1) == substring($str,1)){
                    echo "BUZZ ".$i;
                }
            }
        }
    }
}
?>

Open in new window

greetings ukapu2005, I can see no purpose in converting this function to PHP, as it seems to me, , the main function does NOT do anything useful? ? Is this some sort of homework assignment?
although Roads_Roads has given you a translation, In my view Roads_Roads did not get the Integer thing correct, I would use the $i variable as the $str string. Also there is some difference between Console output in PHP (and Java) and web output.

What are you trying to do? ? Why would you need the output of "BUZZ 3". this is always the same every time this is run and there will always be the same double digits (44, 55) in a run of 0 to 99, and Why is there a String[] args in the main function parameters and the args is never accessed or used? ?
class TestClass {

static public function main($args) {
for($i=0;$i<100;$i++) {
	$j = $i-7;
	$k = $j%10;
	if ($k == 0) {
		echo 'Actual I value reminder by 7 is : '.$i;
		// fwrite(STDOUT, 'Actual I value reminder by 7 is : '.$i."\n");
		}
	$str = ''.$i;
	if (strlen($str) > 1) { 
		if (substr($str,0,1) == substr($str,1,1)) echo 'BUZZ '.$i;
		}
	}
}
} // end of class TestClass

TestClass::main(7);

Open in new window

this looks like home work....