Else Syntax Error
Contents |
here for a quick overview of the site Help Center else syntax error python Detailed answers to any questions you might have Meta Discuss
Java Else If Syntax Error
the workings and policies of this site About Us Learn more about Stack Overflow the
Syntax Error On Token Else While Expected
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Syntax Error Else Javascript
Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to resolve “Syntax error on token ”else“” in Java? up vote 0 down vote favorite 1 I am syntax error on else delete this token new around here but let's get straight to the question: When I was writing the following code for a class project calculator I came across a "token error". This is the full error message: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token "else", delete this token at calculatorApplet.main(calculatorApplet.java:42) I wrote this code: import java.util.Scanner; import javax.swing.JOptionPane; public class calculatorApplet { Scanner sc = new Scanner(System.in); public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Welcome to the Calculator!"); String option = JOptionPane.showInputDialog(null, "Which calculator mode do you want?"); if (option.equals("Addition")) { Double add1 = Double.parseDouble(JOptionPane.showInputDialog(null, "Okay type the first number(s) of your addition problem.")); Double add2 = Double.parseDouble(JOptionPane.showInputDialog(null, "Now type the second number(s) of your addition problem.")); Double preAdd = add1+add2; Double Add = preAdd; JOptionPane.showMessageDialog(null, "The sum is " + Add + "."); } else { JOptionPane.showMessageDialog(null, "Huh?"); } if (option.equals
This Site Careers Other all forums Forum: Beginning Java syntax error on token "else" Abel Gonza Greenhorn Posts: 3 posted 5 years ago I can't seem to figure out why this is happening. Any syntax error else unexpected tips at all will be very appreciated. syntax error on token "else", delete syntax error on token else finally expected this token. I'll post all the code if requested. public class WhoWins { Pickrps picked = new Pickrps(); String theanswer syntax error on token else delete this token in java = picked.getAnswer(); void whowon(){ if ("rock".equals(theanswer)||"Rock".equals(theanswer));{ System.out.println("rock"); } else if ("paper".equals(theanswer)||"Paper".equals(theanswer));{ System.out.println("paper"); } else ("scissors".equals(theanswer)||"Scissors".equals(theanswer));{ System.out.println("scissors"); } } } Rob Spoor Sheriff Posts: 20678 65 I like... posted 5 years ago Welcome http://stackoverflow.com/questions/17833861/how-to-resolve-syntax-error-on-token-else-in-java to the Ranch! After the word "else" there shouldn't be any guard anymore - just the { or statement. That statement can be another "if", which is what I think you want: else if ("scissors".equals(theanswer)||"Scissors".equals(theanswer));{Also, are you aware of method equalsIgnoreCase? You can use that to shorten your code: else ("scissors".equalsIgnoreCase(theanswer));{ // likewise for the others SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD https://coderanch.com/t/551633/java/java/syntax-error-token 6 - OCEJPAD 6 How To Ask Questions How To Answer Questions Abel Gonza Greenhorn Posts: 3 posted 5 years ago Rob Spoor wrote:Welcome to the Ranch! After the word "else" there shouldn't be any guard anymore - just the { or statement. That statement can be another "if", which is what I think you want: else if ("scissors".equals(theanswer)||"Scissors".equals(theanswer));{Also, are you aware of method equalsIgnoreCase? You can use that to shorten your code: else ("scissors".equalsIgnoreCase(theanswer));{ // likewise for the others What do you mean guard? fred rosenberger lowercase baba Bartender Posts: 12219 36 I like... posted 5 years ago and else statement doesn't have a condition on it. The idea is that on the IF and IF-ELSE lines, you have handled most conditions. the ELSE is for everything that wasn't caught by the previous statements. the general flow should be: if (condition1) { //do stuff } else if (condition2) { //do other stuff} else { //do other other stuff } There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors Abel Gonza Greenhorn Posts: 3 posted 5 years ago got it thanks Rob Spoor Sheriff Posts:
»javacommentsWant to join? Log in or sign up in seconds.|Englishlimit my search to /r/javause the following search parameters to narrow your results:subreddit:subredditfind submissions in "subreddit"author:usernamefind submissions by "username"site:example.comfind submissions from "example.com"url:textsearch https://www.reddit.com/r/java/comments/2k0fye/syntax_error_on_token_else_delete_this_token_error/ for "text" in urlselftext:textsearch for "text" in self post contentsself:yes (or self:no)include (or exclude) self postsnsfw:yes (or nsfw:no)include (or exclude) results marked as NSFWe.g. subreddit:aww site:imgur.com dogsee the search faq for details.advanced search: syntax error by author, subreddit...this post was submitted on 22 Oct 20140 points (25% upvoted)shortlink: remember mereset passwordloginSubmit a new linkSubmit a new text postjavasubscribeunsubscribe57,370 readers~43 users here nowSubmit Link Submit Text Seek Programming Help News, Technical discussions, research syntax error on papers and assorted things of interest related to the Java programming language Please seek help with Java programming in /r/Javahelp! Subreddit rules! Upvote good content, downvote spam, don't pollute the discussion with things that should be settled in the vote count. Join us on IRC freenode.net #reddit-java Related Sub-reddits: Programming Computer Science CS Career Questions Learn Programming Java Help ← Seek help here Learn Java Java TIL Java Examples JavaFX Oracle JVM Languages Clojure Scala Groovy ColdFusion Kotlin Want to practice your coding? DailyProgrammer ProgrammingPrompts ProgramBattles List of useful Frameworks / Libraries / Software Awesome Java (GIT