473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

else if

153 100+
I am stuck here and have done various research on else if statements and it seems to be what im meant to do. Please help if you can.


Expand|Select|Wrap|Line Numbers
  1.     private boolean isValidGuess() 
  2.     {
  3.         if ((Character.isLowerCase(guess)) && (!guessedAlready.contains(guess)));
  4.         {
  5.             return true;
  6.         }
  7.  
  8.  
  9.  
  10.        else if (!Character.isLowerCase(guess))
  11.             {
  12.                 return false;
  13.                 System.out.println(guess + " is not a lower case entry");
  14.             }
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.              else if(guess == '*')
  23.             {
  24.                 System.out.println("You gave up!");
  25.             }
  26.             else
  27.             {
  28.                 return false
  29.                 System.out.println("You guessed " + guess + " already");
  30.             }
  31.  
  32.  
  33.     }
  34.  
  35.  
I am getting an else without if error.

Also, as you might have seen, Im also stuck with does not contain, guessed already. Any input appreciated.

Brendan
Oct 20 '08 #1
15 4174
Ganon11
3,652 Expert 2GB
Your final return statement is missing a semicolon...that's the only issue I saw with a brief look-over.
Oct 20 '08 #2
brendanmcdonagh
153 100+
Thanks for that but still doesn't solve the else with out an if problem :(
Oct 20 '08 #3
Laharl
849 Expert 512MB
The structure of your code looks like this:

if{}
else if {
else if {}
else {}
}

Do you see the problem now?
Oct 20 '08 #4
brendanmcdonagh
153 100+
I'm sorry i don't.

I have a method opening {

i have an if stament with {}

i have an else if statements {}

close method }

Sorry if im being stupid!
Oct 20 '08 #5
JosAH
11,448 Expert 8TB
Copy and paste the error message verbatim; you get an "else without if" error
because you have a trailing semi colon at the end of your first if clause.

You also get a bit of whining about a missing semi colon. If you fix that you get
an error saying "unreachable code" because you want to print something after
returning from the method.

kind regards,

Jos
Oct 20 '08 #6
brendanmcdonagh
153 100+
Hi Jos, thanks for your reply(again!)

I'm using netbeans so included all errors and all code. Sorry to overwhelm you. just in case you need it.

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Brendan.LIVINGROOM\My Documents\M257_08J_TMA01_DownloadBrendanMcDonagh\T MA01 Download\TMA01\build\classes
C:\Documents and Settings\Brendan.LIVINGROOM\My Documents\M257_08J_TMA01_DownloadBrendanMcDonagh\T MA01 Download\TMA01\src\tma01q2\Guesser.java:56: 'else' without 'if'
else if (!Character.isLowerCase(guess))


C:\Documents and Settings\Brendan.LIVINGROOM\My Documents\M257_08J_TMA01_DownloadBrendanMcDonagh\T MA01 Download\TMA01\src\tma01q2\Guesser.java:95: illegal start of expression
public String toString()
C:\Documents and Settings\Brendan.LIVINGROOM\My Documents\M257_08J_TMA01_DownloadBrendanMcDonagh\T MA01 Download\TMA01\src\tma01q2\Guesser.java:113: ';' expected
}


C:\Documents and Settings\Brendan.LIVINGROOM\My Documents\M257_08J_TMA01_DownloadBrendanMcDonagh\T MA01 Download\TMA01\src\tma01q2\Guesser.java:118: '}' expected
}


