Java class won't compile - java

Im quite the novice when it comes to programming and im trying to translate this PHP algorithm to Java.
function isPrime($n)
{
$i = 2;
if ($n == 2) {
return true;
}
while ($i < $n) {
if ($n % $i == 0) {
return false;
}
$i++;
}
return true;
}
for ($i = 3; $i < 100; $i++) {
if (isPrime($i)) {
echo $i;
}
}
The only thing i've come up with so far is this.
public class Primtal {
public static boolean isPrime(int n)
{
int i = 2;
if (n == 2) {
return true;
}
while (i < n) {
if ( n % i == 0) {
return false;
}
i++;
}
return true;
}
for(int i = 3; i < 1000; i++){
if (isPrime(i)) {
System.out.print(i);
}
}
}
I realize this look really stupid but i really need to get this to work. I think the problem lies mostly with the for loop as im currently getting the error illegal start of type there. Im not really sure how to translate this to Java and i would appreciate any help i can get.

I believe the problem with your code is that you've put a for loop in the middle of class declaration, which is incorrect - it needs to be inside some method. It seems logical in this case to put it in main(), so it's executed when you run your program. Maybe something like this:
public class Primtal
{
public static boolean isPrime(int n)
{
int i = 2;
if(n == 2)
{
return true;
}
while(i < n)
{
if(n % i == 0)
{
return false;
}
i++;
}
return true;
}
public static void main(String[] args)
{
for(int i = 3; i < 1000; i++)
{
if(isPrime(i))
{
System.out.print(i);
}
}
}
}
(Note the addition of public static void main(String[] args) in the second half of the code.)
Oracle has official tutorials on how Java programs need to be structured, and other basics of the language. You can find the one related to the main method here. Or, to start from the beginning, the full tutorial starts here.

you can't write the for loop
for(int i = 3; i < 1000; i++){
if (isPrime(i)) {
System.out.print(i);
}
}
directly inside a class.
i believe what you wish to do is to have a main method, in which you can have the for loop

Your for loop needs to be within a method of some sort,so you can put it in the main method:
public class Primtal {
public static void main(String [] args)
{
for(int i = 3; i < 1000; i++)
{
if (isPrime(i)) {
System.out.print(i);
}
}
public static boolean isPrime(int n)
{
int i = 2;
if (n == 2) {
return true;
}
while (i < n) {
if ( n % i == 0) {
return false;
}
i++;
}
return true;
}
}

The problem is that your for loop isn't in a method. Enclose it in a main method.
public static void main(String[] args) {
// Your for loop here
}
Also, change print to println, or else all the numbers will appear concatenated together on one line.

Related

Why prime no using recursion is not working?

class PrimeNo {
static int i = 2;
public static boolean isPrime(int ip) {
if (i < ip) {
if (ip % i == 0) {
//system.out.println(i); // it is giving 3
i = 2;
return false;
}
i++;
isPrime(ip);
}
i = 2;
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(9));
}
}
Why it is returning true not false? As I have incremented i++ when inner if is not executed. When inner if executes then it should return false. why It is skipping.
Classical mistake: calling isPrime(ip) alone does not cause a return.

function to find number of ways u can split n objects using parts up to m

I'm using recursion to solve the problem. On paper my answer should work so I went wrong with the code. However, I can't figure exactly where the problem is.
public class Partition {
public static void main(String[] args) {
System.out.println(part(6,4));
}
public static int part(int n, int m) {
if (n==0) {
return 1;
}
else if(m == 0 || n<0) {
return 0;
}
else {
return part(n-m, m) + part(n, m);
}
}
}
You need to reduce m only for the problem to return 9 as you indicated.
public static int part (int n, int m) {
if (n == 0) {
return 1;
} else if (m == 0 || n < 0) {
return 0;
} else {
return part(n - m, m--) + part(n, m);
}
}
I'm not sure what you're trying to do, but if it is to compute the combination it should look like this :
public static int part(int n, int m) {
if(m>n) { //This prevent a wrong input from the user
return part(m, n);
} else if (m==0 || m==n) { //This is your base case
return 1;
} else if(m < 0 || n<0) { //this should not happened, but you never know
return 0;
} else { //this is where you're making mistake(s)
//I don't know if I'm using the formula you are looking for
//But if not, make sure yours do not use part(n, m) otherwise it will run forever
return part(n-1, m) + part(n-1, m-1);
}
}

