Hello everyone, newbie here, im getting an "illegal start of expression" where the while loop starts but im not sure whats wrong with it. I would really appreciate it if anyone could help me as i understand most of the concepts of java but have a hard time writing the code for it.

The object of this program is for the user to select an operation of the 4 choices available to figure out fibonacci

Thank you in advance for your help!

import java.util.Scanner;

public class Fibonacci 
{
public static void main(String[] args) 
 {
    String operationS;
    char operation;
    
    Scanner scan = new Scanner( System.in );
    
    //Welcome Message
    System.out.println( " Welcome to the Fibonacci Calculator" );
    
    while (operationS != 'q') && (operationS != 'Q');
    {        
    
    //prints a menu, then prompts for the operation
    System.out.println
            ( 
            "\nOperations are: "
            + "\n\t A for Recursive method that calculates Fibonacci numbers"
            + "\n\t B for Recursive method that reads an integer" 
            + "\n\t C for Recursive method that prints the first N even numbers"
            + "\n\t D for Recursive method that adds the first N numbers"
            + "\n\t Q to Quit"
            );
    System.out.println( "Enter your selection: ");
    operationS = scan.next( );
    operation = operationS.charAt( 0 );
    
    //perform the operation and print result
    switch (operation)
      {
        case 'A':
        case 'a':
            System.out.println("\n\t Enter an integer between 5 and 20: ");
        case 'B':
        case 'b':
            System.out.println("\n\t Enter an integer greater than 20: ");
        case 'C':
        case 'c':
            System.out.println("\n\t Enter an integer: ");    
        case 'D':
        case 'd':
            System.out.println("\n\t Enter an integer: ");    
        case 'Q':
        case 'q':
            System.out.println("\n\t Exit: ");    
      }
    }
 }

public static int fibo (int n)
{
    if (n<=1) //base case
        return 1;
    else
        return fibo(n-1) + fibo(n-2); //general case
}  
}

Recommended Answers

All 9 Replies

You never set operationS to value and then you ask if it equals something. You need to set it to a starting value, or a value inputted from the user.

i guess i would have to set operation to something inside the loop, right.

during your loop is already to late, you have to give it a default value, can be a null-value, or any valid value (as long as it's not 'Q' or 'q')

an other problem you're having, is that you compare a String object to characters, and this by using the comparators for primitive data, two big no-no's :)

this is how the changes should be:

...
String operationS = "run";
...
while(!operations.equalsIgnoreCase("q")){
...

...
}
...

Hey stultuske thank you so much for that explanation.

OK so i made those changes but i have the following questions:

1st - When i try to run the program from the command line i get "Exception in thread "main" java.lang.NoClassDefFoundError: fibonacci " What do i need to do so it runs?

2nd - Where should i put the formula that calculates each selection. Should i put it with each CASE or outside the switch like i have it now.

Heres my updated code.

import java.util.Scanner;

public class Fibonacci 
{
public static void main (String[] args) 
 {
    String operationS = "run ";
    char operation;
    int n;
    
    Scanner scan = new Scanner( System.in );
    
    //Welcome Message
    System.out.println( " Welcome to the Fibonacci Calculator" );
    
    while(!operationS.equalsIgnoreCase("q")){
           
    //prints a menu, then prompts for the operation
    System.out.println
            ( 
            "\nOperations are: "
            + "\n\t A for Recursive method that calculates Fibonacci numbers"
            + "\n\t B for Recursive method that reads an integer" 
            + "\n\t C for Recursive method that prints the first N even numbers"
            + "\n\t D for Recursive method that adds the first N numbers"
            + "\n\t Q to Quit"
            );
    System.out.println( "Enter your selection: ");
    operationS = scan.next( );
    operation = operationS.charAt( 0 );
    
    //perform the operation and print result
    switch (operation)
      {
        case 'A':
        case 'a':
            System.out.println("\n\t Enter an integer between 5 and 20: ");
            n = scan.nextInt();
            break;
        case 'B':
        case 'b':
            System.out.println("\n\t Enter an integer greater than 20: ");
            n = scan.nextInt();
            break;
        case 'C':
        case 'c':
            System.out.println("\n\t Enter an integer: "); 
            n = scan.nextInt();
            break;
        case 'D':
        case 'd':
            System.out.println("\n\t Enter an integer: ");  
            n = scan.nextInt();
            break;
        case 'Q':
        case 'q':
            System.out.println("\n\t Exit: "); 
             break;
      }
    }
 }

public static int fibo (int n)
{
    if (n<=1) //base case
        return 1;
    else
        return fibo(n-1) + fibo(n-2); //general case
}  
}

is your filename fibonacci.java or Fibonacci.java ?

> I would really appreciate it if anyone could help me as i understand most of the concepts of
> java

Are you sure? The line while (operationS != 'q') && (operationS != 'Q'); is choke full of syntactic errors. Make sure you concentrate on understanding the concepts rather than just making your homework assignment *work*.

And searching the web for the problem you are facing gives you enough information to solve it yourself.

Fibonacci.java is the name.

in that case, don't forget to compile before you run, I tried it and it worked (well... it ran anyway, doubt it did what you want it to do)

OK perfect. Thank you so much for your help Stultuske. You are a great helper.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.