Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java cant read my file, only showing \"No such file was found\" import java.io.F

ID: 3873194 • Letter: J

Question

Java cant read my file, only showing "No such file was found"

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.*;

public class Artists

{

private String artistName;

private int artistID;

/** constructor for Artist*/

public Artists(int ID, String artists)

{

setArtistID(artistID = ID);

setArtistName(artistName = artists);

}

/** toString method */

public String toString()

{

return getArtistID() + " " + getArtistName();

}

/** getter for artist name*/

private String getArtistName()

{

return artistName;

}

/** setter for artist name*/

private void setArtistName(String newName)

{

artistName = newName;

}

/** getter for artist ID*/

private int getArtistID()

{

return artistID;

}

/** setter for artist ID*/

private void setArtistID(int newID)

{

artistID = newID;

}

public static void main(String[] args)

{

ArrayList<Artist> myList = new ArrayList<Artist>();

int artistCounter = 0;

String[] artistNameArray = new String[116];

try

//File file = new File("plartists.txt");

//Scanner input = new Scanner(file);

{

Scanner input = new Scanner(new File("p1artists.txt"));

File Myfile = new File("p1artists_out.txt");

PrintWriter output = new PrintWriter(Myfile);

int i = 0;

while (input.hasNext())

{

artistCounter++;

try

{

int artistID = input.nextInt();

String artistName = input.next();

artistNameArray[i] = artistName;

output.println(artistID + " " + artistName);

i++;

}

catch (InputMismatchException e)

{

input.next();

}

}

output.close();

input.close();

}

catch (FileNotFoundException e)

{

System.out.println("No such file was found");

}

/**Write all to new file*/

int j = 0;

int artCounter = 0;

int totalAppraised = 0;

  

try

{

Scanner input = new Scanner(new File("p1arts.txt"));

File File = new File("p1arts_out.txt");

PrintWriter output = new PrintWriter(File);

while (input.hasNext())

{

try

{

String[] splitArray = input.nextLine().split(" ");

output.printf("%s %s %s %s %s ", splitArray[0], splitArray[1],

splitArray[2], artistNameArray[j], splitArray[3]);

totalAppraised += Integer.parseInt(splitArray[3]);

j++;

artCounter++;

}

catch (InputMismatchException e)

{

input.next();

}

}

output.close();

input.close();

}

catch (FileNotFoundException e)

{

System.out.println("No such file was found");

}

/**Totals*/

int finalArtistCount = artistCounter-1;

System.out.println(" Total artists: " + finalArtistCount);

System.out.println("Total amount of artwork: " + artCounter);

System.out.println("Total appraised value of all artwork: $" + totalAppraised);

}

}

-----------------------------------------------------------------------------------------------

This is my project

Description:

1.) In this part, we are going to apply the concepts of Bag Interface to Online Shopping Cart. If you examine the Listing 1-2 program, you will find that the program is not complete and the type Item is missing.

2.)In designing interactive application like shopping activities, it is desirable to have the test data stored in a file to speed up the testing time. Therefore, to prepare for the future project, we need to get used to file I/O in Java.

3)There is a tab-delimited text file named “p1artists.txt” which is to be used as an input file. Each record consists of the following two fields:

a) artistID: integer.

b) artistName: String;

Assignment:

A) Refer to the sample program of Listing 1-2 on pages 41 and 42, instead of Item class, implement the Artist class that contains the following:

1.) The 2 fields mentioned above in “p1artists.txt” file.

2.)The Constructor that accepts the 2 fields when the record of an artist is created.

3.) Getters and setters for the 2 fields.

4.) The toString() method.

5. )Test the program with the following data.

ArtistID

Artist Name

1

Acconci

2

Ames

3

Aserty

4

Baron

5

Battenberg

B) Modify the above program according to the following:

1.) Read the input file “p1artists.txt” instead of hardcoding the values.

2.) Use the exception handler to ensure that artist ID is numeric.

3.) If the input is incorrect, print the line number and its contents, but do not stop.

4.) Send the output to a text file named “p1artists_out1.txt”.

C) Enhance the above program for the following:

1.) A tab-delimited text file named “p1arts.txt” contains the following record formats:

ArtID

Title

ArtistID

Appraised Value

1001

Red Rock Mountain

50

18000

1002

Offerings

52

10000

1003

Spring Flowers

12

2400

2.) Assume that this file contains no error. You are going to read “p1artists.txt” into an array, and then process “p1arts.txt” to add the artist name to the output. Name this output file “p1arts_out.txt”. Example follows:

ArtID

Title

