Getting Compilation error : "java:9: error: ';' expected"; - java

I'm getting this compilation error:
Solution.java:9: error: ';' expected
boolean isEVen(){
^
Code:
import java.util.Scanner;
public class Solution {
public static void main (String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
boolean isEVen(){
return n%2==0;
}
if (isEVen() && n<=5 && n>=2){
System.out.println("Not Weird");
}else{
if(!isEVen()){
System.out.println("Weird");
}else{
if(isEVen() && n>=6 && n<=20){
System.out.println("Weird");
} else{
if (isEVen() && n>20){
System.out.println("Not Weird");
}
}
}
}
}
}
Where am I supposed to put the semi-colon?

Update your code as below.
Move isEVen() method outside of main method and pass int n as a parameter to it.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if (isEVen(n) && n <= 5 && n >= 2) {
System.out.println("Not Weird");
} else {
if (!isEVen(n)) {
System.out.println("Weird");
} else {
if (isEVen(n) && n >= 6 && n <= 20) {
System.out.println("Weird");
} else {
if (isEVen(n) && n > 20) {
System.out.println("Not Weird");
}
}
}
}
}
static boolean isEVen(int n) {
return n % 2 == 0;
}
}

Another approach is to move all the processing outside of main method and into the Solution class itself:
import java.util.Scanner;
public class Solution
{
protected boolean isEVen(int n)
{
return n % 2 == 0;
}
protected void doSomething()
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if (isEVen(n) && n <= 5 && n >= 2)
{
System.out.println("Not Weird");
}
else
{
if (!isEVen(n))
{
System.out.println("Weird");
}
else
{
if (isEVen(n) && n >= 6 && n <= 20)
{
System.out.println("Weird");
}
else
{
if (isEVen(n) && n > 20)
{
System.out.println("Not Weird");
}
}
}
}
}
public static void main(String[] args)
{
Solution s = new Solution();
s.doSomething();
}
}

Related

How can I return a for loop output as a String array?

Instead of my for loop printing the output id, I would like it to return the output into a String array that I can use later in my main method. Is this possible?
public class FizzBuzz {
public static void main(String[] args) {
FizzBuzz runit = new FizzBuzz();
runit.run(20);
}
void run(int highestnumber) {
System.out.println("FizzBuzz will run until: " + highestnumber);
System.out.println();
for (int a = 0; a < highestnumber; a++) {
if (a % 3 == 0 && a % 5 == 0) {
System.out.println("FizzBuzz");
}
else if (a % 3 == 0) {
System.out.println("Fizz");
} else if (a % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(a);
}
}
}
}
Yes, you can return a String array form the runit method, as public String[] runit. Create an empty arrayList in the runit method, in each for loop instead of just printing add the value in ArrayList.
When for loop terminates convert this ArrayList to String Array and return it.
In my opinion the other answers are overkill. There is no need to use an ArrayList for this. Just initialize an array as you allready know how large it will be.
Further on i made the method private. You should always make your methods "as private as possible".
public class FizzBuzz {
public static void main(String[] args) {
FizzBuzz runit = new FizzBuzz();
String[] result = runit.run(20);
for (String s : result) {
System.out.println(s);
}
}
private String[] run(int highestnumber) {
String[] ret = new String[highestnumber];
System.out.println("FizzBuzz will run until: " + highestnumber);
System.out.println();
for (int a = 0; a < highestnumber; a++) {
if (a % 3 == 0 && a % 5 == 0) {
ret[a] = "FizzBuzz";
} else if (a % 3 == 0) {
ret[a] = "Fizz";
} else if (a % 5 == 0) {
ret[a] = "Buzz";
} else {
ret[a] = Integer.toString(a);
}
}
return ret;
}
}
Of course you can, do it like this:
import java.util.ArrayList;
public class FizzBuzz {
public static void main(String[] args) {
FizzBuzz runit = new FizzBuzz();
String[] stuff = runit.run(20);
for(String s : stuff) {
System.out.println(s);
}
}
public String[] run(int highestnumber) {
ArrayList<String> output = new ArrayList<String>();
System.out.println("FizzBuzz will run until: " + highestnumber);
System.out.println();
for (int a = 0; a < highestnumber; a++) {
if (a % 3 == 0 && a % 5 == 0) {
output.add("FizzBuzz");
}
else if (a % 3 == 0) {
output.add("Fizz");
} else if (a % 5 == 0) {
output.add("Buzz");
} else {
output.add(""+a);
}
}
String[] outputArray = output.toArray(new String[0]);
return outputArray;
}
}

