Results 1 to 4 of 4
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default filterType cannot be resolved, Syntax error on token "else", } expected?

    I'm not sure why I'm getting these errors. They only begin to happen after the second if statement, and the subsequent if statements are formatted exactly the same.
    The errors are marked by the comments in the code.


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class GazillionSongs {
    	public static void main(String[] args) throws FileNotFoundException {
    		System.out.println("Welcome to Java Song Collection!");
    		System.out
    				.println("This program sorts and filters large databases of popular songs.");
    		System.out
    				.println("Please enter a file that contains a database you wish to filter or sort. (i.e, alistofsongs.txt)"); // sample
    																																// file
    																																// =
    																																// agazillionsongs.txt
    		Scanner input2 = new Scanner(System.in);
    		String filename = input2.nextLine();
    		
    		File f = new File(filename);
    		Scanner tokens = new Scanner(f);
    		
    		ArrayList<Song> songs = new ArrayList<Song>();
    		while ((tokens.hasNextLine())) {
    			songs.add(Song.parse(tokens.nextLine()));
    		}
    
    		System.out.println("Please select and enter a command: Sort, Filter");
    		Scanner input = new Scanner(System.in);
    		String inputType = input.nextLine();
    		
    		SongCollection collection = new SongCollection(songs);
    		
    		if (inputType.equals("Filter"))
    			System.out.println("Please select a filter type: Year, Rank, Artist, Title");
    				if (input.hasNextLine()){
    					String filterType = input.nextLine();
    					if (filterType.equals("Year")){
    						System.out.println("Please enter a year or a range of years that you wish to see songs from.");
    						System.out.println("(i.e, 1998 or 2000-2004)");
    						String rangeType = input.nextLine();
    						collection.filterYear(Range.parse(rangeType));	
    						System.out.println(collection.toString());
    					}else if (filterType.equals("Rank"))
    						System.out.println("Please enter a rank or a range of ranks that you wish to see songs from.");
    						System.out.println("(i.e, 1 or 20-30)");
    						String rangeType = input.nextLine();
    						collection.filterRank(Range.parse(rangeType));	
    						System.out.println(collection.toString());
    					}else if (filterType.equals("Artist"))          //error here: filterType cannot be resolved
    						System.out.println("Please enter an artist that you wish to see songs from.");
    						System.out.println("(i.e, Paramore or Ashley Tisdale)");
    						String artistName = input.nextLine();
    						collection.filterArtist(artistName);	
    						System.out.println(collection.toString());
    					}else if (filterType.equals("Title"))         //error here: Syntax error on token "else", { expected
    						System.out.println("Please enter a song title you wish to see.");
    						System.out.println("(i.e, Misery Business)");
    						String songTitle = input.nextLine();       //error here: input cannot be resolved
    						collection.filterTitle(songTitle);	
    						System.out.println(collection.toString());
    			}
    					
    				
    		}

  2. #2
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: filterType cannot be resolved, Syntax error on token "else", } expected?

    I adjusted my code by deleting the "else" in the if statements, but now the program terminates automatically after inputting commands other than "Year".
    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class GazillionSongs {
    	public static void main(String[] args) throws FileNotFoundException {
    		System.out.println("Welcome to Java Song Collection!"); //greets the user
    		System.out
    				.println("This program sorts and filters large databases of popular songs."); //explains purpose of program
    		System.out 
    				.println("Please enter a file that contains a database you wish to filter or sort. (i.e, alistofsongs.txt)"); // sample
    																																// file
    																																// =
    																																// agazillionsongs.txt
    		Scanner input2 = new Scanner(System.in);
    		String filename = input2.nextLine();
    		
    		File f = new File(filename);
    		Scanner tokens = new Scanner(f);
    		
    		ArrayList<Song> songs = new ArrayList<Song>();
    		while ((tokens.hasNextLine())) {
    			songs.add(Song.parse(tokens.nextLine()));
    		}
    
    		System.out.println("Please select and enter a command: Sort, Filter");
    		Scanner input = new Scanner(System.in);
    		String inputType = input.nextLine();
    		
    		SongCollection collection = new SongCollection(songs);
    		
    		if (inputType.equals("Filter"))
    			System.out.println("Please select a filter type: Year, Rank, Artist, Title");
    				if (input.hasNextLine()){
    					String filterType = input.nextLine();
    					if (filterType.equals("Year")){
    						System.out.println("Please enter a year or a range of years that you wish to see songs from.");
    						System.out.println("(i.e, 1998 or 2000-2004)");
    						String rangeType = input.nextLine();
    						collection.filterYear(Range.parse(rangeType));	
    						System.out.println(collection.toString());
    					 if (filterType.equals("Rank"))
    						System.out.println("Please enter a rank or a range of ranks that you wish to see songs from.");
    						System.out.println("(i.e, 1 or 20-30)");
    						String rangeType2 = input.nextLine();
    						collection.filterRank(Range.parse(rangeType2));	
    						System.out.println(collection.toString());
    					if (filterType.equals("Artist"))
    						System.out.println("Please enter an artist that you wish to see songs from.");
    						System.out.println("(i.e, Paramore or Ashley Tisdale)");
    						String artistName = input.nextLine();
    						collection.filterArtist(artistName);	
    						System.out.println(collection.toString());
    					 if (filterType.equals("Title"))
    						System.out.println("Please enter a song title you wish to see.");
    						System.out.println("(i.e, Misery Business)");
    						String songTitle = input.nextLine();
    						collection.filterTitle(songTitle);	
    						System.out.println(collection.toString());
    					}
    				}		
    				
    		}
    }

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: filterType cannot be resolved, Syntax error on token "else", } expected?

    I have a feeling there's something wrong with my else if statements...but they seem fine so I don't understand

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: filterType cannot be resolved, Syntax error on token "else", } expected?

    Hi,

    I think you've forgotten to add an open curly brace after your IF statement. Please consider to use a block when creating an IF statement. Even if you only have one statement inside the block. It will protect you from unwanted bug that might be produced.

Similar Threads

  1. Syntax error on token "(", @ expected and more errors
    By ebrown086 in forum New To Java
    Replies: 4
    Last Post: 08-17-2013, 05:31 PM
  2. Syntax error on token "}", super expected
    By amentjacob1 in forum New To Java
    Replies: 9
    Last Post: 07-06-2012, 04:15 AM
  3. Error - "The import org.junit cannot be resolved"
    By Jessaurum in forum New To Java
    Replies: 7
    Last Post: 11-03-2009, 03:20 AM
  4. Syntax error on token "(", ; expected
    By baltimore in forum AWT / Swing
    Replies: 3
    Last Post: 10-28-2009, 12:19 AM
  5. Syntax error on token "(", ; expected
    By romilc in forum New To Java
    Replies: 7
    Last Post: 10-24-2009, 01:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •