Getting error on the string im trying to pass to my function call of Createnewfile in main

My only issue is that I get an error where I have commented. Im willing to send my strings (token[]) data at index 1 to the Createnewfile () method in the main. I can't achieve it. Please guide as to what I should do.

import java.util.*;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;


class CreateFile
{
    public static void main( String[] args )
    {   

    Scanner S = new Scanner (System.in);
    System.out.println ("FileExplorer");

    System.out.println ("Select:");
    System.out.println ("1.create new file");
    System.out.println("2. Enter text in the new created file ");
    System.out.println ("3.View List");
    System.out.println ("4.Display contents of particular file");
    System.out.println ("5.Delete a selected file");
    System.out.println ("6.Copy files to other directory");
    System.out.println ("7.Cut files from one directory to other");

    System.out.println ("My File Explorer:");
    String phrase = S.nextLine();
    String delims = "[ ]+";
    String[] tokens = phrase.split(delims);
    for (int i = 0; i < tokens.length; i++)
        System.out.println(tokens[i]);

    if (tokens[0].equals ( "createfile") )
    {
        System.out.println ("Create");
        Createnewfile( tokens[] );    //I get an error here, please tell me how to send 
                                                  //the tokens[1] index 1 string to my  createnewfile function
    }
    if (tokens[0].equals ( "entertext") )
    {
        System.out.println ("entering text");
        String St = tokens[1];
        Entertext(St);
    }
    if (tokens[0].equals ( "showcontent") )
    {
        System.out.println ("Displaying contents");
    }
    if (tokens[0].equals ( "List") )
    {
        System.out.println ("Listing all .txt files");
    }
    if (tokens[0].equals ( "delete") )
    {
        System.out.println ("Deleting the selected file");
    }

}

public void Createnewfile( String [] tokens)
{
    try 
        {

          File file = new File(tokens[1]);

          if (file.createNewFile())
          {
            System.out.println("File is created!");
          }
          else
          {
            System.out.println("File already exists.");
          }

        }
        catch (Exception e) 
        {
          e.printStackTrace();
        }
}


public static void Entertext(String St)
{
    try     
        {
            Scanner S = new Scanner (System.in);
            String content = S.nextLine();
            File file = new File( St);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Jon Skeet
people
quotationmark

If you're trying to the array reference, just lose the []:

Createnewfile(tokens); 

That's not just passing tokens[1], however.

It's very odd that your Createnewfile method always uses tokens[1] - it would make more sense for that method to have a String parameter (instead of String[]) and then you could call it as:

Createnewfile(tokens[1]);

to pass just a reference to the string you want to use to create the file - just like you do for your Entertext method, for example.

I would also strongly urge you to learn about the naming conventions in Java, and follow them. (So your methods would be createNewFile, enterText etc.)

people

See more on this question at Stackoverflow