• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# [java] 2 questions

## 11 posts in this topic

Greets, I''ve got a couple of questions that I hope you folks''ll be able to help me out with. First question: Is it possible to have a case statement use strings or chars? (i.e non numerics)? I know the docs say it needs a numeric value but is there a way to bypass this? Second question (for 10 points): Is it possible to have create docking frames with just plain ol java (i.e not swing or any other extra package)? If so, are there any tutorials, docs, samples, demos, anything that covers this and where can I find them? Thanks for playing who wants to be a... 10 point winner(?) *blink* *blink*... anyways if you have answers I would appreciate em. Thanks, Smoo
0

##### Share on other sites
First Q:
It is possible, but not really a case statement...
The easiest way I can think to do this is
I. Create an abstract class or interface for the function you want to call in the body of case
II. Put instances of the class into a Hashtable then get them out according to input

Second Q:
It is possible, but not easily accomplished from stuff that I have done. Ya can do it, but you would likely end up rolling your own implementation of it.
0

##### Share on other sites
Okay, thanks anon. For the first answer, well.. I have no clue how to make hashtables et. al. so I think I''ll just pass for now and use plain old if statements until I actually look at docs for hashtables. I didn''t even know where to start but that''s a place where I''ll look.

As for the second answer I believe I will take your advice and leave that one alone for now too. It''s not critical that I have a docking windows and an incredibly fancy GUI, this is but a lowly map editor that I''ve been working on for myself (and others when it''s done).

I have a third question that comes up now that I have found very little helpful documentation for. Basically I have:
File file1 = new File("Maps/");
String stArray[] = file1.list();
but I want to have a filter for *.txt files. How could I accomplish this? I have looked into a lot of crap which resulted in.. well crap. As I said, the docs were useless. Just a small little code snipet would have been great but that''s one thing that''s lacking unfortunately.

So if anyone could explain, show a small code sample on how to create a filter for something such as *.txt, I would appreciate it alot.

Thanks,
Smoo
0

##### Share on other sites
you could use integer constants. Something like this:

public static final int NORTH = 1;
public static final int SOUTH = 2;
public static final int WEST = 3;
public static final int EAST = 4;

ok so say these variables are declared somewhere in class h. h could be the class related to the thing or just a class simply to define these things for all your classes.

so you do your case statement but instead of putting say 3, you''d put for case h.WEST, which happens to be 3. So say this is a umm wind simulation. Instead of setting the wind to south using a string you''d just set it to h.SOUTH, then next time your case thing reads it everything works out. You don''t even need to remember which word equals which number.
0

##### Share on other sites
You can use char''s in a case statement(I could swear) just if you
are writing the charachter put it in with the char markers around it case ''a'':
If you do want to know how to use Hashtables I could post how.

