Forum Stats

  • 3,740,363 Users
  • 2,248,239 Discussions
  • 7,861,216 Comments

Discussions

I need help with my code

User_RZVSJ
User_RZVSJ Member Posts: 1 Green Ribbon

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java.io.PrintWriter;

import java.util.Scanner;

 

public class MyFiles

{

    private static BufferedReader in;

 

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

    { 

        double max=0, min=10000000;

 

        Product p_min=new Product("",0.0,0);

        Product p_max=new Product("",0.0,0);

        File j = new File("in.txt");

 

        Scanner input = new Scanner(j);

        PrintWriter pw = new PrintWriter(new File("out.txt"));

        String line;          

        in = null;

        while((line=in.readLine())!=null)

        {

 

            String[] array=line.split(";");

            String name=array[0];

            double price=Double.valueOf(array[1]);

            int quantity=Integer.valueOf(array[2]);

            if(price>max)

            {

                max=price;

                p_max=new Product(name,price,quantity);

            }

            if(price<min)

            {

                min=price;

                p_min=new Product(name,price,quantity);

            }

        }

       pw.println("Product with maximum price is:"+p_max);

        System.out.println("Product with maximum price is:"+p_max);

      System.out.println("Product with minimum price is:"+p_min);

        pw.println("Product with minimum price is:"+p_min);

        input.close();

        pw.close();

      }

}

 

public class Product extends MyFiles {

 

        private String name;

        private double price;

        private int quantity;    

        public Product(String name, double price, int quantity)

        {

            this.name=name;

            this.price=price;

            this.quantity=quantity;

        }

        public Product(Product p)

        {

            p.name=name;

        

     p.price=price;

            p.quantity=quantity;

        }

        public String toString()

        {

            return name+" which has the price "+price+" and is present in a quantity of "+quantity;

        }

    }

Input file :

sugar;1.5;10


milk;4.5;100


grain;3.2;200



I got these errors :


Exception in thread "main" java.io.FileNotFoundException: in.txt (The system cannot find the file specified)

at java.base/java.io.FileInputStream.open0(Native Method)

at java.base/java.io.FileInputStream.open(FileInputStream.java:211)

at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)

at java.base/java.util.Scanner.<init>(Scanner.java:639)

at MyFiles.main(MyFiles.java:29)

Sign In or Register to comment.