Unknown syntax error on else - java

I am coding a plugin for a game. This is probably a really simple answer and I have stared at it for about 15 minutes and cannot figure out what the error is on this else statement. It is after an if statement but Eclipse still says that there is a syntax error. I'm sure that this is an easy fix but I is eluding me all the same. Thank you in advance.
#EventHandler (priority = EventPriority.HIGHEST)
public void onPlayerChat(AsyncPlayerChatEvent event)
{
Player sender = event.getPlayer();
String sentmessage = event.getMessage();
if (sentmessage.charAt(0) == '#')
{
event.setCancelled(true);
String message = "";
ArrayList<String> recipients = new ArrayList<String>();
recipients.add("");
int nor = 0;
for (int x = 1; true; x++)
{
if (sentmessage.charAt(x) != ' ')
{
recipients.set(nor, recipients.get(nor) + sentmessage.charAt(x));
}
else if (sentmessage.charAt(++x) == '#');
{
nor++;
recipients.ensureCapacity(nor);
recipients.add("");
}
else //Syntax error is on this else statement
{
message = sentmessage.substring(x);
break;
}
}
plugin.privateMessage(recipients, sender, message);
}
}

else if (sentmessage.charAt(++x) == '#');
Remove ; at end
should be:
else if (sentmessage.charAt(++x) == '#'){
.........
}

Related

Why isnt my code executing after the for loop?

while(counter != this.data.size()) {
for(int i=0; i <= p.data.size(); i++) {
double coefficient = first.getData().getCoefficient() * sec.getData().getCoefficient();
int degree = first.getData().getDegree() + sec.getData().getDegree();
Term current = new Term(coefficient,degree);
if(ans.data.isEmpty()) {
ans.data.addFirst(current);
}
else {
boolean found = false;
DNode<Term > tmp = ans.data.getFirst();
while(found != true) {
if(tmp.getData() == null) {
ans.data.addLast(current);
found = true;
}
else if(current.getDegree() > tmp.getData().getDegree()) {
ans.data.addBefore(current, tmp);
found = true;
}
else if(current.getDegree() == tmp.getData().getDegree()) {
double co = current.getCoefficient() * tmp.getData().getCoefficient();
int deg = current.getDegree() + tmp.getData().getDegree();
tmp.getData().setCoefficient(co);
tmp.getData().setDegree(deg);
found = true;
}
System.out.println("DID I GET HERE");
tmp = tmp.getNext();
}
System.out.println("DID I GET HERE2");
}
System.out.println("DID I GET HERE3");
sec = sec.getNext();
}
System.out.println("DID I GET HERE4");
first = first.getNext();
counter++;
}
My code doesnt even reach the did i get here4 for some reason can anyone tell me why?
I am having a lot of difficulty as this project is due later today the code is suppose to multiply 2 polynomials

How do I output a message using JFrame depending on whether data is incorrect or correct?

I have used a boolean method to decide whether the data is correct or not. For some reason, even when the data is correct or not the data still shows up as incorrect. The boolean is underlined and states that the variable error is never used. When the report button is clicked it should check if the data is correct or not.
If the data is not correct it should show
Data problems
data errors here.
If the data is correct it should show
Medical Report
data here.
here is my code:
if (e.getSource()==reportButton )
{
int riskCounter =0;
boolean error = false;
String convert = ageField.getText();
if (error = true){
log.setText("Data problems \n============\n");
}
else
{
log.setText("Medical Report \n============\n");
}
try {
int age = Integer.parseInt(convert);
if (age < 0 || age > 116)
{
log.append("Age can only be between 0 and 116!\n");
error = true;
}
else if (age > 70)
{
riskCounter++;
error = false;
}
else
{
error = false;
log.append("Age: " + age + "\n");
}
}
catch (NumberFormatException r)
{
log.append("Integers only for Age!\n");
error = true;
}
String smoke = smokesField.getText();
if (smoke.equalsIgnoreCase("y"))
{
error = false;
log.append("Smokes: "+smoke +"\n");
riskCounter++;
}
else if (smoke.equalsIgnoreCase("n"))
{
log.append("Smokes: "+smoke +"\n");
error = false;
}
else
{
error = true;
log.append("Smokes must be one of 'y', 'Y','n','N'\n");
}
String overweight = overweightField.getText();
if (overweight.equalsIgnoreCase("y"))
{
log.append(overweight +"\n");
error = false;
riskCounter++;
}
else if (overweight.equalsIgnoreCase("n"))
{
error = false;
log.append("Overweight: "+ overweight +"\n");
}
else
{
log.append("Overweight must be one of 'y', 'Y','n','N'\n");
}
if (riskCounter <=0)
{
log.append("Low Risk");
}
else if (riskCounter >0 && riskCounter <2)
{
log.append("Medium Risk");
}
else if (riskCounter >=2 && riskCounter <3)
{
log.append("High Risk");
}
else if (riskCounter >=3)
{
log.append("Very High Risk");
}
}
I needed to compare the boolean. To check if the error is true:
if (error){
log.setText("Data problems \n============\n");
}