Expand|Select|Wrap|Line Numbers
  1.  
  2. package tma01q2;
  3.  
  4. import java.util.Scanner;
  5. import java.util.TreeSet;
  6. import tma01q1.Player;
  7.  
  8. /**
  9.  * Title: Guesser class  
  10.  * Description: This class represents the player guessing a code word
  11.  * and is to be completed according to instructions in TMA01 Question 2   
  12.  */
  13.  
  14. public class Guesser extends Player
  15. {
  16.     private char guess;
  17.     private Set<Character> guessedAlready;
  18.     final char EXIT_CHARACTER = '*';
  19.  
  20.  
  21.     //complete the constructor    
  22.     public Guesser(String name)
  23.     {
  24.         super(name);
  25.         guessedAlready = new TreeSet<Character>();
  26.  
  27.  
  28.     }
  29.  
  30.     //complete the following methods
  31.  
  32.     //readChar returns the first character entered by a user
  33.     //after conversion to lowercase form
  34.     private char readChar()
  35.     {
  36.         Scanner aScanner = new Scanner(System.in);
  37.         String currentline = aScanner.nextLine();
  38.         currentLine.toLowerCase();
  39.         return currentLine.charAt(0);
  40.  
  41.     }    
  42.  
  43.     // isValidGuess returns true if the value of guess is valid
  44.     // (a previously unguessed, lowercase alphabet letter, or the exit character)
  45.     // otherwise it returns false.
  46.     // This method also displays an appropriate error message 
  47.     // if an invalid input has been detected.
  48.     private boolean isValidGuess() 
  49.     {
  50.         if ((Character.isLowerCase(guess)) && (!guessedAlready.contains(guess)));
  51.         {
  52.             return true;
  53.         }
  54.  
  55.  
  56.  
  57.        else if (!Character.isLowerCase(guess))
  58.             {
  59.                 return false;
  60.                 System.out.println(guess + " is not a lower case entry");
  61.             }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.              else if(guess == '*')
  70.             {
  71.                 System.out.println("You gave up!");
  72.             }
  73.             else
  74.             {
  75.                 return false;
  76.                 System.out.println("You guessed " + guess + " already");
  77.             }
  78.  
  79.  
  80.     }
  81.  
  82.     // inGuessedAlready returns true if the received character is
  83.     // stored in the guessedAlready structure, otherwise it returns false
  84.     private boolean inGuessedAlready()
  85.     {   
  86.         if(guessedAlready.contains(guess))
  87.         {
  88.             return true;
  89.         }
  90.         else
  91.         {
  92.             return false;
  93.         }
  94.  
  95.     // Returns the Guesser's name and a list of guessed characters
  96.     public String toString()
  97.     {
  98.         System.out.println(this.getName() + "guesses " + guessedAlready.toString());
  99.     }
  100.  
  101.     // This is the main method that a driver class uses.
  102.     // It returns a 'valid' character
  103.     // (EXIT_CHARACTER or lowercase and not previously guessed).
  104.     // The guess and guessedAlready variables are updated when required
  105.     public char turn()
  106.     {  
  107.         System.out.println("Please have a guess or enter the exit character");
  108.         guess = this.readChar();
  109.         while(!this.isValidGuess())
  110.         {
  111.            guess = this.readChar();
  112.         }
  113.         guessedAlready.add(guess);
  114.     }    
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121.  
Oct 20 '08 #7
brendanmcdonagh
153 100+
I found the other errors, just a missing } but still have else without an if
Oct 20 '08 #8
JosAH
11,448 Expert 8TB
I found the other errors, just a missing } but still have else without an if
Reread my reply again; you have a construct like this:

Expand|Select|Wrap|Line Numbers
  1. if ( ... ); // <--- note the trailing semicolon here
  2. {
  3.    // this is always executed
  4. }
  5.  
kind regards,

Jos
Oct 20 '08 #9
asedt
125 100+
I found the other errors, just a missing } but still have else without an if
I guess you have nestled the if/elseif/else statment worng, but you can post you current code agen.

And you indentation/whitespace hurt my eyes.
Oct 20 '08 #10
chaarmann
785 Expert 512MB
An "else" must always be after an "if", directly!. "always" means you cannot just write an "else" clause by itself! And "directly" means, you cannot write some other code between the end of the "if" and beginning of the "else".
So where is the "if" for the "else" in line 10? It's not the one in line 3!

