Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

please solve the errors

+5 votes
asked Jun 9, 2021 by JAHANVI GIRIYA_042 (230 points)

import java.util.*; 

  

public class solution { 

    public static void main(String args[]) 

    { 

  

        // Get the file 

        File f = new File("F:\\program.txt"); 

  

        // Check if the specified file 

        // Exists or not 

        if (f.exists()) 

            System.out.println("Exists"); 

        else

            System.out.println("Does not Exists"); 

    } 

 

// Java program to demonstrate 

// exists() method of File Class 

  

import java.io.*; 

  

public class solution { 

    public static void main(String args[]) 

    { 

  

        // Get the file 

        File f = new File("F:\\program1.txt"); 

  

        // Check if the specified file 

        // Exists or not 

        if (f.exists()) 

            System.out.println("Exists"); 

        else

            System.out.println("Does not Exists"); 

    } 

}

2 Answers

+1 vote
answered Jun 9, 2021 by ATTA NIKHITHA (170 points)

import java.util.io;

import java.*;

public class solution{

public static void main(String args[]){

 File f = new File("F:\\program1.txt"); 

if(f.exists()){

System.out.println("Exists");

}
else{

system.out.println("Does not Exists");

}

}

}

commented Jun 9, 2021 by ATTA NIKHITHA (170 points)
If possible post the question
0 votes
answered Jun 10, 2021 by Peter Minarik (55,660 points)

Let me point out problems with your code:

  • You need to import java.io.File, before you can use the File class
  • Your class must go into a file where the file name matches the name of the class. If your class is called Solution, then it must go to Solution.java. Since the default file created by OnlineGDB is Main.java, either you rename your class Main, or you create a new file (left to the RUN button), call it Solution.java, move your code there into a new function and call it from Main.main. The first is simpler. ;)

Please find the fixed code below:

import java.io.File;

public class Main
{ 
    public static void main(String args[]) 
    { 
        File f = new File("F:\\program.txt"); 

        if (f.exists()) 
            System.out.println("Exists"); 
        else
            System.out.println("Does not Exists"); 
    } 
} 
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...