0

i am almost through with this assignment but i keep getting the error "unreported exception java.io.IOException; must be caught or declared to be thrown" Am i writing my code out of order or am i missing a chunk of code? Thanks for your time!

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

public class database1 {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        String empnumber;
        String firstname;
        String lastname;
        String city;
        String state;
        String zipcode;
        String jobtitle;
        int salary;
        int max = 200000;
        int counter = 0;

        FileWriter ryyt = new FileWriter("c:\\EmployeeData.txt");

        BufferedWriter out = new BufferedWriter(ryyt);

        while (counter < 9) {
            System.out.print("Enter the employee number ...... ");
            empnumber = console.next();

            System.out.print("Enter employee's first name .... ");
            firstname = console.next();

            //firstname = Character.toUpperCase(firstname.charAt(0)) + 
            firstname.substring(1);

            System.out.print("Enter employee's last name ..... ");
            lastname = console.next();
            lastname = Character.toUpperCase(lastname.charAt(0))
                    + lastname.substring(1);

            System.out.print("Enter employee's city .......... ");
            city = console.next();
            city = Character.toUpperCase(city.charAt(0)) + city.substring(1);

            System.out.print("Enter employee's state ......... ");
            state = console.next();
            String upperstate = state.toUpperCase();

            System.out.print("Enter employee's zip code ...... ");
            zipcode = console.next();

            System.out.print("Enter employee's job title ..... ");
            jobtitle = console.next();
            jobtitle = Character.toUpperCase(jobtitle.charAt(0))
                    + jobtitle.substring(1);

            System.out.print("Enter employee's salary ........ ");
            salary = console.nextInt();

            while (salary > max) {
                if (salary > max) {
                    System.out.println(
                            "Salary is over the maxium allowed, re-enter please ...");
                    System.out.print("Enter employee's salary ........ ");
                    salary = console.nextInt();
                } else {
                    System.out.println("Thank you please enter the next employee!");
                }
            }

            System.out.println();

            out.write(empnumber + ",");
            out.write(firstname + ",");
            out.write(lastname + ",");
            out.write(city + ",");
            out.write(upperstate + ",");
            out.write(zipcode + ",");
            out.write(jobtitle + ",");
            out.write(salary + ",");
            out.newLine();

            counter = counter + 1;
        }

        out.close();
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
user7671
  • 11
  • 4
  • please learn how to accept the answer, not just taking it and go... I've answered your question here previously http://stackoverflow.com/questions/43527657/file-writer-do-while-errors/43529361#43529361 – Yahya Apr 21 '17 at 18:50
  • @John The question you mention seems to be deleted. – ventiseis Apr 21 '17 at 22:07
  • @ventiseis He posted a question with codes full of mistakes, causing a lot of errors and I refactored it fully for him and ran it on eclipse + tested it to make sure, then I answered his question... he then took it and did not accept the question -> and now he's raising the same question and when I exposed him he deleted it! – Yahya Apr 21 '17 at 22:18
  • Perhaps a moderator is capable to resolve this. You can flag the question and include a description of this incident. I was just reviewing the question via the triage review queue. – ventiseis Apr 21 '17 at 22:22
  • Hello @john .. Firstly, i deleted the other post because after revising my code, i still had issues so i started a new thread. I am new to Stackoverflow and i didn't understand how to "answer" a question. Secondly, i did not take your code. In fact, i tried it out and it had tons of errors. I ran my code by my instructor and he helped me get to the code you see in this post. And lastly, i'm sorry if i upset you. As i said i am new to stackoverflow and didnt know how to answer your question. Thanks for your post anyways even though i did not take your code and not accept it. have a nice day. – user7671 Apr 22 '17 at 16:32
  • "In fact, i tried it out and it had tons of errors", kindly I'm asking you to undelete the question so people can see my answer and run it (as I tested many times on Eclipse before I posted it) otherwise I'll have to report you to the moderator as a misleading user. – Yahya Apr 22 '17 at 17:59

1 Answers1

1

You have to use throws IOException like this :

public static void main(String[] args) throws FileNotFoundException, IOException 

Or you can surround your statement with try{}catch(...){} :

try {
    //Your code ...
} catch (IOException ex) {
    //exception 
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Heh, posted the same thing :p I'll delete mine since yours is worded better – Imus Apr 21 '17 at 18:46
  • 1
    Thank you.. i fixed that error. i ran program and am getting this error: – user7671 Apr 21 '17 at 18:49
  • ----jGRASP exec: java database1 Exception in thread "main" java.io.FileNotFoundException: c:\EmployeeData.txt (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.(FileOutputStream.java:179) at java.io.FileOutputStream.(FileOutputStream.java:70) at java.io.FileWriter.(FileWriter.java:46) at database1.main(database1.java:26) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. – user7671 Apr 21 '17 at 18:50
  • Do i need to physically add the txt file? is that the issue? – user7671 Apr 21 '17 at 18:50
  • @user7671 try to create your file in a folder location or put it in a folder for example : `ryyt = new FileWriter("d:\\folder\\EmployeeData.txt");` – Youcef LAIDANI Apr 21 '17 at 18:54