What you have written is following:
if (condition) action; // in your case, action is not there, so it's empty! The action cannot follow AFTER the semicolon!
{ // start of a code block
some code // this code is ALWAYS executed, independent of the condition!.
} // end of code block
else // where is my if-statement? there is only some stange code above me, but no "if"!

What you may not know are 2 cases that you combined:
1.)
Expand|Select|Wrap|Line Numbers
  1. x = 7;
  2. y = false;
  3. if (y = (x==7)); // this is a legal statement. notice "=" and "==". It sets y to true. Always. Now the action of the if-statement is called. But this action is not given, so it does nothing!
  4. x = 9; // this is alway set, regardless of the previous "if" statement
2.)
Expand|Select|Wrap|Line Numbers
  1. x=7;
  2. { // legal. it's just the start of a code block. You could write "synchronized" in front if you like. It has nothing to do with an "if" statement.
  3.   y=10;
  4.   int z=20;
  5. }
  6. y=30; // just set y to 30 that was just set to 10 before.
  7. // x has still old value = 7
  8. // but you cannot access z anymore!. Z was only valid inside the code block. If you try to write "z=10;", it will not compile. (Scope!)
Oct 20 '08 #11
brendanmcdonagh
153 100+
Thjanks everyone,

Your suggestion worked Jos - as always - thanks

Regards

Brendan
Oct 20 '08 #12
brendanmcdonagh
153 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. private boolean isValidGuess() 
  3.     {
  4.         if ((Character.isLowerCase(guess)) && (!guessedAlready.contains(guess)))
  5.         {
  6.             return true;
  7.         }
  8.  
  9.  
  10.  
  11.        else if (!Character.isLowerCase(guess))
  12.             {
  13.                 return false;
  14.                 System.out.println(guess + " is not a lower case entry");
  15.             }
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.              else if(guess == '*')
  24.             {
  25.                 return false;
  26.                 System.out.println("You gave up!");
  27.  
  28.             }
  29.             else
  30.             {
  31.                 return false;
  32.                 System.out.println("You guessed " + guess + " already");
  33.             }
  34.  
  35.  
  36.     }
  37.  
I'm really sorry about this but i ve 3 unreachable errors now to do with this method.

my understanding is this
I can have an if stament
{
it's execution code
}
then as many else if()
{
exectubale code
}

then an else for for all other options
else()
{
}

am i wrong?
Oct 20 '08 #13
brendanmcdonagh
153 100+
sorry solved, the return statements has to be last in statement block,

sorry, sorry, sorry!
Oct 20 '08 #14
r035198x
13,262 8TB
sorry solved, the return statements has to be last in statement block,

sorry, sorry, sorry!
Not necessarily. You just have to make sure that every possible path of execution has it.
Oct 21 '08 #15
JosAH
11,448 Expert 8TB
In the OPs example it has this unreachable code (three times):

Expand|Select|Wrap|Line Numbers
  1. return <something>:
  2. System.out.println(<something>);
  3.  
After returning you can't print anything so the println() is unreachable.

kind regards,

Jos
Oct 21 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
3
by: Patrice | last post by:
Hi, I need to do multi-conditional statements like below, but this error is displayed : Expected 'End' /myFilepath, line x else response.write(arrCorpo(sparam,sdiv)) end if I don't...
5
by: WindAndWaves | last post by:
Hi Team The function below searches all the tables in a database. However, if subsearch = true then it searches all the objects listed in a recordset (which are all table names). I thought to...
5
by: Brie_Manakul | last post by:
Is there a way in javascript to do an if else that shows a script in an iframe? Let me know if that doesn't make sense. We have a portal and in a portlet I need to grab these javascript links to...
4
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*,...
8
by: pelicanstuff | last post by:
Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile? All the Elses and Ifs look ok to me but there's a few. Private Sub...
23
by: bearophileHUGS | last post by:
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other...
17
by: JRough | last post by:
I'm trying to get error proof code. I have this code which seems to work but now I look at it I think it should be elseif not else and I wonder why it works. It is in the block:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.