A easy way to filter files is to use the JFileChooser to find the file. It''s a open file GUI dialog box. Say something like
JFileChooser chooser = new JFileChooser();chooser.setCurrentDirectory(new File("."));chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { String name = f.getName().toLowerCase(); return name.endsWith(".txt") // f.isDirectory(); } public String getDescription() { return "Text files"; } });int r = chooser.showOpenDialog(this);if(r == JFileChooser.APPROVE_OPTION){ String name = chooser.getSelectedFile().getAbsolutePath(); //name is the name of the file you selected}

Also what do you mean by docking windows. Do you mean when there''s other window inside the frame like in Photoshop when you
have multible images open?
0

##### Share on other sites
quote:
Original post by Icculus
A easy way to filter files is to use the JFileChooser to find

He said he doesn''t want to use Swing. If he was OK with Swing, the whole docking window thingie would be *much* easier.

Hashtables are simple mappings from key objects to value objects. I put in a key object and a value object, I then use the key object to lookup the value object. Keys must be unique, so you can''t put in duplicate keys. If you need to place multiple items in under one key, you can use another collection type that supports duplicate keys, or you can use an array object or collection as the value object, so your key lookup gives you an array of objects.

java.util.HashTable ht = new java.util.HashTable();ht.put(new Integer(10),"Yo");ht.put(new Double(10.10),"Qiero");ht.put("Cool","Tacobell");System.out.println(ht.get(new Integer(10));System.out.println(ht.get(new Double(10.10));System.out.println(ht.get("Cool"));

Well, you get the idea.

In context, AP is probably suggesting you create the hashtable, placing Integer objects with your Strings as keys. You would use the String to look up the Integer, get the int value of the Integer, then switch on that. Integer constants would be simpler, but less flexible. You might try using polymorphism or evaluation classes to accomplish the same thing.

Choices[] desc = new Choices[10];//stuff array hereboolean foundOne = false;int i=0;for (;((i  foundOne=desc.evaluate(inputString);}//if foundOne is true, i = number of choice picked//another option is to subclass the choices and override//some execute method then call it if evaluate is true.

ManaSink
0

##### Share on other sites
quote:
Original post by Anonymous Poster

[quote] Original post by Icculus
A easy way to filter files is to use the JFileChooser to find

He said he doesn''t want to use Swing. If he was OK with Swing, the whole docking window thingie would be *much* easier.

Hashtables are simple mappings from key objects to value objects. I put in a key object and a value object, I then use the key object to lookup the value object. Keys must be unique, so you can''t put in duplicate keys. If you need to place multiple items in under one key, you can use another collection type that supports duplicate keys, or you can use an array object or collection as the value object, so your key lookup gives you an array of objects.

java.util.HashTable ht = new java.util.HashTable();ht.put(new Integer(10),"Yo");ht.put(new Double(10.10),"Qiero");ht.put("Cool","Tacobell");System.out.println(ht.get(new Integer(10));System.out.println(ht.get(new Double(10.10));System.out.println(ht.get("Cool"));

Well, you get the idea.

In context, AP is probably suggesting you create the hashtable, placing Integer objects with your Strings as keys. You would use the String to look up the Integer, get the int value of the Integer, then switch on that. Integer constants would be simpler, but less flexible. You might try using polymorphism or evaluation classes to accomplish the same thing.

Choices[] desc = new Choices[10];//stuff array hereboolean foundOne = false;int i=0;for (;((i<desc.length)&&(!foundOne));i++){  foundOne=desc.evaluate(inputString);}//if foundOne is true, i = number of choice picked//another option is to subclass the choices and override//some execute method then call it if evaluate is true.

ManaSink

0

##### Share on other sites
Well, you guys have alot of input here and I''m happy about that. It looks like docking will just have to wait for another time. I will also look into hashtables later, right now I have my faithful if statements.

Getting back to the file filters though, Icculus, as ManaSink mentioned I am not using swing but I am also not looking to use filters for a dialog box. It will be something that I want to apply to this:
FilenameFilter filter = new FilenameFilter()
File file1 = new File("Maps/");
String stArray[] = file1.list(filter);

I want *.txt to go in the filter. If someone knows how to do that, I would appreciate it greatly.

I apparently cannot do
FilenameFilter filter = new FilenameFilter();
It gives errors claiming the FilenameFilter is an interface.

If I just have
FilenameFilter filter;
filter.accept(stuff);
it says filter hasn''t been initialized yet.

Well, if anyone can demystify this stuff for me, I would very much appreciate it.

Thanks,
Smoo
0

##### Share on other sites
Smoo,

I posted to your last question about filename filters. Maybe you didn''t see it because i was the last one to post to it.
http://www.gamedev.net/community/forums/topic.asp?topic_id=15611&forum_id=24&Topic_Title=List+dirs&forum_title=Java+Development&M=False&S=True
That''s it. Basically what you have to do is have your class implement the interface FilenameFilter, then you need to override one method
boolean accept(File f)
just return true if it ends in .txt or false otherwise.

for the question about docking frames, is that where if the frame or window is within a certain distance from another it will ''stick'' to it? Because AWT uses native components, you will probably not be able to detect the dragging of an outermost container. But you can mess around with java.awt.event.MouseMotionListener.

If you don''t want to use many if statements, is it possible to invoke methods based on the string or char input? for example, you have a string of value "Employee" and a method "processEmployee", you can do the following

java.lang.reflect.Method m = getMethod("process"+ string,this);
m.invoke(this,null);

or something.
0

##### Share on other sites
I haven't worked with tyhe file system but, It seems to me that:
File file1 = new File("Maps/");String stArray[] = file1.list();

stArray[] contains a text array of file names...
couldn't you just make a loop that does something like this...
File file1 = new File("Maps/");String stArray[] = file1.list();int counter = stArray.length;int counter2 = 0;String stArray2[] = new String[counter];for(int x = 0; x < counter; x++){   if(stArray[x].endsWith(".txt")){     stArray2[counter2] = stArray[x];     counter2++;    }}

This should fill a second array with only the files that end in .txt

War doesn't determine who is right, war determines who is left.

Edited by - Wrathnut on June 28, 2000 11:13:04 AM
0

##### Share on other sites
Sure you can pass chars to a switch. I just made a sameple project confirming this. Heres the code:

#include

void main()
{
char letter = NULL;

cout << "Enter a letter." << endl;
cin >> letter;

switch(letter)
{
case ''a'': cout << "Hello" << endl; break;
case ''b'': cout << "Bye Bye" << endl; break;

default: cout << "I dont understand" << endl; break;
}
}

I think you can also pass strings and in the case part you would do this case "string": but i dont remember if i have ever done that or not.

-Snyper

0

##### Share on other sites
Not too shabby Snyper, but he''s doing his proggie in java

War doesn't determine who is right, war determines who is left.
0