My program caught an error I don't know how to fix

So I'm trying to code UNO in Java, and I'm still trying to generate cards. I'm not too sure what the problem here is, but for some reason, my code catches an error inside my methods. I've checked my code a few times already and it isn't a syntax error, so I legitimately don't know what's going on with it.
I've temporarily stopped coding this for now so that I won't create any more errors before you guys tell me what's wrong, to make it easier to modify it. Please tell me what I did wrong!
public class JavaUNO {
public static void main(String[] args) throws Exception {
boolean inProgress = false;
boolean drawCard = false;
String[][] playerDeck = {{}};
byte playerDeckLength = 0;
// MAIN OUTPUT
try {
// INITIALIZATION
Scanner scan = new Scanner(System.in);
// PROGRAM STARTING PROMPT
System.out.println("> Deck:");
// **PLAYER DECK INIT**
try {
System.out.println("> Cards Generated:");
while (playerDeckLength < 7) {
// **CARD GENERATION**
try {
// INITIALIZATION
double randType = Math.random();
double randColor = Math.random();
playerDeck[playerDeckLength][0] = "";
playerDeck[playerDeckLength][1] = "";
// GENERATES RANDOM CARD TYPE
if (randType < 0.066) {
playerDeck[playerDeckLength][0] = "0";
} else if (randType < 0.132) {
playerDeck[playerDeckLength][0] = "1";
} else if (randType < 0.198) {
playerDeck[playerDeckLength][0] = "2";
} else if (randType < 0.264) {
playerDeck[playerDeckLength][0] = "3";
} else if (randType < 0.33) {
playerDeck[playerDeckLength][0] = "4";
} else if (randType < 0.396) {
playerDeck[playerDeckLength][0] = "5";
} else if (randType < 0.462) {
playerDeck[playerDeckLength][0] = "6";
} else if (randType < 0.528) {
playerDeck[playerDeckLength][0] = "7";
} else if (randType < 0.594) {
playerDeck[playerDeckLength][0] = "8";
} else if (randType < 0.66) {
playerDeck[playerDeckLength][0] = "9";
} else if (randType < 0.726) {
playerDeck[playerDeckLength][0] = "Reverse Cycle";
} else if (randType < 0.792) {
playerDeck[playerDeckLength][0] = "+2 Cards";
} else if (randType < 0.858) {
playerDeck[playerDeckLength][0] = "+4 Cards";
} else if (randType < 0.924) {
playerDeck[playerDeckLength][0] = "Skip Turn";
} else if (randType < 1) {
playerDeck[playerDeckLength][0] = "Color Change";
}
//GENERATES RANDOM CARD COLOR
if (randColor < 0.25) {
playerDeck[playerDeckLength][1] = "Blue";
} else if (randColor < 0.5) {
playerDeck[playerDeckLength][1] = "Yellow";
} else if (randColor < 0.75) {
playerDeck[playerDeckLength][1] = "Red";
} else if (randColor < 1) {
playerDeck[playerDeckLength][1] = "Green";
}
//CHECKS IF CARD IS WILDCARD
if (playerDeck[playerDeckLength][0] == "+4 Cards") {
playerDeck[playerDeckLength][1] = "Wildcard";
} else if (playerDeck[playerDeckLength][0] == "+2 Cards") {
playerDeck[playerDeckLength][1] = "Wildcard";
} else if (playerDeck[playerDeckLength][0] == "Color Change") {
playerDeck[playerDeckLength][1] = "Wildcard";
}
playerDeckLength += 1;
} catch (Exception e) {
System.out.println("");
System.out.println("> An uncaught error occured!");
System.out.println("> Location: Card Generation");
}
System.out.println("Type: " + playerDeck[playerDeckLength][0] + "; Color: " +
playerDeck[playerDeckLength][1]);
}
} catch (Exception e) {
System.out.println("");
System.out.println("> An uncaught error occured!");
System.out.println("> Location: Player Deck Init");
}
} catch (Exception e) {
System.out.println("");
System.out.println("> An uncaught error occured!");
System.out.println("> Location: Main Output");
}
}
}
COMMAND PROMPT:
> Deck:
> Cards Generated:
> An uncaught error occurred!
> Location: Card Generation
> An uncaught error occurred!
> Location: Player Deck Init
You are initializing an empty two dimensional string array. The code tries to access an index that is not allocated so i think the program is probably throwing IndexOutOfBounds exception
Seems to be a lot of code but very few class / functions ;)
First of all try to organize a bit better your code, it will be easier to debug, modify and maintain it ...
I also invite you to read about Exception and Exception handling in Java, you will see that using Exception every time lead to many problems !
Of course, some of us will be able to make your program work, but honestly, you just need to read a bit more and you'll make it :)
You don't init your array and you probably taking "IndexOutOfBounds" error. Try init your array with something like this: "String[][] playerDeck = new String[7][2];".
Also you need to change your checks from playerDeck[playerDeckLength][0] == "+4 Cards" to if (playerDeck[playerDeckLength][0].equalsIgnoreCase("+4 Cards"))
The problem is that you initialise an empty (2 dimensional) array. When you try to access this it will give an index out of bound exception. If you know the size you'll have to initiate it with that size.
Other that that, please check the comments on your question. This should help you to solve these problems yourself.

