0

In the code bellow the string is created but it doesn't aske for the user input(the bold part), It behaves fine with next() method.

import java.io.File;

import java.util.*;

public class VirtualKey {

    public static void main(String[] args) {
    Scanner Sc = new Scanner(System.in);
    System.out.println("Welcome to the Calculator");
    File folder = new File("C:\\Users\\Alifa\\OneDrive\\Documents\\Practice projects");
      File[] listOfFiles = folder.listFiles();
    int u = 1;
    while(u==1) {
        System.out.println("Please select the oppration you want to perform"+"\r\n" 
            + "1.To see the list of all files." +"\r\n" 
            + "2.To perform oppretaion on a specific file." +"\r\n"
            + "0.Exit." );
        int c = Sc.nextInt();
      if(c==1) {
          Arrays.sort(listOfFiles);
          for (int i = 0; i < listOfFiles.length; i++) {
          System.out.println(listOfFiles[i].getName());
      }}
      else if(c==2) {
          while(u==1) {
                System.out.println("Please select the oppration you want to perform"+"\r\n" 
                    + "1.To add a file" +"\r\n" 
                    + "2.To Delete a file." +"\r\n"
                    + "3.To search a specific file."+"\r\n"
                    + "0.To go back to main context." );
                int a = Sc.nextInt();
                if(a==1) {
                    
                }
                else if(a==2) {
                    
                }
                else if(a==3) {
                    System.out.println("Please, give the file name that you want to find with right extention: ");
                    **String filename= Sc.nextLine();**
                    System.out.println(filename);
                    for (int i = 0; i < listOfFiles.length; i++) {
                        String str = listOfFiles[i].getName();
                        if (str.equals(filename)) {
                            System.out.println("Your file has been found.");
                            break;
                        }
                        else if (i==listOfFiles.length-1) {
                            System.out.println("Your file not found. Please, make sure you entered correct file name with the right extention.");
                        }
                    }
                    
                }
                else if(a==0) {
                    break;
                }
                else {
                    System.out.println("Please select a valid option");
                }
      }}
      else if(c==0) {
          System.out.println("The program is closed.");
            break;
      }
      else {
            System.out.println("Please select a valid option");
        }

}
}}

I have added a Screenshot of the out put, can some please help me fix this.enter image description here

Federico klez Culloca
  • 24,336
  • 15
  • 57
  • 93

0 Answers0