I need to use the valueOf method to return a string representation of a boolean arguement but I don't know how to use this method.

Here is my code at the moment, if the inputted file name ends with .txt then 'true' is returned, if not then 'false' is returned.
So i want to return a string instead of true and false using this method.

public static void main(String[] args) {

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

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

        suffix = firstin.endsWith(actual);

        System.out.println(suffix);
//may be you didn't notice that valueOf is a static method.
String suffix = String.valueOf(firstin.endsWith(actual));
//OR give you have only 2 values..
String suffix = (firstin.endsWith(actual))? "true" : "false" ;
//may be you didn't notice that valueOf is a static method.
String suffix = String.valueOf(firstin.endsWith(actual));
//OR give you have only 2 values..
String suffix = (firstin.endsWith(actual))? "true" : "false" ;

On your 4th line of code, that gives my IDE an error of incompatible types, required : boolean, found : java.lang.String

I'm curious. Why return a String rather than the boolean?

I need to use the valueOf method to return a string representation of a boolean arguement but I don't know how to use this method.

Here is my code at the moment, if the inputted file name ends with .txt then 'true' is returned, if not then 'false' is returned.
So i want to return a string instead of true and false using this method.

Hi here is how you can do it. I have just used BufferedReader instead of Scanner.

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class FileNameDesc 
{
   public static void main(String[] args)
   {
	   try
	   {
	     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	     
	     String output ="";
	     
	     System.out.println("Enter file name :");
	     String filename = br.readLine();
	     
	     System.out.println("Enter description :");
	     String description = br.readLine();
	     
	     int index = filename.indexOf(".");
	     String fileext = filename.substring(index+1);
	     
	     if(fileext.equalsIgnoreCase("txt"))
	     {
	    	output = String.valueOf(true);
	    	System.out.print(output + " ");
	    	System.out.println(description);
	     }
	     else
	     {
	    	 output = String.valueOf(false);
	    	 System.out.print(output + " "); 
	     }
	    
	   }
	   catch(Exception e)
	   {
		   e.printStackTrace();
	   }
	   
   }
}

If all you want to do is to print the boolean there's no need to convert to String - print does that for you. You can just use
System.out.print("My boolean is " + myBooleanVariable);

I'm curious. Why return a String rather than the boolean?

Sorry about not replyin earlier, the requirement is to return a specific sentence, rather than just 'true' or 'false'

In your post it says you need to use valueOf, however, if you aren't absolutely stuck on using that, you could probably use parseBoolean.

Once you get whether it is true or false you could take that boolean and use parseBoolean on it and then use that string to print out the sentence you need.

For example:

Boolean a; //your boolean
String result; //the string you want to represent the booleans value
a = Boolean.parseBoolean(result); //parsing from boolean to string
System.out.println("Your sentence here, plus the string's value is: " + result); //your output

Again, I'm not sure how useful this will be to you since it looks like you might have a requirement of using valueOf

If you need a sentence, I'd go with

String sentence = (myBooleanVar) ? "Sentence 1": "Sentence 2";

(like in post #2)

On your 4th line of code, that gives my IDE an error of incompatible types, required : boolean, found : java.lang.String

First: It's this OR that. Use the one that works.
Second: I tested the code before posting. Checked again, it compiles fine with 1.5 & 1.6.

Unless you changed the type of "suffix" to boolean in ur code I don't see why you should get it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.