Issue
I am not able to understand why this type of error is coming.
I don't really understand this topic. I am doing this because it's my practical submission in my college.
The error shown in the output:
>PS E:\Vedang\Java_Package> cd "e:\Vedang\Java_Package\" ; if ($?) { javac FileRandomIntegers.java } ; if ($?) { java FileRandomIntegers }
>FileRandomIntegers.java:9: error: constructor File in class File cannot be applied to given types;
File inFile = new File("rand.dat");
^
> required: no arguments
> found: String
> reason: actual and formal argument lists differ in length
>FileRandomIntegers.java:13: error: no suitable constructor found for FileWriter(File)
FW = new FileWriter(inFile);
^
> constructor FileWriter.FileWriter(String) is not applicable
(argument mismatch; File cannot be converted to String)
> constructor FileWriter.FileWriter(java.io.File) is not applicable
(argument mismatch; File cannot be converted to java.io.File)
> constructor FileWriter.FileWriter(FileDescriptor) is not applicable
(argument mismatch; File cannot be converted to FileDescriptor)
>FileRandomIntegers.java:39: error: no suitable constructor found for Scanner(File)
Scanner scanner = new Scanner(inFile);
^
> constructor Scanner.Scanner(Readable) is not applicable
(argument mismatch; File cannot be converted to Readable)
> constructor Scanner.Scanner(InputStream) is not applicable
(argument mismatch; File cannot be converted to InputStream)
> constructor Scanner.Scanner(java.io.File) is not applicable
(argument mismatch; File cannot be converted to java.io.File)
> constructor Scanner.Scanner(Path) is not applicable
(argument mismatch; File cannot be converted to Path)
> > constructor Scanner.Scanner(String) is not applicable
(argument mismatch; File cannot be converted to String)
> > constructor Scanner.Scanner(ReadableByteChannel) is not applicable
(argument mismatch; File cannot be converted to ReadableByteChannel)
>Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors
Code:
import java.io.*;
import java.util.*;
class FileRandomIntegers
{
public static void main(String args[])
{
FileWriter FW = null;
BufferedWriter bfwr = null;
File inFile = new File("rand.dat");
try
{
int rand_int;
FW = new FileWriter(inFile);
bfwr = new BufferedWriter(FW);
Random generate_rand = new Random();
System.out.println("\nGenerating & Storing Random integers from 0 to 99..\n");
for(int i=0; i<10; i++)
{
rand_int = generate_rand.nextInt(100);
System.out.print(rand_int+"\t");
bfwr.write(rand_int+"\r\n");
}
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
bfwr.close();
}
catch(IOException e){ }
}
try
{
System.out.println("\nRetrieving Random integers stored in file 'rand.dat'..");
Scanner scanner = new Scanner(inFile);
while(scanner.hasNextInt())
{
System.out.println(scanner.nextInt()+"\t");
}
}
catch(NullPointerException e)
{
System.out.println(e.getMessage());
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}
Solution
Here are the results from one of my tests.
Generating & Storing 10 Random integers from 0 to 99.
86 33 63 1 36 97 20 82 25 9
Retrieving Random integers stored in file random.dat
86 33 63 1 36 97 20 82 25 9
I took your code and broke it into simpler methods that I could test individually.
I also used a BufferedReader
to read the file, since you used a BufferedWriter
to write the file.
Here's the complete runnable code I used.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class FileRandomIntegers {
public static void main(String[] args) {
new FileRandomIntegers().createAndProcessFile();
}
private Random random;
public void createAndProcessFile() {
this.random = new Random();
File file = new File("random.dat");
try {
writeFile(file);
readFile(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeFile(File file) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
System.out.println("Generating & Storing 10 Random integers "
+ "from 0 to 99.");
for (int index = 0; index < 10; index++) {
int number = random.nextInt(100);
System.out.print(number + " ");
writer.write(Integer.toString(number));
writer.write(System.lineSeparator());
}
System.out.println();
System.out.println();
writer.flush();
writer.close();
}
public void readFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
System.out.println("Retrieving Random integers stored in file " +
file.getName());
String line = reader.readLine();
while (line != null) {
System.out.print(line + " ");
line = reader.readLine();
}
System.out.println();
System.out.println();
reader.close();
}
}
Answered By - Gilbert Le Blanc Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.