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

in JAVA, why my code can run, but it does not show any files on my desktop. Scan

ID: 3739834 • Letter: I

Question

in JAVA, why my code can run, but it does not show any files on my desktop.

Scanner k = new Scanner(System.in);
System.out.println(" Please enter the first filename: ");
String fileName1 = k.nextLine();
System.out.println(" Please enter the second filename: ");
String fileName2 = k.nextLine();
File f1 = new File(fileName1);
Scanner file1 = new Scanner(f1);
PrintWriter f2 = new PrintWriter(fileName2);
  
while(file1.hasNext()){
String str= fileName1.toUpperCase();
f2.println(str.toUpperCase());
}
file1.close();
f2.close();
}

Explanation / Answer

I made some changes and succefully it is showing all the files on desktop.You can see the changes in following code

import java.util.*;

import java.io.*;

class FileDemo

{

public static void main(String args[])

{

try

{

Scanner k = new Scanner(System.in);

System.out.println(" Please enter the first filename: ");

String fileName1 = k.nextLine();

System.out.println(" Please enter the second filename: ");

String fileName2 = k.nextLine();

PrintWriter f1 = new PrintWriter(new File(fileName1));

PrintWriter f2=new PrintWriter(new File(fileName2));

Scanner file1=new Scanner(new File(fileName1));

String str;

while(file1.hasNext())

{

str= fileName1.toUpperCase();

f2.println(str.toUpperCase());

}

f1.close();

f2.close();

}

catch(Exception e1)

{

System.out.println(e1);

}

}

}