Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Having trouble with if, else if, else if...

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble with if, else if, else if...

    I cannot find my error. I am getting the classic error....


    --------------------Configuration: <Default>--------------------
    E:\Object Java\Assignments\Assignment 5\Binary.java:80: 'else' without 'if'
    else if(bt.equalsIgnoreCase("t")){
    ^
    1 error

    Process completed.


    I know it is a problem with my if, else if statements. However, I cannot figure out where the problem is. I think it is where the if connects with the else if when selected b or t. (I have a few other classes to go with this code but for space saving, I didn't include them).

    Can anyone please help me. I've been working on this code for two days and I can't find the error!

    Also when I paste the code in, it screws up my identation.

    import java.io.*;
    import java.util.Scanner;
     
    public class Binary implements Serializable{
     
        public static void main(String[] args) {
     
            boolean dofile = true;
            String yn = "n", fname = "", bt = "b", rw = "r", line = "";
            PrintWriter textOut = null;
            ObjectOutputStream binaryOut = null;
            ObjectOutputStream outputStream = null;
            ObjectInputStream inputStream = null;
            OutputText mto = null;
            OutputBinary mbo = null;
     
            Scanner keyboard = new Scanner(System.in);
     
            do{
            	System.out.println("Enter the file name.");
            	fname = keyboard.nextLine();
     
            	System.out.println("Chose binary or text file (b/t):");
            	bt = keyboard.nextLine();
     
            	if(bt.equalsIgnoreCase("b"));{
     
            		mbo = new OutputBinary(fname);
                	binaryOut = mbo.createBinaryFile();
     
    				System.out.println("Chose read or write (r/w):");
            		rw = keyboard.nextLine();
     
                	if (rw.equalsIgnoreCase("r")){
     
            			try{
            		    	inputStream = new ObjectInputStream(new FileInputStream(fname));
            		    	String binaryOut = inputStream.readUTF();
            		    	System.out.println("File contains..." + binaryOut);
            			}
     
            			catch (IOException e){
            				System.out.println("The file is blank, please write in it.");
                    	}
     
                     	binaryOut.close();
     
                	}
             		else if(rw.equalsIgnoreCase("w")){
     
            	    	do{
            	    		System.out.println("Enter a line of information to write to the file:");
     
            		  	 	try{
            		     		line = keyboard.nextLine();
            		     		binaryOut.writeObject(line);
            		     		outputStream.flush();
            		     		outputStream.close();
            		     	}
     
            		     	catch(IOException e){
            		     		System.out.println("Problem writing to file.");
            		     	}
     
            				System.out.println("Would you like to enter another line? (y/n)");
            				yn = keyboard.nextLine();
            				if(yn.equalsIgnoreCase("n"))
            				dofile = false;
     
            	      	}while(dofile);
             		}
     
             	}  	
            	else if(bt.equalsIgnoreCase("t")){
     
            		mto = new OutputText(fname);
            	    }
     
            		System.out.println("Chose read or write (r/w):");
            	    rw = keyboard.nextLine();
     
            		if(rw.equalsIgnoreCase("r")){
     
            			try{
            				BufferedReader inputStream = new BufferedReader(new FileReader(fname));
            			}
     
            			catch (IOException e){
            				System.out.println("You have to write something in a file before you can read it!");
            			}
     
            			System.out.println("File contains: " + fname);
            				char next = (char)inputStream.read();
            				textOut.close();
            	 	}
            	 	else if(rw.equalsIgnoreCase("w")){
     
            	   		do{
     
            		  		System.out.println("Enter a line of information to write to the file:");
     
            		  		try{
            		      		line = keyboard.nextLine();
            		      		textOut = new PrintWriter(new FileOutputStream(fname));
            		      		textOut.println(line);
            		      		outputStream.flush();
            		      		outputStream.close();
            		   		}
     
            		   		catch(IOException e){
            		   		System.out.println("Problem writing to file.");
            		   		}
     
            				System.out.println("Would you like to do another file? (y/n)");
            				yn = keyboard.nextLine();
            				if(yn.equalsIgnoreCase("n"))
            				dofile = false;
     
            	    		}while(dofile);
     
                 		}
     
            		System.out.println("Would you like to do another file? (y/n)");
            		yn = keyboard.nextLine();
            		if(yn.equalsIgnoreCase("n"))
            			dofile = false;
     
            }while(dofile);
     
         }
     
      }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Having trouble with if, else if, else if...

    if(bt.equalsIgnoreCase("b"));{
    Look very closely.
    Improving the world one idiot at a time!

Similar Threads

  1. trouble with GUI
    By MrHardRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 04:00 PM
  2. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  3. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM
  4. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM
  5. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM