Results 1 to 3 of 3
  1. #1
    reignman is offline Member
    Join Date
    Jul 2014
    Posts
    1
    Rep Power
    0

    Default need help with java project code

    Hey everyone. my name is Raymond I am a MIS major as Ohio State University. nearing the end of the semester and this weeks project has me a little tripped up. below is my main method code. the problem is that it is not catching an exception if an invalid output file is entered. if an invalid input is entered, it still prompts for a output file but then catches exception that the input file was invalid


    public static void main(String[] args) {
    //declare scanner
    Scanner in = new Scanner(System.in);

    //prompt user to input and output file and declare files
    System.out.print("Enter an input file: ");
    String inputFileName = in.nextLine();
    File inputFile = new File(inputFileName);

    System.out.print("Enter and output file: ");
    String outputFileName = in.nextLine();

    //attempt to proceed with program if input/output programs are valid
    try {

    //declare input and output files
    Scanner inFile = new Scanner(inputFile);
    PrintWriter outFile = new PrintWriter(outputFileName);

    //declare variable to count number of individuals in file
    int numOfIndv = 0;

    //declare variables to check if individual is max or min compared to others
    int maxMean = 0;
    String nameOfMax = "";
    int minMean = 1000;
    String nameOfMin = "";

    //print output header to file
    outFile.println("Final Overall Scores Report \n");
    outFile.printf("%-21s %6s %7s %5s %7s", "Name", "Mean", "Median", "Max", "Min \n");
    outFile.printf("%-21s %6s %7s %5s %7s","------------------","------","------", "----", "---- \n");


    while(inFile.hasNext()){
    numOfIndv++;

    //read first line of block for name
    String name = inFile.nextLine();

    //create array of scores, determine mean, median, max and min
    List<Integer> scores = readNextSeries(inFile);
    int mean = getAverage(scores);
    int max = getMax(scores);
    int min = getMin(scores);
    int median = getMedian(scores);

    //determine if individuals mean scores are max or min
    if(mean < minMean){
    minMean = mean;
    nameOfMin = name;
    }
    if(mean > maxMean){
    maxMean = mean;
    nameOfMax = name;
    }

    //format and print individuals results to output file
    outputIndvResults(outFile, name, mean, median, max, min);
    outFile.println();

    }

    //print more results
    outFile.println();
    outFile.println("Total number of participants: " + numOfIndv);
    outFile.println("Highest average score: " + nameOfMax + " (" + maxMean + ")");
    outFile.println("Lowest average score: " + nameOfMin + " (" + minMean + ")");

    //close file
    outFile.close();

    }
    catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    }

    sorry if my code is kind of messy. i'm learning lol. thanks in advance for any pointers

  2. #2
    Matrice is offline Member
    Join Date
    Jul 2014
    Location
    Mililani
    Posts
    37
    Rep Power
    0

    Default Re: need help with java project code

    Wow,

    Well I've seen more complex code, since I am only 2 days old of a Java Programmer, I can confidentally say that I was actually able to understand some of that code. Yeah, I'm not sure what the solution to that would be. You seem good at this though. How long have you been programming Java for? Maybe, your not as bad you think you are at this. :D

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: need help with java project code

    When posting code can you please wrap it in [ code] tags [ /code] so it retains its formatting.
    Unformatted code is very hard to follow.
    Since this is likely to be to do with the flow of your code, seeing the structure is quite important.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 06-26-2014, 08:43 PM
  2. Replies: 1
    Last Post: 12-17-2013, 11:10 AM
  3. Assistance with my coding project? (Morse Code)
    By playtodie in forum New To Java
    Replies: 5
    Last Post: 04-04-2013, 06:05 AM
  4. Replies: 7
    Last Post: 11-27-2012, 11:16 AM
  5. Project Code Name: Sentry
    By mox27 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-18-2008, 02:00 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
  •