Java annoying syntax error [closed] - java

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm new to Java and I'm trying to make an lvling system. Hers my code so far:
import java.util.*;
class Player
{
private String Name;
private int Level;
private int EXP;
int NextGoaltoLvl = 1000;
public Player(String n, int lvl, int xp)
{
Name = n;
Level = lvl;
EXP = xp;
}
public void printStats()
{
System.out.println("Name: " +Name);
System.out.println("Level: " +Level);
System.out.println("Exp: " + EXP);
}
public void addLevel(int addlvl)
{
Level += addlvl;
System.out.println("Congratulations,"+ Name +",you have leveled up to " + Level + "!");
}
public void addExp(int num)
{
EXP += num;
if (EXP == NextGoaltoLvl)
{
addLevel(1); NextGoaltoLvl += 1000; EXP = 0;
}
}
}
public class MainC
{
public static void main(String[] args)
{
Player Player01 = new Player("kert109",1,0);
for (int i = 0; i >= 10000; i++)
{
Player01.addExp(1);
}
Player01.printStats();
}
}
Player01.printStats();
I still having an error here. Says: Syntax error, insert "}" to complete ClassBody.
I have no idea whats wrong. Help? I have check ever "{" and "}". I have cleaned to code too. (Using Eclipse.)

Two errors I see:
1.
Near addExp, there is a while loop outside of the method, which is a syntax error. What's the purpose of this loop anyhow? It's an infinite loop without any breaks or returns in its body - is it actually supposed to go on forever?
2.
for (int i; i >= 10000; i++)
{
Player01.addExp(1);
}
You forgot to initialize i here. Although, this loop doesn't make sense, your condition asks if i is greater than something else and yet you increment it on each iteration (i++). What are you trying to do here?

Instead of
public void addExp(int num)
{
EXP += num;
}
while (true)
{
if (EXP == NextGoaltoLvl)
{
addLevel(1); NextGoaltoLvl += 1000; EXP = 0;
}
}
(an infinite loop? outside all function code? compile error + logic error)
I think you want
public void addExp(int num)
{
EXP += num;
if (EXP == NextGoaltoLvl)
{
addLevel(1); NextGoaltoLvl += 1000; EXP = 0;
}
}
(increment level if XP reaches new level XP)
A. R. S points out another serious issue with your for loop.
Instead of
for (int i; i >= 10000; i++)
{
You want
for (int i = 0; i <= 10000; i++)
{
or just
Player01.addExp(10000);
If what you want to do is to add 10000 XP to the player

you need to put the while loop inside of the method addExp and initialize i to 0 in one of your for loops
public void addExp(int num)
{
EXP += num;
while (true)
{
if (EXP == NextGoaltoLvl)
{
addLevel(1); NextGoaltoLvl += 1000; EXP = 0;
}
}
}
public static void main(String[] args)
{
Player Player01 = new Player("kert109",1,0);
for (int i=0; i >= 10000; i++)
{
Player01.addExp(1);
}
Player01.printStats();
}

1) For loop has an error because you have initiated int i without specifying it's initial value.
for( int i = yourInitialValue; i >= 10000; i++ )
2)
public void addExp(int num)
{
EXP += num;
}
while (true)
{
if (EXP == NextGoaltoLvl)
{
addLevel(1); NextGoaltoLvl += 1000; EXP = 0;
}
}
while loop is outside your addExp method. Where you might want it to be:
public void addExp(int num){
EXP += num;
while (true)
{
if (EXP == NextGoaltoLvl)
{
addLevel(1); NextGoaltoLvl += 1000; EXP = 0;
}
}
}

Your while loop isn't in a method. Here's a revised addExp method.
public void addExp(int num)
{
EXP += num;
if (EXP == NextGoaltoLvl)
{
addLevel(1); NextGoaltoLvl += 1000; EXP = 0;
}
}
You're also not initializing i in your main method. It's also important to note that i >= 10000 will always return false. Your for loop should probably be revised to:
for (int i = 0; i < 10000; i++)
{
Player01.addExp(1);
}

Related

Project Euler Consecutive Prime Sum Java