Artist

ArtistID

Appraised Value

1001

Red Rock Mountain

Mogan

50

18000

1002

Offerings

Novarre

52

10000

1003

Spring Flowers

Chico

12

2400

3.)Use an exception handler if the ArtistID does not exist in the “p1artists.txt” file. However, you still need to write this record to the output file.

4.)In the end, include the total number of the Art Works and Artists, and the total appraised value.

---------------------------------------------------------------------------------------------------------------------------------------

Listing 1-2 CODE

public class OnlineShopper

{

public static void mian (String[] args)

{

Item[] items = { new Item("Bird feeder", 2050),

new Item("Squirrel guard", 1547),

new Item("Bird bath", 4499),

new Item("Sunflower seeds", 1295)};

BagInterface<Item> shoppingCart = new Bag<>();

int totalCost = 0;

for (int index = 0; index < items.length; index++)

{

Item nextItem = items[index];

shoppingCart.add(nextItem);

totalCost = totalCost + nextItem.getPrice();

}

while (!shoppingCart.isEmpty())

System.out.println(shoppingCart.remove());

System.out.println("Total cost: " + " $" + totalCost / 100 + " . " + totalCost % 100);

}

}

Sample Output

Sunflower seeds   $12.95

Bird Bath $44.99

Squirrel guard $15.47

Bird feeder $20.50

Total cost: $93.91

------------------------------------------------------------------------------------------------------------------------------------------------------

p1arts.txt

---------------------------------------------------------------------------------------------------------------------------

p1artist.txt

---------------------------------------------------------------------------------------------------------------------------------------

p1artists-full.txt

ArtistID

Artist Name

1

Acconci

2

Ames

3

Aserty

4

Baron

5

Battenberg

Explanation / Answer

NOTE: Added a catch for IndexOutOfBound for missing data in the input file.

For File read , if you are working with Netbeans then you should place your text files in your project folder, For example: If your project name is ArtistFinder then your text files should be in the folder root project folder named ArtistFinder

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

public class Artists
{
private String artistName;
private int artistID;

/** constructor for Artist*/
public Artists(int ID, String artists)
{

setArtistID(artistID = ID);
setArtistName(artistName = artists);
}

/** toString method */
public String toString()
{
return getArtistID() + " " + getArtistName();
}

/** getter for artist name*/
private String getArtistName()
{
return artistName;
}

/** setter for artist name*/
private void setArtistName(String newName)
{
artistName = newName;
}

/** getter for artist ID*/
private int getArtistID()
{
return artistID;
}

/** setter for artist ID*/
private void setArtistID(int newID)
{
artistID = newID;
}
public static void main(String[] args)
{
ArrayList<Artists> myList = new ArrayList<Artists>();
int artistCounter = 0;
String[] artistNameArray = new String[116];
try

//File file = new File("plartists.txt");
//Scanner input = new Scanner(file);
{
Scanner input = new Scanner(new File("p1artists.txt"));
File Myfile = new File("p1artists_out.txt");
PrintWriter output = new PrintWriter(Myfile);
int i = 0;
while (input.hasNext())
{
artistCounter++;
try
{
int artistID = input.nextInt();
String artistName = input.next();
artistNameArray[i] = artistName;
output.println(artistID + " " + artistName);
i++;
}
catch (InputMismatchException e)
{
input.next();
}
}
output.close();
input.close();
}
catch (FileNotFoundException e)
{
System.out.println("No such file was found");
}

/**Write all to new file*/
int j = 0;
int artCounter = 0;
int totalAppraised = 0;
  
try
{
Scanner input = new Scanner(new File("p1arts.txt"));
File File = new File("p1arts_out.txt");
PrintWriter output = new PrintWriter(File);
while (input.hasNext())
{
try
{
String[] splitArray = input.nextLine().split(" ");
output.printf("%s %s %s %s %s ", splitArray[0], splitArray[1],
splitArray[2], artistNameArray[j], splitArray[3]);
totalAppraised += Integer.parseInt(splitArray[3]);
j++;
artCounter++;
}
catch (InputMismatchException e)
{
input.next();
}
catch (IndexOutOfBoundsException e){

}
}
output.close();
input.close();
}
catch (FileNotFoundException e)
{
System.out.println("No such file was found");
}

/**Totals*/
int finalArtistCount = artistCounter-1;
System.out.println(" Total artists: " + finalArtistCount);
System.out.println("Total amount of artwork: " + artCounter);
System.out.println("Total appraised value of all artwork: $" + totalAppraised);
}
}

PLEASE RATE !!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote