Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

this code doesnt work probably with gui  RSS feed

 
youssef ali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the error or problem you are experiencing?

One "new" thing I learned a few days ago--don't use Scanner with a File object, get the Path object from the file and use it. If you cannot get it to read, then that is probably your problem.
 
youssef ali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually my problem with while loop it just looping one time and then the program freezes
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to simplify your reading. You have several reads from your Scanner that are not checked before the read--hasNext()--so any of those could be causing your problem or using scanner with a File object. The "while" in and of itself is not a problem, but what you are doing in it, definitely is. clean it up and do checks on the data BEFORE you read it and post any error messages you may get.

Also how do you know it actually makes it through that first iteration of the loop?

youssef ali wrote:actually my problem with while loop it just looping one time and then the program freezes
 
youssef ali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just made a file of one line and it saved it to the list but when adding more than one line in the file the program freezes and i already tried that code outside gui it worked very good so my problem is really in the while loop when i use gui with it and i tried swingworker it didnt work
 
youssef ali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and i tried to but data=s.next(); outside the loop to see if it will work but it didnt so mainly i was using the loop only without the code before it(data,x,g) so i knew that the loop looped once and the sequence of reading the file because it is actually like that ("name (male|female) gba")
 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:You need to simplify your reading. . . .
Agree. It looks as though you are trying to read both with a buffered reader and with a scanner simultaneously, though it is difficult to be certain because of the poor indentation in your code.
Use a scanner or a buffered reader but not both. Remember you can use a Scanner on the individual lines:-And welcome to the Ranch

You didn't use the code tags←link correctly, but don't worry. I corrected the tags.
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Don't declare different variables on the same line, that creates unnecessary mess, especially when there are no space left in between different statements. Following that logic you could go forward and cram everything to 1 line only, that is doable I think too, but there is no point to save space of readability price. READABILITY is the most important. Your indentation is also terrible - you need to fix that. If you use IDE - look up on Google for indentation and formatting options keystrokes. If you don't use IDE, most of nowadays text editors has auto indentation too - you might just need to enable it (Google should be handy here too).

After all, don't use File along with Scanner class. Instead, use Path (you'll need to do some import statements), otherwise in case you're using some non standard files you could run into problems - some of us in this thread know that already.
i.e.:

add_file(). Where did you get that method naming convention from? Looks similar to Python, some other languages too. Look up for Java coding conventions (<- link). That document is from 1999, but techniques are still widely applied - so, in case you forgot them, please go through document and try to improve your code.
 
youssef ali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry i am beginner but all those solutions aren't working so can anyone give me an alternate code that reads a file then sends it to the method without freezing the GUI
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
youssef ali wrote:i am sorry i am beginner but all those solutions aren't working
Please show us exactly which solutions and how you attempted to use. This time, please indent your code as suggested earlier, so everyone could read it.

@Les: sorry Les, I missed that part about your advise to OP not to use File along with Scanner, so I duplicated, apologies
 
youssef ali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i have afile that has string and float in this sequence(name float male)
i want to read it in to 3 variables and then add them to list using my method
 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what format is that file in? Is it a text file? Which encoding? Does it use the default encoding for your operating system?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Youssef,
I cleaned up your code so you can see what is going on, my comments are in-line in the code:

Looking at your code, there are a lot of things done that just do not work in the real world--you have to know what is there before you can read it. In all of your reads, you don't know and that is very very bad. If you are going to use a Scanner object, along with another form of input, you still have to be sure both have data waiting and that the data is appropriate for what you want to read.

Put some println statements in your loop and throughout your code to see what is happening--and if execution is even reaching those points you think they are. From your previous comments of 1 line in the file, you are not even executing your while 1 time because you read that line long before you get to your loop. And in the case of you have more than 1 line in your file, then you actually hit your while loop and there is where I will assume that it hangs from all of the UNCHECKED reads you do.

If you need to read from the Scanner make a little test program that reads from the Scanner object like you use in your program, and get it working. Know this: you have to check to see if you have the appropriate input BEFORE you read using the Scanner object. You can do a hasNextLine() and then read the individual data items from the line, which may leave that last return hanging in the file, or better IMO is to read the full line and parse it. But you have to check BEFORE you read.

Remember to keep your programs clean and formatted appropriately, it will make a world of difference. Stacking commands on a line does nothing for you in Java except make your program very difficult to read and follow. Java has a smart compiler, the JIT Compiler, and what ever you put on your page is modified by the JIT Compiler to be as optimal as it can make it, and good clean code is what makes the difference, not white space or the lack there of, to the JIT Compiler.

You have some major rewriting of this to do, to get rid of ALL of those uncheck reads: never be afraid to refactor your work, even to the point of do it all over--sometimes that is the best approach.

Any ways, have fun.

Les
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!