I wrote a program that didn't work for project Euler problem 50, so if you haven't solved that one probably don't look if you want too solve it.
the problem is linked here:https://projecteuler.net/problem=50
Spoiler to answer below
My answer was 997661, which was exactly ten more than the real solution
My program seems to function to me, but I am inexperienced and was hoping that a more experienced programmer could find what was wrong.
import java.util.ArrayList;
public class ConsecutivePrimeSum {
public static void main(String[] args) {
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 2; i < 1000000; i++) {
if (isPrime(i)) {
primes.add(i);
}
}
int total = 0;
int counter = 0;
while (total + primes.get(counter) < 1000000) {
total += primes.get(counter);
System.out.println(primes.get(counter));
counter += 1;
}
System.out.println(total + " " + counter);
}
public static boolean isPrime(Integer number) {
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 2; i < sqrt; i++) {
if (number % i == 0 && number != i) {
return false;
}
}
return true;
}
}

Why this recursive code repeated infinite? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm just calculating with recursive code, but it goes into infinite loop.
EDIT: Full code.
and IDE(Eclipse) says nothing about it, and it can be running well.
class RepresentWithN {
static int number;
static int N;
static int answer;
public int solution(int N, int number) {
RepresentWithN.N = N;
RepresentWithN.number = number;
answer = 9;
calc(0, 0);
return answer == 9 ? -1 : answer;
}
static int conN(int length) {
int tmp = N;
for (int i = 1; i < length; i++) {
tmp += tmp * 10;
}
return tmp;
}
static void calc(int prev, int count) {
if (count == 9) {
return;
}
if (prev == number) {
answer = Math.min(answer, count);
return;
}
System.out.println("count=" + count + " prev=" + prev);
for (int i = 1; i <= 5; i++) {
calc(prev + conN(i), count + 1);
calc(prev - conN(i), count + 1);
calc(prev * conN(i), count + 1);
calc(prev / conN(i), count + 1);
}
}
It repeated 'count' is around 7 or 8, and don't know why.
eventually count has the value 9. check this by printing this values.
static void calc(int prev, int count) {
System.out.println("count=" + count + " prev=" + prev);
if (count == 9) {
return;
}
...
There are so many branches in your recursion and that needs a huge amount of calculation. Thats why it is not ending. You can wait another hour or days/years to see the ends of this program. Or replace this code with efficient one.
Try this. After each run, uncomment one of the recursive calls.
public class ForEverDemo {
static long count = 0;
public static void main(String[] args) {
alongtime(0);
System.out.println("count = " + count);
}
public static void alongtime(int v) {
count++;
if (v == 9) {
return;
}
for (int i = 0; i < 3; i++) {
alongtime(v + 1);
// alongtime(v + 1);
// alongtime(v + 1);
// alongtime(v + 1);
// alongtime(v + 1);
}
}
}

how do i get even placed digits from a number in java

I want my program to get all the even digits from a number input. Then multiply those with digits with 2. If the result is a two digit number, add them. At the end i want it to give me the sum of all the even digits.
public class evenplaceadd {
public static void main(String[] args) {
System.out.println(sumOfevenPlace(5566));
}
public static int sumOfevenPlace(int number)
{
int maxDigitLength = 4;
int sum = 0;
for (int i = 0; i < maxDigitLength; i++)
{
if (i % 2 == 0)
{
int digita = number % 10;
int digitb =digita*2;
int digitc;
if(digita < 9)
{
sum = sum + digitb;
}
else if(digitb>9)
{
digitc =(digitb % 10)+ (digitb /10);
sum =sum + digitc;
}
}
else
{
number = number/10;
}
}
return sum;
}
}
Your code seems ok for the most part. There are some minor flaws in the code which I am sure you will be able to figure out after understanding the code provided below. I have changed it up a bit and made it easier to read. Please confirm it is working, and next time please provide the code when asking question. I know you are new to the community, and so am I. Its a learning experience for all of us. All the best in the future :)
public static void int sumOfEvenDigits(int num){
int sum = 0;
int lastDig = 0;
while(num/10 != 0)
{
lastDig = num % 10;
num = num / 10;
if(lastDig % 2 != 0)
{
continue;
}
if(lastDig > 10)
{
sum += lastDig / 10;
sum += lastDig % 10;
}
else
{
sum += lastDig;
}
}
return sum;
}

Cannot find symbol compile error in for loop

I'm having a problem with my code here. I'm trying to find all multiples of 3 and 5 up to one thousand and add them all up, and at the end when I try to output the sum, java gives me a 'cannot find symbol' error. Can anybody figure out what's wrong here?
public class Problem1
{
public static void main(String []args)
{
//int sum1;
//int sum2;
int finalSum;
for(int i = 0; i < 1000; i++)
{
if(i % 3 == 0)
{
int sum;
sum += i;
}
else if(i % 5 == 0)
{
int sum;
sum += i;
}
}
System.out.println(sum);
}
}
Java has block scoping, which means that the sum declared in between {}s (braces) is not visible outside. Declare sum once, outside of the for loop.
public class Problem1
{
public static void main(String []args)
{
int sum = 0;
for(int i = 0; i < 1000; i++)
{
if(i % 3 == 0)
{
sum += i;
}
else if(i % 5 == 0)
{
sum += i;
}
}
System.out.println(sum);
}
}
you can not declare sum in loop.Then it is local to that method.
It's because you're "creating" sum inside the if statements which limits their scope - they're created within the if blocks and destroyed at the next closing brace.
Get rid of those two int sum; lines inside the if blocks and put it at the top of the function (where the rather useless finalSum is). Or just use finalSum everywhere.
You can also combine the if conditions for shorter code:
public class Problem1
{
public static void main(String []args)
{
int finalSum = 0;
for(int i = 0; i < 1000; i++)
if((i % 3 == 0) || (i % 5 == 0))
finalSum += i;
System.out.println(finalSum);
}
}
you have defined the variable sum inside the the if/else which limits the scope of the variable.System.out.print() statement is outside the scope of sum hence you are getting the error.
public class Problem1
{
public static void main(String []args)
{
int sum=0;
for(int i = 0; i < 1000; i++)
{
if(i % 3 == 0)
{
sum += i;
}
else if(i % 5 == 0)
{
sum += i;
}
}
System.out.println(sum);
}
}

How to get the last number from the loop [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm working on application which should show the biggest factor of a number and it has to be the prime number too.
That's my app:
public class BiggestFactor {
public static void main(String[] args) {
double dev = 0d;
for (double j = 0; j < 984654354654d; j++) {
if (984654354654d % j == 0) {
dev = j;
}
// show dev when is a prime number
double i;
for (i = 2; i < dev; i++) {
double n;
n = dev % i;
if (n == 0) {
// do nothing - not a prime number
break;
}
}
if (i == dev) {
System.out.println(dev);
}
}
}
}
and my question is how to get as a result just the last number? In my case I get bunch of numbers.
The minimal change is to declare a new variable result:
double result = -1;
and instead of printing dev, simply save its value in result:
if (i == dev) {
result = dev;
}
Then, at the end of the function, print result:
System.out.println(result):
public class BiggestFactor
{
public static void main(String[] args)
{
double dev = 0d;
double last = dev;
for (double j = 0; j < 984654354654d; j++)
{
if (984654354654d % j == 0)
{
dev = j;
}
double i;
for (i = 2; i < dev; i++)
{
double n;
n = dev % i;
if (n == 0)
{
break;
}
}
if (i == dev)
{
last = dev;
}
}
System.out.println(last);
}
}
I have done a little refactoring and moved the logic of checking prime in another function for better understanding. Also change the number to 1001 to increase the verification speed :P
public class BiggestPrimeFactor {
public static void main(String[] args) {
double dev = 0d;
double numberToCheck = 1001d;
for (double j = 0; j <= numberToCheck / 2; j++) {
if (numberToCheck % j == 0 && isPrime(j)) {
dev = j;
}
}
System.out.println(dev);
}
private static boolean isPrime(double n) {
boolean prime = true;
for (long i = 2; i <= n / 2; i += 2) {
if (n % i == 0) {
prime = false;
break;
}
}
return prime;
}
}
Basically, what is does it to continuously update dev to be the current biggest prime factor.

Categories