I need to create a program that uses the String classes to read the suffix .txt and if true, return the sentence inputted, but if false return another sentence.
I'm unsure of which boolean method I will need to use to execute this. Can anyone help me? Here is my code so far:

public static void main(String[] args) {

        String firstin, secondin, actual = ".txt";
        boolean suffix;
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter file name and description: ");
        firstin = scan.next();
        secondin = scan.next();

        suffix = firstin.endsWith(actual);
  

        System.out.println(suffix);

How can i return something other than true if true and false if false. For example, if the suffix is .txt, true is returned but i want to replace 'true' with another string visa versa with false.

Edited 6 Years Ago by anevins: n/a

You could use the split method to split the filename on the period (.) then check what the latter part of the splitted content contains you could behave accordingly say return "Text File" if it contains .txt and "Non-text File" otherwise.

You cannot return anything apart from true/false from a method that returns a boolean after all thats what it is supposed to do, if you need to return multiple values say "Text File", "Executable file", "Image File" then why not return a String ?

You could use the split method to split the filename on the period (.) then check what the latter part of the splitted content contains you could behave accordingly say return "Text File" if it contains .txt and "Non-text File" otherwise.

You cannot return anything apart from true/false from a method that returns a boolean after all thats what it is supposed to do, if you need to return multiple values say "Text File", "Executable file", "Image File" then why not return a String ?

Which string method returns endsWith(); value? I've checked the methods but can't find it.

Go through the javadocs, endsWith() is a method not a return value. Also I suggested you using the split method. Read the javadocs for the String class.

This article has been dead for over six months. Start a new discussion instead.