Denary number to hexadecimal

As a homework, I was asked to write a program which would convert a denary number to hexadecimal. What I've created kinda works but as the output it gives me reversed number and I have no idea how to solve it (it is my first program).
public static void main(String[] args) {
System.out.println("Give a denary number: ");
Scanner sc = new Scanner(System.in);
int dec1 = sc.nextInt();
String dec = Integer.toString(dec1);
int zmienna;
for(int i = 0; i < dec.length(); i++) {
zmienna = dec1 % 16;
dec1 = dec1 / 16;
if(zmienna == 10) {
System.out.print("A");
}
else if (zmienna == 11) {
System.out.print("B");
}
else if (zmienna == 12) {
System.out.print("C");
}
else if (zmienna == 13) {
System.out.print("D");
}
else if (zmienna == 14) {
System.out.print("E");
}
else if (zmienna == 15) {
System.out.print("F");
}
else if (zmienna == 0 & i == dec.length() - 1) {
System.out.print("");
}
else {
System.out.print(zmienna);
}
}
}
I just change a little in your program i just tried to solve your problem and it works. I just added a StringBuilder and append characters and at the last reverse it.
code
import java.util.Scanner;
public class A {
public static void main(String[] args) {
System.out.println("Give a denary number: ");
Scanner sc = new Scanner(System.in);
int dec1 = sc.nextInt();
StringBuilder sb =new StringBuilder();
String dec = Integer.toString(dec1);
int zmienna;
for(int i = 0; i < dec.length(); i++) {
zmienna = dec1 % 16;
dec1 = dec1 / 16;
if(zmienna == 10) {
//System.out.print("A");
sb.append("A");
}
else if (zmienna == 11) {
//System.out.print("B");
sb.append("B");
}
else if (zmienna == 12) {
//System.out.print("C");
sb.append("C");
}
else if (zmienna == 13) {
//System.out.print("D");
sb.append("D");
}
else if (zmienna == 14) {
//System.out.print("E");
sb.append("E");
}
else if (zmienna == 15) {
// System.out.print("F");
sb.append("F");
}
else if (zmienna == 0 & i == dec.length() - 1) {
System.out.print("");
}
else {
//System.out.print(zmienna);
sb.append(zmienna);
}
}
System.out.println(sb.reverse());
}
}

Returning moves with MiniMax