How do I make this output a boolean?

So here's my code, I want the output to be like this:
Given two numbers, is the second input a multiple of the first?
For Example:
Input:
3
6
Output:
true
public boolean multiple(int m, int n){
int i = 0;
int j = 0;
boolean check = true;
if(n%m == 0){
i++;
return check;
}
else{
j++;
return false;
}
}
When I try it I get an error, I think it's because the return statement is within the if and else statements.
The code is perfectly fine .. Error must be Somewhere else
public class Test1 {
public static void main(String[] args) {
System.out.println(multiple(3, 9));
}
public static boolean multiple(int m, int n){
int i = 0;
int j = 0;
boolean check = true;
if(n%m == 0){
i++;
return check;
}
else{
j++;
return false;
}
}
}
Output
true
here is output see IDEONE
The easiest way is to return the result of your if statement.
return n % m == 0;
I'm not sure what i/j are doing. You don't use them except to increment, but they are local to the function and get GC'd after the return. What you have now is basically this:
boolean bool = some_calculation();
if (bool == true)
{
return true;
}
else
{
return false;
}

How to control parameter in method in java

I have a very easy question but I don’t know what. The sample code below
public class test {
public static void main(String[] args) {
test test = new test();
int temp = test.method(0);
System.out.println("temp = " + temp);
}
public int method(int i) {
if (i < 7) {
i++;
method(i);
}
return i;
}
}
If I want get 7 in the temp in the main what can I do? Just use the other static parameter? or is there any way to achieve this one?
public int method(int i) {
if (i < 7) {
i++;
return method(i);
} else {
return i;
}
}
should work fine :)
breakdown:
IF i is smaller than 7, increment i and return method(i)
ELSE return i (must be 7)
the else block is optional:
public int method(int i) {
if (i < 7) {
i++;
return method(i);
}
return i;
}
but it's more clear if you have the else since it is conditional.

Why am I getting this error with my for loop?

public boolean isNumber(String t) {
for (int i = 0, i<= 9, i++) {
if t.equals(i) {
return true;
}
}
return false;
}
Copypastad the wrong method originally -_-
I have this inside a class compiling with this error:
data_structures/ExpressionEvaluator.java:40: illegal start of type for (int i = 0, i< 10, i++) {
Use semi-colons instead of commas.
for(int i = 0; i < 10; i++) {
//do stuff
}
You should use semi-colon and your if should be surrounded with brackets.
public boolean isNumber(String t) {
for (int i = 0; i <= 9; i++) {
if (t.equals(i)) {
return true;
}
}
return false;
}
I would suggest reading Language Basics
Semicolons separate the qualities of a for loop. Also, the condition of your if block must be surrounded by parentheses.
public boolean isNumber(String t) {
for (int i = 0; i <= 9; i++) {
if (t.equals(i)) {
return true;
}
}
return false;
}
public boolean isNumber(String t) {
for (int i = 0; i<= 9; i++) {
if( t.equals(i) ){
return true;
}
}
return false;
}
1 . use ";" replace of ","
2 .
if(boolean) {
//do stuff
}
Your method only verifies if the String that you passed is a digit, not a number (a number can have more than one digit). You could verifiy it only using a char and calling the ,Character.isDigit
char c = '1';
boolean isDigit = Character.isDigit(c);
If you really want to create your own method, passing a String param, I suggest you to modify like this:
public boolean isDigit(String t) {
return t.length() == 1 && Character.isDigit(t.charAt(0));
}

Categories

Resources