Welcome to Dream.In.Code
Become a Java Expert!

Join 149,604 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,897 people online right now. Registration is fast and FREE... Join Now!




Well this is irritating (if else etc.)

 
Reply to this topicStart new topic

Well this is irritating (if else etc.)

jreibe3000
17 Sep, 2007 - 10:02 AM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
Ok, well now this is where I stand. I had my code working fine, but my teacher said she wanted to me to make changes. I made the changes she marked for me, and now I get a bazillion error messages when I try to compile, starting with the error saying I have an else without an if.
Below is my code as she told me it should be. What is going on here?
CODE

import java.util.*;
import java.io.*;

public class Person
{
    private String name;
    private String phone;
    private String email;
    
    public Person()
    {
        name="";
        phone="";
        email="";
    }
    
    public Person(String n, String p, String e)
    {
        name=n;
        phone=p;
        email=e;
    }
    
    public boolean readInput()throws IOException
    {
        Scanner scan=new Scanner(System.in);
        
        boolean flag=true;
        
        System.out.print("Please Enter the person's name or xx to quit: ");
        name=scan.nextLine();

        if(!name.equals("xx")){
            System.out.print("Please Enter a phone number: ");
            phone=scan.nextLine();
            System.out.print("Please Enter an email address: ");
            email=scan.nextLine();
        else
            flag=false;
        }
        return flag;
    }
    
    public void printInput(int printFileHeading)
    {
       if(printFileHeading==1)
            System.out.println("Your person's info is below:");
    
        System.out.println(name);
        System.out.println(phone);
        System.out.println(email);
        
    }

    public void printFile(PrintWriter outputFile)
    {
        outputFile.println(name);
        outputFile.println(phone);
        outputFile.println(email);
    }
    
    public void readFile(BufferedReader inputFile)throws IOException
    {
        name=inputFile.readLine();
        while(name!=null) {
              
            phone=inputFile.readLine();
            email=inputFile.readLine();
            printInput(0);
            name=inputFile.readLine();
        }
    }
        
}

//NOW COMES MY DRIVER CLASS, WHICH STILL COMPILES FINE

import java.io.*;
import java.util.*;

public class PersonDriver
{

    public static String getFileName()
    {
        Scanner scan=new Scanner(System.in);
        String myFile;
  
       System.out.print("Enter the file name and path: ");
        myFile=scan.nextLine();
        return myFile;
   }

    public static void main(String [] args)throws IOException
    {
        Scanner scan=new Scanner(System.in);
        String myFile=getFileName();
        
        boolean flag=true;
        
        Person one=new Person();
    
        FileWriter fwriter=new FileWriter(myFile);
        PrintWriter outputFile=new PrintWriter(fwriter);
    
        do{
            flag=one.readInput();
            if(flag==true){
                one.printInput(1);
                one.printFile(outputFile);
            }
        }while(flag==true);
        
       outputFile.close();
        
        FileReader freader=new FileReader(myFile);
        BufferedReader inputFile=new BufferedReader(freader);    
        
        System.out.println("File Info");
        one.readFile(inputFile);
        inputFile.close();        
    }
}


Much appreciated as always!
User is offlineProfile CardPM
+Quote Post

capty99
RE: Well This Is Irritating (if Else Etc.)
17 Sep, 2007 - 10:06 AM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,252



Thanked: 16 times
Dream Kudos: 550
My Contributions
CODE

        if(!name.equals("xx")){
            System.out.print("Please Enter a phone number: ");
            phone=scan.nextLine();
            System.out.print("Please Enter an email address: ");
            email=scan.nextLine();
        else
            flag=false;
        }


your missing the closing bracket for the if,
and the opening bracket for the else.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month