I've got my code working and the program running perfectly! Turns out the problem also involved the computer not returning a 0 when required. Thank you to everyone who helped. If anyone needs any help like I did check out the code below.
import java.util.Scanner;
public class TicTacToeSolver{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
char[] board={0,0,0,0,0,0,0,0,0};
int player=1,move;
System.out.println("Tic-Tac-Toe:");
while (true){
PrintBoard(board);
if (player==1){
System.out.print("Enter move: ");
move=input.nextInt();
if (ValidMove(board,move)==true){
board[move-1]='X';
player=-1;
}
}
else{
board[MiniMax(board,player,0,9)[1]]='O';
player=1;
}
if(CheckWin(board)!=0){
PrintBoard(board);
System.out.println("You lost!");
System.exit(0);
}
else if (CheckDraw(board)==true){
PrintBoard(board);
System.out.println("Draw!");
System.exit(0);
}
}
}
public static boolean ValidMove(char[]board,int move){
if(move>=1 && move<=9){
if (board[move-1]==0){
return true;
}
}
return false;
}
public static int CheckWin(char[] board){
if (board[0]=='X' && board[1]=='X' && board[2]=='X'||//Horizontal.
board[3]=='X' && board[4]=='X' && board[5]=='X' ||
board[6]=='X' && board[7]=='X' && board[8]=='X' ||
board[0]=='X' && board[4]=='X' && board[8]=='X' ||//Diagonal.
board[2]=='X' && board[4]=='X' && board[6]=='X' ||
board[0]=='X' && board[3]=='X' && board[6]=='X' ||//Vertical.
board[1]=='X' && board[4]=='X' && board[7]=='X' ||
board[2]=='X' && board[5]=='X' && board[8]=='X'){
return -1;
}
else if (board[0]=='O' && board[1]=='O' && board[2]=='O'||//Horizontal.
board[3]=='O' && board[4]=='O' && board[5]=='O' ||
board[6]=='O' && board[7]=='O' && board[8]=='O' ||
board[0]=='O' && board[4]=='O' && board[8]=='O' ||//Diagonal.
board[2]=='O' && board[4]=='O' && board[6]=='O' ||
board[0]=='O' && board[3]=='O' && board[6]=='O' ||//Vertical.
board[1]=='O' && board[4]=='O' && board[7]=='O' ||
board[2]=='O' && board[5]=='O' && board[8]=='O'){
return 1;
}
return 0;
}
public static boolean CheckDraw(char[] board){
int total=0;
for (int i=0;i<9;i++){
if (board[i]!=0){
total+=1;
}
}
if (total==9){
return true;
}
return false;
}
public static void PrintBoard(char[] board){
System.out.println("------------------------------");
System.out.println(board[0]+"|"+board[1]+"|"+board[2]);
System.out.println("-+-+-");
System.out.println(board[3]+"|"+board[4]+"|"+board[5]);
System.out.println("-+-+-");
System.out.println(board[6]+"|"+board[7]+"|"+board[8]);
System.out.println("\n");
}
public static int[] MiniMax(char[] board,int player,int depth, int maxDepth){
int bestScore,bestMove=-1,score;
if (CheckWin(board)!=0||depth==maxDepth){
return new int[] {CheckWin(board),bestMove};
}
if (CheckDraw(board)==true){
return new int[] {0,bestMove};
}
if (player==-1){
bestScore=-9999;
}
else{
bestScore=9999;
}
for (int i=0;i<9;i++){
if (board[i]==0){
if (player==-1){
board[i]='O';
score=MiniMax(board,player*-1,depth+1,maxDepth)[0];
board[i]=0;
if (score>bestScore){
bestScore=score;
bestMove=i;
}
}
else{
board[i]='X';
score=MiniMax(board,player*-1,depth+1,maxDepth)[0];
board[i]=0;
if(score<bestScore){
bestScore=score;
bestMove=i;
}
}
}
}
return new int[] {bestScore,bestMove};
}
}
I think I've answered this elsewhere so there must be a few of you doing this assignment! Below the first level you should only be checking if the levels below return any winning moves or not. You don't need to use any of their values. Example code:
public List<Location> bestMoves() {
List<Location> winningMoves = possibleMoves().stream()
.filter(move -> apply(move).isWinFor(nextPlayer))
.collect(Collectors.toList());
if (!winningMoves.isEmpty())
return winningMoves;
else
return possibleMoves().stream()
.filter(move -> apply(move).bestMoves().isEmpty())
.collect(Collectors.toList());
}
You could return both the move and the score through an object that contains both.
public class TicTacToeSolver{
private static class Move {
int move;
int score;
Move( int m, int s ) {
move = m;
score = s;
}
}
...
}
You could return this object like this:
return new Move( bestScore, bestMove );
Within minimax() itself, you could get the score like this:
Move candidateMove = MiniMax(board,player*-1,depth+1,maxDepth);
int score = candidateMove.score;
At the top level, you could get the best move like this:
Move bestMove = MiniMax(board,player,0,9);
int move = bestMove.move;

Recursive method to add odd numbers

