Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  FileWriter does not write.  (Read 1558 times)
0 Members and 1 Guest are viewing this topic.
Offline SkyAphid
« Posted 2012-07-17 08:04:03 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
   public void parseSynonyms(String line, ArrayList<String> synonyms, File f){
      try{
         BufferedWriter fw = new BufferedWriter(new FileWriter(f));
         fw.newLine();
         
         Scanner scanner = new Scanner(line);
         scanner.useDelimiter(" ");
         
         while(scanner.hasNext()){
            String s = scanner.next();
            if (s.contains("[[")){
               if (Memory.get().isDebugMode()) {
                  System.out.println(s);
               }
               fw.write(" " + s);
               fw.flush();
               fw.newLine();
               synonyms.add(s);
            }
         }
         
         fw.close();
      }catch(FileNotFoundException e){
         e.printStackTrace();
         System.out.println("Criticial Error, Exiting.");
         System.exit(0);
      }catch(IOException e){
         e.printStackTrace();
      }
   }


I can't find anything wrong with this.
S does have a string in it. Not empty.
The file exists. In fact, the program creates it if it doesn't.
It overwrites the file and empties it to add to insult.

How do I fix this?

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline Sickan

Senior Duke


Medals: 9



« Reply #1 - Posted 2012-07-17 08:07:42 »

You forget to close the Scanner...

Cheers Smiley
Offline SkyAphid
« Reply #2 - Posted 2012-07-17 08:14:52 »

You forget to close the Scanner...

Cheers Smiley
Other than optimizing my program, that didn't fix anything hahah.
Thanks anyway for helping and for reminding me of that though, hahah. Smiley

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline steveyO
« Reply #3 - Posted 2012-07-17 11:09:55 »

Just tried it here and it works fine.  The file is written for me (provided your passed line string does actually contain a "[[", otherwise its empty).

https://play.google.com/store/apps/details?id=com.bullsquared.alggame Annoying Little Gits (Android)
www.bullsquared.com   Play java (applet) games! www.chessclockpro.com Free Online Chess Clock
Offline ReBirth
« Reply #4 - Posted 2012-07-17 13:51:22 »

Where do you write? If you tried to write on drive C on windows 7 with UAC enabled, there's chance will fail.

Offline SkyAphid
« Reply #5 - Posted 2012-07-17 14:46:13 »

Where do you write? If you tried to write on drive C on windows 7 with UAC enabled, there's chance will fail.
I just write to the folder it's in, which is in Documents/My Projects/Java Projects/Aida/

You know what's awesome? When something doesn't work on your computer but does on pretty much everyone elses.

Dammit Java. Even if I'm onto something, I'm not.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-07-17 20:36:15 »

@ReBirth, you would get a FileNotFoundException if so.

@OP are you sure the strings contain "[[" in the first place?

Offline SkyAphid
« Reply #7 - Posted 2012-07-20 09:13:55 »

@ReBirth, you would get a FileNotFoundException if so.

@OP are you sure the strings contain "[[" in the first place?

Yup. It outputs before it supposedly writes.

I just said screw it and wrote it to a singleton hashtable and outputted the object through an object stream.

Java seriously pisses me of sometimes.

Off subject, I have another problem.

1  
2  
3  
4  
5  
6  
7  
8  
        for (int a = 1; a < words.size(); a++){
           String word = words.get(a);
           String word2 = words.get(a-1);
           
           if (word.equals(word2)){
              words.remove(a);
           }
        }


All words are "?"

It doesn't remove them.
What am I doing wrong?

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #8 - Posted 2012-07-20 09:32:29 »

Well, it should remove half of them (I think).

Perhaps you meant something like this:

1  
2  
3  
4  
5  
6  
7  
8  
        for (int a = 1; a < words.size(); a++){
           String word = words.get(a);
           String word2 = words.get(a-1);
           
           if (word.equals(word2)){
              words.remove(a--);
           }
        }

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-07-21 03:34:51 »

Are you using UTF-8 to read the words, assuming that you are reading the words in at first.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (37 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!