Processing - Found one too many { characters without a } character afterward

Here is my code... can someone please tell me what is wrong?
void keyPressed() {
if (key == '\n') {
equation = typing;
switch (equation.charAt(2)) {
case "-":
if (equation.charAt(3) == "x") {
math[0] = -1;
};
else {
math[0] = int(equation.charAt(3) * -1);
};
}
}
}
I don't understand what is wrong. All of the braces match up. Is it that you can't use an if else inside a switch???
try replacing
if (equation.charAt(3)=="x") {
math[0] = -1;
};
else {
math[0] = int(equation.charAt(3)*-1);
};
with
if (equation.charAt(3)=='x') { // NOTE character comparision
math[0] = -1;
}
else {
math[0] = int(equation.charAt(3)*-1);
}

Why do I get "Cannot be Resolved" errors in this code?

I'm trying to check if a move is legal for the game Othello. I'm using a for each loop and once (only once) when I use the variable that it's checking for, it gives me a 'cannot be resolved error'. I have bolded where this occurs. Can anyone help? (Yes I know that the code isn't finished yet, I'm trying to get rid of this error first.)
public boolean isLegal(Location loc1)
{
String currentColor = currentPlayer.getColor();
boolean isLegal = false;
int row = loc1.getRow();
int col = loc1.getCol();
if(board.isValid(loc1))
{
if(board.get(loc1) == null)
{
for(Location tempLoc : board.getValidAdjacentLocations(loc1))
{
if(!board.get(tempLoc).equals(currentColor))
{
int tempRow = tempLoc.getRow();
if((row != tempLoc.getRow()) && (col == tempLoc.getCol()))
{
//count up column
if(**tempLoc.getRow()** < row)
{
for(int i = row; i > 1;)
{
Location tempLoc2 = new Location(i-2, col);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count down column
else
{
for(int i = row; i < 6;)
{
Location tempLoc2 = new Location(i+2, lcol);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
}
else
i=9;
isLegal = true;
}
}
}
else if(col != tempLoc.getCol() && row == tempLoc.getRow())
{
//count left/right row
if(col > tempLoc.getCol())
{
}
else
}
else
{ //count up/right & down/left diag
if(1!=0)
{
}
//count up/left & down/right diag
else
}
}
}
}
}
return isLegal;
}
The "else" statements without a body at the bottom of your code are confusing the compiler about what counts as inside the loops. If you fix that error, the other one will go away.

Categories

Resources