views:

38

answers:

3

Okay I'll try to be direct. I am working on a file sharing application that is based on a common Client/Serer architecture. I also have HandleClient class but that is not particularly important here.

What I wanna do is to allow users to search for a particular file that can be stored in shared folders of other users. For example, 3 users are connected with server and they all have their respective shared folders. One of them wants to do a search for a file named "Madonna" and the application should list all files containing that name and next to that file name there should be an information about user(s) that have/has a wanted file. That information can be either username or IPAddress. Here is the User class, the way it needs to be written (that's how my superiors wanted it):

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class User {

    public static String username;
    public static String ipAddress;

    public User(String username, String ipAddress) {

        username = username.toLowerCase();
        System.out.println(username + " " + ipAddress);
    }

    public static void fileList() {

        Scanner userTyping = new Scanner(System.in);
        String fileLocation = userTyping.nextLine();
        File folder = new File(fileLocation);
        File[] files = folder.listFiles();

        ArrayList<String> list = new ArrayList<String>();

        for (int i = 0; i < files.length; i++) {

            list.add(i, files[i].toString().substring(fileLocation.length()));
            System.out.println(list.get(i));

        }

    }

    public static void main(String args[]) {
        System.out.println("Insert the URL of your shared folder");
        User.fileList();

    }


}

This class stores attributes of a particular user (username, IPAddress) and also creates the list of files from the shared folder of that particular user. the list type is ArrayList, that's how it has to be, again, my superiors told me to.

On the other hand I need another class that is called RequestForFile(String fileName) whose purpose is to look for a certain file within ArrayLists of files from all users that are logged in at the moment of search.

This is how it should look, and this is where I especially need your help cause I get an error and I can't complete the class.

import java.util.ArrayList;


public class RequestForFile {

    public RequestForFile(String fileName) {

        User user = new User("Slavisha", "84.82.0.1");
        ArrayList<User> listOfUsers = new ArrayList();
        listOfUsers.add(user);

        for (User someUser : listOfUsers) {

            for (String request : User.fileList()) {
                if (request.equals(fileName))
                    System.out.println(someUser + "has that file");
            }

        }

    }

}

The idea is for user to look among the lists of other users and return the user(s) with a location of a wanted file. GUI aside for now, I will get to it when I fix this problem. Any help appreciated. Thanks I'm here to answer anything regarding this matter.

A: 

in the "line 14: for (String request : User.fileList()) {" I get this error: "void type not allowed here" and also "foreach not applicable to expression type"

You need to let User.fileList() return a List<String> and not void.

Thus, replace

public static void fileList() {
    // ...
}

by

public static List<String> fileList() {
    // ...
    return list;
}

To learn more about basic Java programming, I can strongly recommend the Sun tutorials available in Trials Covering the Basics chapter here.

BalusC
thank you. I know the basics but I have certain holes in my knowledge. Hope to get better as the time goes on.
AmateurProgrammer
A: 

There are lots of problems here such as:

I don't think that this code can compile:

         for (String request : User.fileList()) 

Because fileList() does not return anything. Then there's the question of why fileList() is static. That means that all User objects are sharing the same list. I guess that you have this becuase you are trying to test your user object from main().

I think instead you should have coded:

 myUser = new User(...);
 myUser.fileList()

and so fileList could not be static.

You have now explained your overall problem more clearly, but that reveals some deeper problems.

Let's start at the very top. Your request object: I think it represents one request for one user with one file definition. But it needs to go looking in the folders of many users. You add the the requesting user to a list, but what about the others. I think that this means that you need another class responsible for holding all the users.

Anyway lets have a class called UserManager.

 UserMananger{

      ArrayList<User> allTheUsers;

       public UserManager() {

       }

       // methods here for adding and removing users from the list

       // plus a method for doing the search

       public ArrayList<FileDefinitions> findFile(request) [
               // build the result
       }

 }
djna
Thanx, this made it a lot easier for me. I still can't get around the surch function to work properly. I will try to figure out better solution or ask question differently
AmateurProgrammer
A: 

It looks like you're getting that error because the fileList() method needs to returns something that can be iterated through - which does not include void, which is what that method returns. As written, fileList is returning information to the console, which is great for your own debugging purposes, but it means that other methods can't get any of the information fileList sends to the console.

On a broader note, why is RequestForFile a separate class? If it just contains one method, you can just write it as a static method, or as a method in the class that's going to call it. Also, how will it get lists of other users? It looks like there's no way to do so as is, as you've hard-coded one user.

And looking at the answers, I'd strongly second djna's suggestion of having some class that acts as the controller/observer of all the Users.

char
I put RequestForFile as a separate class for now, so I could work easier. I will put it into UserManager class later, as a method. I agree, Djina's suggestion works great.Thank you also for your contribution.
AmateurProgrammer