I have the below snippet of code to use a recursive method to add the sum of odd numbers.
I have already coded the iterative method successfully that adds the sum of all odd numbers between n and m which are entered by the user. I'd like to reach that goal but am started slow to make sure I understand what is happening.
I know that it makes more sense to do it iteratively, however I am experimenting with the two types to see which is more efficient. I am stuck on the below as it is not doing what i want it to and i can't understand why.
import java.util.*;
public class SumofOdd
{
public static void main (String [] args)
{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter an odd number");
n = sc.nextInt();
int x = add(n);
}
public static int add(int x)
{
if (x == 0)
{
return 0;
}
else
{
return (x + add(x-1));
}
}
}
I have changed the above to the below. It compiles however stops after I enter the number.
import java.util.*;
public class SumofOdd
{
public static void main (String [] args)
{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter an odd number");
n = sc.nextInt();
if (n%2 == 0)
{
System.out.println("The number entered is even");
}
else
{
int x = add(n);
}
}
public static int add(int x)
{
if (x <= 0)
{
return 0;
}
else
{
return (x + add(x-2));
}
}
}
import java.util.*;
public class OddR{
public static void main (String Args [])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter an odd number");
int max = s.nextInt();
if((max% 2) == 0) {
System.out.println(max + " is Even number and therefore is invalid");
}
else{
System.out.println("Enter a greater odd number");
int m = s.nextInt();
if (m <max){
System.out.println("Invalid data");
}
else{
if((m % 2) == 0) {
System.out.println(m + " is Even number and therefore is invalid");
}
else{
int data = (addodd(m)- addodd(max))+max;
System.out.print("sum:"+data);
}
}
}
}
public static int addodd(int m)
{
if(m<=0)
{
return 0;
}
if(m%2 != 0)
{
return (m+addodd(m-1));
}
else
{
return addodd(m-1);
}
}
}
This is the answer recursively of the sum of odd numbers from n to m
public int addOdds(int n) {
if (n <= 0) {
return 0;
}
if (n % 2 == 0) {
return addOdds(n - 1);
}
return x + addOdds(n - 1);
}
Take care, I never tested the code.
class Oddsum {
public int addodd(int n)
{
if(n<=0)
{
return 0;
}
if(n%2 != 0)
{
return (n+addodd(n-1));
}
else
{
return addodd(n-1);
}
}
}
public class Xyz {
public static void main (String[] v)
{
int n = 9;
Oddsum o = new Oddsum();
int data = o.addodd(n);
System.out.print("sum:"+data);
}
}
This is working fine
public static void main (String[] args){
public static int oddSum(int s){
if (s <= 0)
return 0;
else
return s + oddSum(s -2);
}
}

Simple Java Method Error when Returning

I'm learning methods and was trying to write code that basically tells if a number is prime. However, I keep encountering the error:
error: cannot find symbol
return(isPrime);
^
error: illegal start of type
return(isPrime);
This is my current code (i hope i'm using the method correctly):
import java.util.Scanner;
public class DoublePalindromicPrimes{
public static void main(String args[]){
Scanner in= new Scanner(System.in);
System.out.println("Please enter a number:");
int n = in.nextInt();
//prime(n);
boolean resultPrime = prime(n);
if (resultPrime){
System.out.println("This is a prime");
}
else {
System.out.println("This is not a prime");
}
}
public static boolean prime(int x){
for (int i=2;i<x;i++){
boolean isPrime;
if (x%i==0){
isPrime=false;
}
else{
isPrime=true;
}
}
return isPrime;
}
}
Any help is appreciated!
public static boolean isPrime(int x)
{
if(x > 2) {
if(x%2 == 0) {
return false;
} else {
int sqrt = (int)(Math.sqrt(x));
for(int i=3;i<=sqrt;i+=2) {
if(x%i == 0) {
return false;
}
}
}
return true;
} else if(x==2) {
return true;
} else { //1, 0, and negatives
return false;
}
}
change it to
return isPrime;
note the space and make declaration out of isPrime for loop
You declared isPrime inside your loop, so the return statement can't see it. Move it outside the loop.
public static boolean prime(int x) //throws InvalidNumberException
{
if (x <= 0)
{
//throw new InvalidNumberException("The number is invalid");
}
int squareRoot = (int)(Math.sqrt(x));
for (int i = 2; i <= squareRoot; i++)
{
if (x % i == 0)
{
return false;
}
}
return true;
}
This is an optimized prime validator.

Categories

Resources