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 3 of 3

Thread: help with codoe

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    17
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default help with codoe

    The purpose of the code is to write a name and test score into a new file. The main problem i get is that it doesn't write the name in the file but it is able to write the test scores. Also I have a section where i check for the largest and smallest test score and that seems to not work either.
    The error occurs as soon as I enter a name when promted.

     
    import java.io.*;
    import java.text.NumberFormat;
    import java.util.Scanner;
    public class statsP2 {
     
     
    	public static void main(String[] args) {
     
     
     
    		Scanner input = new Scanner(System.in);
    		FileReader in;
    		FileWriter out;
    		BufferedReader readFile;
    		BufferedWriter writeFile;
    		String name;
    		Double score;
    		String score1;
    		String fileName;
    		int numScores;
    		double totalScores = 0;
    		double avgScore;
    		double smallest;
    		double largest;
    		NumberFormat percent = NumberFormat.getPercentInstance();
    		/* prompt user for file name 
    		System.out.print("Enter the name of the application file: ");
    		fileName = input.nextLine();
    		*/
    		File dataFile = new File("testName2.txt");
     
     
     
     
     
    try {
    			out = new FileWriter(dataFile);
    			writeFile = new BufferedWriter(out);
    			// writes name to file
    			System.out.println("Enter your name: ");
    				name=input.next();
    				writeFile.write(name);
    				writeFile.newLine();
     
    				System.out.println("How many scores would you like to enter? : ");
    			 numScores = input.nextInt();
    			 double[] myNum = new double[numScores];
    				System.out.println("Enter " + numScores + " class score: ");
    				for( int i =0; i<numScores; i++) {
    					 score=input.nextDouble();
    					  totalScores += score;
    				writeFile.write(String.valueOf(score));
    				writeFile.newLine();
    				}
     
    				avgScore = totalScores/numScores;
    				in = new FileReader(dataFile);
    				readFile = new BufferedReader(in);
    				name=readFile.readLine();
    				System.out.println(name);
    	    		while ((score1 = readFile.readLine()) != null) {
    	    			for( int i =0; i<=numScores; i++) {
    						 myNum[i] = Double.parseDouble(score1);
     
    					}
     
    				}
    	    		largest =myNum[0];
       			 smallest=myNum[0] ;
       			for (int i = 1; i < myNum.length; i++) {
       				   if (myNum[i] > largest)
       				    largest = myNum[i];
       				   else if (myNum[i] < smallest)
       				    smallest = myNum[i];
       				  }
    	    		System.out.print("\n" + name + " Average = " + percent.format(avgScore/100)
    	    		+ "\n" + "Lowest Score :" + smallest + "\n" + "Highest Score: " + largest);
     
    		writeFile.close();
    		out.close();
     
    		readFile.close();
        		in.close();
        	} catch (FileNotFoundException e) {
    			System.out.println("File does not exist or could not be found.");
    			System.err.println("FileNotFoundException: " + e.getMessage());
    		} catch (IOException e) {
    			System.out.println("Problem reading file.");
        		System.err.println("IOException: " + e.getMessage());
        	}
    	}
     
    }

  2. #2
    Junior Member
    Join Date
    Apr 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I build a max heap java class for an object?

    I am trying to build a max heap java class for item objects, so I can solve the knapsack problem and implement greedy algorithms to do so. Item class includes weight, value, Id(to distinguish items), and most importantly a priority factor:

    public class Items {
    double weight;
    double value;
    double priorityFactor;
    int ID;

    //constructor for items class
    public Items(int weight, int value, int id, int priorityFactor)
    {
    this.weight=weight;
    this.value=value;
    this.ID=id;
    this.priorityFactor = priorityFactor;
    } //end constructor



    Now the issue that I'm facing is in the max heap class, which is supposed to build me a max heap tree based on the priority factor. Meaning that I should have an array of item objects: private Items[] array

    What I'm finding it difficult to implement is how cam I insert those items in the array based on the priority factor. If I do this, I think I will be able to implement the greedy algorithms. How to handle this?

  3. #3
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: help with codoe

            try {
                /*--------------------------------------------------------------*/
                Scanner input = new Scanner(System.in);
                Scanner reader = new Scanner(new File("path"));
                BufferedWriter writer = new BufferedWriter(new FileWriter(new File("path")));
                double totalScore = 0, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
                /*--------------------------------------------------------------*/
     
                System.out.print("Enter your name: ");
                String name = input.next();
                writer.write(name+"\n");
                System.out.print("How many scores would you like to enter: ");
                int numScores = input.nextInt();
                System.out.println("Enter " + numScores + " class score: ");
                for(int i = 0; i < numScores; i++) {
                    double score = input.nextDouble();
    		totalScore += score;
    		writer.write(score+"\n");
                }
                writer.close();
                double average = totalScore/numScores;
                name=reader.nextLine();
                System.out.println(name);
                for(int i = 0; i < numScores; i++) {
                    double num = Double.parseDouble(reader.next());
                    max = max < num ? num : max;
                    min = min > num ? num : min;
                }
                System.out.println("\n" + name + " Average = " + average + "\n" + "Lowest Score :" + min + "\n" + "Highest Score: " + max);
        	} catch (FileNotFoundException e) {
                System.out.println("File does not exist or could not be found.");
                System.err.println("FileNotFoundException: " + e.getMessage());
            } catch (IOException e) {
                System.out.println("Problem reading file.");
                System.err.println("IOException: " + e.getMessage());
        	}


    --- Update ---

    what is the priority factor?