Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Jcombobox And File Handling !

combobox

  • Please log in to reply
32 replies to this topic

#13 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 14 April 2012 - 08:46 AM

What if i use a database instead? Will it be easier to add and retrieve data then(in terms of the code i have to write)? and what datatype should i use for the textarea's text i store in the database(one that has high character limit!)
  • 0

#14 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 14 April 2012 - 08:59 AM

A database might be easier, depending on how complicated of a project you're making. If all you want to do is read a list of objects, perhaps flat files would be easier. However, if you're eventually going to be performing queries, sorts, that sort of thing, on your data, perhaps databases would be easier.

Strings are stored as a varchar in MySQL (varchar2 in Oracle, which is what I use). Those are the two databases I am familiar with.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#15 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 14 April 2012 - 09:14 AM

No all i want to do is read a list of files! And now that you've said that! My original has still not been solved. I still don't know how to update the switch's case. And if switch cannot be used! What is the alternative?
  • 0

#16 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 14 April 2012 - 09:23 AM

Like I explained before, one option is a List<String> and a HashMap<String, ___>. It depends on what you want to do with the list item once selected. You haven't shown us any of your code yet, so I can't say what you should do.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#17 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 14 April 2012 - 01:28 PM

Can you post what your switch MIGHT look like with a few cases?
Example:
switch( ... ) {
   case ? : ... ;
   case ? : ... ;

  • 0

#18 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 15 April 2012 - 07:10 AM

String ch=a.next();
switch(ch){
case abc:
File f= new File("C:/abc.txt");
FileReader fr=new FilerReader(f);
BufferedReader br= new BufferedReader(br);
String str="";
while(br.readLine()!=null)
str=str+"\n" + br.readLine();
jtextArea1.setText(str);


case def:
File f= new File("C:/def.txt");
FileReader fr=new FilerReader(f);
BufferedReader br= new BufferedReader(br);
String str="";
while(br.readLine()!=null)
str=str+"\n" + br.readLine();
jtextArea1.setText(str);

}
  • 0

#19 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 15 April 2012 - 07:21 AM

Are those two case statements exactly the same except for the String with the filename?

Should the case values be in "s? You have coded them as variables.
case abc:
vs
case "abc":

Looks like a good place to use the HashMap as suggested earlier by gregwarner.
  • 0

#20 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 15 April 2012 - 07:52 AM

I think the problem is that you're trying to hardcode all of the text file names into your program.
You need your program to be dynamic and be able to accept all types of file names.

Example:
println("Enter the file you'd like to open");
String filename = getUserInput();
if( filename exists ) {
	methodName( filename );
} else {
  println("that file doesn't exist");
}

You can break redundancy in your code by using methods.

public void methodName(String filename) {
   File f= new File(filename);
   FileReader fr=new FilerReader(f);
   BufferedReader br= new BufferedReader(br);
   String str="";
   while(br.readLine()!=null)
	  str=str+"\n" + br.readLine();
   jtextArea1.setText(str);
}

  • 0

#21 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 16 April 2012 - 05:30 AM

String ch=a.next();
switch(ch){
case abc:
File f= new File("C:/abc.txt");
FileReader fr=new FilerReader(f);
BufferedReader br= new BufferedReader(br);
String str="";
while(br.readLine()!=null)
str=str+"\n" + br.readLine();
jtextArea1.setText(str);


case def:
File f= new File("C:/def.txt");
FileReader fr=new FilerReader(f);
BufferedReader br= new BufferedReader(br);
String str="";
while(br.readLine()!=null)
str=str+"\n" + br.readLine();
jtextArea1.setText(str);

}


Both of those blocks of code are the same. All you have to do is write it once and use the string to construct the filename.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#22 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 16 April 2012 - 07:22 AM

Yes it will be same for all the cases!But how do i check if a file exists?
  • 0

#23 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 16 April 2012 - 07:23 AM

The File class has a method that will do that: exists()
  • 0

#24 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 16 April 2012 - 08:16 AM

You guys are awesome!!!! Thank you so much!!! :D :D \m/
I got exactly what I wanted!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download