Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What do I need to do?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Thanked 0 Times in 0 Posts

    Default What do I need to do?

    program requests and reads a list of file names (one per line). Program ends when a blank line is entered.

    If a file name endsWith ".txt" then print "Text File: xxxx" where xxxx is the full file name. If the file name contains the string "del" print "Delete: xxxx". If the file satisfies both conditions just do the text file option. Otherwise do not print anything.

    So far I have done this but I can't figure out how to do the part where you output Text file: if it contains both .txt and del. Could anyone help me out?

    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    package assignment2;

    import java.util.Scanner;

    * @author Adam
    public class FileType {

    public static void main(String[] args) {

    String fileNames = "";

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter file names (blank line to end):");
    fileNames = scan.nextLine();

    while (fileNames.contains(".txt")) {

    System.out.println("Text File:" + fileNames);
    fileNames = scan.nextLine();

    while (fileNames.contains("del")) {
    System.out.println("Delete:" + fileNames);
    fileNames = scan.nextLine();




  2. #2
    Forum VIP
    Join Date
    Oct 2010
    My Mood
    Thanked 54 Times in 47 Posts
    Blog Entries

    Default Re: What do I need to do?

    Why are you using 2 while loops? You might want to consider looping through the list only once and doing an if-elseif statement, which would (if ordered correctly) do exactly what you want. Also, you are not acquiring the input as your System.out.println would imply, for that you are going to have to use another loop.

    Also, wrap your like: [code]*Code Here*[/code]