-1

I want an input file with zero data, 50 names and numbers. I used looping and arrays to hold the names and numbers. I think there's something wrong with my array or loop statements.

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException; 
import java.io.PrintWriter;
import java.io.FileOutputStream;

public class StudentParty 

{
    public static void main(String[] args)

    {
        String name1;
        double number1;
        String name2;
        double number2;
        String name3;
        double number3;
        double count = 3;
        double total = 0;
        double average;
        String [] name = new String [50];
        String [] number = new String [50];

        Scanner fileIn = null;
        PrintWriter OutputStream = null;

        try
        {
            fileIn = new Scanner    
            (new FileInputStream
                    ("StudentPartyInput.txt"));
            OutputStream = new PrintWriter(new FileOutputStream
                    ("StudentPartyOutput.txt")); 
        }

        catch (FileNotFoundException e)
        {
            System.out.println("File not foud.");
            System.exit(0);
        }

        while (fileIn.hasNextLine())
                {
                    line = fileIn.nextLine();
                    count++;
                    name1 = fileIn.nextLine();
                    number1 = fileIn.nextDouble();
                    fileIn.nextLine(); 
                    total = total + number1;
                }

        average = total / count; 
        fileIn.close();

        OutputStream.println(); 
        OutputStream.println(name1 + " had " + (number1 - average) + " more drinks than the average ");
        OutputStream.println(name2 + " had " + (number2 - average) + " more drinks than the average ");
        OutputStream.println(name3 + " had " + (number3 - average) + " more drinks than the average ");
        OutputStream.close();   
    }

}
Sam Estep
  • 12,100
  • 2
  • 30
  • 62
Kim1100
  • 3
  • 3
  • 2
    What makes you think there's something wrong? Do you get an error when you try to compile your code? Do you get an error when you try to run your code? Do you get unexpected output? Please edit your question and add the relevant details. – azurefrog Jul 07 '15 at 18:40
  • did you get unexpected result or any compilation error ?? – kavetiraviteja Jul 07 '15 at 18:47
  • you did not declare variable line... you must be getting compilation error.. – kavetiraviteja Jul 07 '15 at 18:52
  • Please consider that scanner is a buggy class. http://stackoverflow.com/questions/5032356/using-scanner-nextline – Victor Jul 07 '15 at 19:31
  • @Victor I would hardly call it buggy... It performs exactly as it is meant to. In the case you offer, the user is just expecting it to perform in a way it simply does not. Which is certainly not reason enough to avoid using `Scanner` as a class. – River Jul 07 '15 at 22:59
  • No @River, to my humble acknowledge the scanner class is buggy (readInt + readline, doesn't work in that specific order).... do as you want. Just was my personal advice... avoid things that contains errors. – Victor Jul 08 '15 at 18:07
  • 1
    @Victor Fair enough, though I'd still argue over your usage of the words "buggy", "doesn't work", and "errors". But I'll let it be. Personal advice it is. – River Jul 08 '15 at 18:17
  • Glad we share visions on this :) Thanks! – Victor Jul 08 '15 at 18:19
  • It said there are errors in your syntax. Sorry I don't know what the problem is exactly, because it is my first time writing a program. I assumed it was within the while loop because the sqwiggly lines show up in that section. It is resolved thank you everyone for your help. – Kim1100 Jul 09 '15 at 02:58

1 Answers1

0

I am assuming that your sample Input looks pretty much like this

sample One
120.00 
sample Two
130.92
sample three
140.34

i have modified your code.. in your code you have used too many redundant variable's like name1,number1 and all ... i have removed all those by array declaration's .... try this

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

    public class StudentParty 

    {
        public static void main(String[] args)

        {
            double total = 0;
            double average;
            String [] name = new String [50];
            Double [] number = new Double [50];

            Scanner fileIn = null;
            PrintWriter OutputStream = null;

            try
            {
                fileIn = new Scanner    
                (new FileInputStream
                        ("StudentPartyInput.txt"));
                OutputStream = new PrintWriter(new FileOutputStream
                        ("StudentPartyOutput.txt")); 
            }

            catch (FileNotFoundException e)
            {
                System.out.println("File not found.");
                System.exit(0);
            }
             int cnt=0;
            while (fileIn.hasNextLine())
                    {
                        name[cnt]=fileIn.nextLine();
                        number[cnt] = Double.valueOf(fileIn.next());
                        total = total + number[cnt];
                        cnt++;
                        if(fileIn.hasNextLine()) fileIn.nextLine();
                    }

            average = total / (double)cnt; 
            fileIn.close();
            OutputStream.println(); 
            OutputStream.println(name[0] + " had " + (number[0] - average) + " more drinks than the average ");
            OutputStream.println(name[1] + " had " + (number[1] - average) + " more drinks than the average ");
            OutputStream.println(name[2] + " had " + (number[2] - average) + " more drinks than the average ");
            OutputStream.close();   
        }

    }

sample O/P which is expected is

sample One had -10.419999999999987 more drinks than the average 
sample Two had 0.5 more drinks than the average 
sample three had 9.920000000000016 more drinks than the average 
kavetiraviteja
  • 1,717
  • 10
  • 30
  • Thank you so much! It finally works! I spent so much time search on how to use loop and array on Google, but everytime I change one thing another error occurs. Thank you! ^-^ – Kim1100 Jul 09 '15 at 02:52