Java Else Syntax Error Token
Contents |
here for a quick overview of the site Help Center Detailed answers to any syntax error on token else while expected questions you might have Meta Discuss the workings and policies syntax error on token else delete this token eclipse of this site About Us Learn more about Stack Overflow the company Business Learn more about syntax error on token delete this token eclipse hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Error On Else Processing
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 new around here but let's get straight to the question: else: syntax error python 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("Subtraction")) { Double sub1 = Double.parseDouble(JOptionPane.showInputDialog(null, "Okay type the first number(s) of your subtraction problem.")); Double sub2 = Double.parseDouble(JOptionPane.showInputDialog(null, "Now typ
Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums Forum: Beginning Java syntax error on token "else" Abel Gonza Greenhorn Posts: 3 posted syntax error on token expected 5 years ago I can't seem to figure out why this is happening.
Syntax Error On Token(s), Misplaced Construct(s)
Any tips at all will be very appreciated. syntax error on token "else", delete this token. I'll post all the
Unexpected Token Else
code if requested. public class WhoWins { Pickrps picked = new Pickrps(); String theanswer = 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"); } } } http://stackoverflow.com/questions/17833861/how-to-resolve-syntax-error-on-token-else-in-java Rob Spoor Sheriff Posts: 20703 68 I like... posted 5 years ago 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));{ https://coderanch.com/t/551633/java/syntax-error-token // likewise for the others SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 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: 12225 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 Abe
1 of 1 New Topic/Question Reply 2 Replies - 4653 Views - Last Post: 17 January 2013 - 06:58 PM Rate Topic: #1 W1174503 New D.I.C Head Reputation: 0 Posts: 38 Joined: 07-January 13 Syntax error on token "else", delete this token Posted 17 January 2013 - 06:03 PM ReadFiles.java line 29 has a syntax error. It keeps highlighting “else” but I am not sure how to fix it. Any ideas? //This program is to Figure out whether it can find data import java.io.*; import java.util.StringTokenizer; public class ReadFiles { //This finds the info and put it in a string File file = new File("C:\\stocks\\intro.csv"); //Define how many rows in a file int row = 0; // Need to change the file into an 2d array String [][] items; //Check if the file is a file public boolean checkIsFile(){ //returns file if " " file exists return file.isFile(); } //find number of row in csv file public int findRowNumber(){ row=0; if(checkIsFile());{ //do this if its a file try{ BufferedReader reader = new BufferedReader(new FileReader(file)); while((reader.readLine()) !=null){ row++; } }catch(Exception e){ System.out.println(e); } }else{ System.out.println("This is not a file"); } return row; } } //Main program public class StockMarket { public static void main(String[] args) { //initialize and object ReadFiles r = new ReadFiles(); System.out.println(r.findRowNumber()); } } Is This A Good Question/Topic? 0 Back to top MultiQuote Quote + Reply Replies To: Syntax error on token "else", delete this token #2 macosxnerd101 Games, Graphs, and Auctions Reputation: 12002 Posts: 44,829 Joined: 27-December 08 Re: Syntax error on token "else", delete this token Posted 17 January 2013 - 06:06 PM You have an unnecessary semi-colon here if(checkIsFile());{. Remove that and you should be good. Was This Post Helpful? 1 Back to top MultiQuote Quote + Reply #3 W1174503 New D.I.C Head Reputation: 0 Posts: 38 Joined: 07-January 13 Re: Syntax error on token "else", delete this token Posted 17 January 2013 - 06:58 PM thank you sir Was This Post Helpful? 0 Back to top MultiQuote Quote + Reply ← Previous Topic Java Next Topic → Page 1 of 1 Related Java TopicsbetaSyntax Error With Class Definition Problem - Syntax Error On Token "public", Class Expected After This To Syntax Error On Token ")", ; Expected After This Token "Syntax Error On Token ";" , , Expected" ? Various Errors In Eclipse, "Syntax Error On Token ";", { E Syntax Error On Token "case" Syntax Error On Token "new", @ Expected Inventory Program Part 6 - Syntax Error On Token(s), Misplaced Construct(s) Syntax Error On Token "goto" - ... Using Arrays - Synt