The Artima Developer Community
Sponsored Link

Java Answers Forum
incompatiple types how to sort it, help pls

3 replies on 1 page. Most recent reply: Dec 9, 2004 8:55 PM by faraday

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
faraday

Posts: 3
Nickname: sharmrkf
Registered: Nov, 2004

incompatiple types how to sort it, help pls Posted: Nov 24, 2004 2:25 PM
Reply to this message Reply
Advertisement
Hi every 1, iam newbie trying to fix this prog. but got stack,

import java.io.*;
import java.io.File.*;

public class LocateAllFiles {


public static void main (String args[]) {

File alldrives[] = File.listRoots();

for (int k=0; k<alldrives.length; k++){

if (alldrives[k].exists()) {

String skdrive="";

if (alldrives[k]==skipDrive(skdrive))){

System.out.println (alldrives[k]+" Available");

File files[] = alldrives[k].listFiles();

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

System.out.println(files[i]);
};
}
else

System.out.println (alldrives[k]+" Not Available.");

}
}
//Now skip any Drive you want

private static String skipDrive(String skd){

skd = "A";
return skd;
}

//skip any file or folder you like
private static String [] skipFile(String [] skfldr){

String skippedfol [] = {"Program Files","java","My Documents"};

skfldr = skippedfol;

return skfldr;
}
}

what i am trying is to control which drive or file to display after creating this program which works fine;

import java.io.*;
import java.io.File.*;

public class LocateAllFiles {

public static void main (String args[]) {

File AllRoots[] = File.listRoots();

for (int k=0; k<AllRoots.length; k++){

if (AllRoots[k].exists()) {

System.out.println (AllRoots[k]+" Accessible");

File files[] = AllRoots[k].listFiles();

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

System.out.println(files[ i ]);
}

else System.out.println (AllRoots[k]+" NOT Accessible.");
}
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: incompatiple types how to sort it, help pls Posted: Nov 25, 2004 12:14 AM
Reply to this message Reply
Your have 2 essentional errors:

1.
alldrives[k] is a File object.
skipDrive(skdrive) delivers a String object.
== will never work this way.
To solve this problem, you have to compare the String with:
alldrives[k].getName()
or
alldrives[k].getPath()
Since getName and getPath will deliver a valid drive name and not just the drive letter, you will have to extract the drive letter from the String. substring (0,1) should do the job.


2.
String s1 = "abcd";
String s2 = "abcd";
boolean b = s1 == s2;

b will allways be false.
Why? s1==s2 compares the Object, not it's content. These are 2 different objetcs with the same content.
The solution is:
s1.equals(s2)


3. (not essential)
You should use the Java naming convention.
AllRoots[] should be called allRoots[], because it's not a class, but a variable.


4. Want to compare the drive letters directly?
Use the datatype char (not Character).
Since it is a primitive type (like int, double, float, boolean, ...) instead of an Object, the content get's compared.

How to?

File drive = ...;
char nameChar f.getPath().charAt(0);

Change your skipDrive Method to
public static char skipDrive
and return the char'A'

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: incompatiple types how to sort it, help pls Posted: Nov 25, 2004 12:27 AM
Reply to this message Reply
Should be
File drive = ...;
char nameChar = drive.getPath().charAt(0);


of course, sorry.

faraday

Posts: 3
Nickname: sharmrkf
Registered: Nov, 2004

Re: incompatiple types how to sort it, help pls Posted: Dec 9, 2004 8:55 PM
Reply to this message Reply
Thanx,

Flat View: This topic has 3 replies on 1 page
Topic: Keeping layout in synch with screen resolution Previous Topic   Next Topic Topic: Command line arguments - Printing out one string

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use