Java problems
Page 1 of 1
CookieCrumb




Posts: 4670
Location: Celephaïs
PostPosted: Thu, 24th Nov 2011 04:31    Post subject: Java problems
Well hello there people of the hump who actually know some java.
Anyways.
What I want to do is have the program read the lines of a .txt file and store it into an String Array. This is what I got so far ( I know I could probably make it a lot simpler/easier but that's the way I want to do it. Rather primitive)
Code:
import java.io.*;

public class UltimateBravery {
   public static int count;
   
   public static void main(String[] args) {
      String line = "";
      
      try {
         FileReader fread = new FileReader("bin\\boots.txt");
         BufferedReader br = new BufferedReader(fread);
         String bootsArray[] = new String[linecount(br)]; // everything seems to be working up to this point
         for(int i = 0; (line = br.readLine()) != null ; i++) {
            bootsArray[i] = line;
            System.out.println(line);               // these three lines aren't working as I thought they would
         }
         
      }
      catch (IOException foo) {
         System.out.println(foo.getMessage());
      }
      
   }
   
   public static int linecount(BufferedReader file) {
      try {
         while(file.readLine() != null) {
            count++;
         }
      }   
         catch (Exception ex) {
            ex.printStackTrace();
         }   
      
      return count;
   }

}


I actually thought this would write each line of the .txt file into the array but it doesn't seem to do that since the system.out.println(line) gives me nothing at all.
Another thing that I'm suspecting (since I'm still a pretty big noob in java) is that the entries of my array aren't useable outside the try block?
So if I want the array available outside the try block how'd that have to look?
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Thu, 24th Nov 2011 09:18    Post subject:
will post when I arrive at work(am on my mobile right now), but the answer to your 2nd question, you define your bootsArray inside the try so it's only reachable there.define it outside and it'll be usable there.


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Thu, 24th Nov 2011 10:46    Post subject:
Code:

import java.io.*;
import java.util.*;

public class testerr {
   public static void main(String[] args) {
      Vector<String> lines = new Vector<String>();
      
      try {
         FileReader fread = new FileReader("bin\\boots.txt");
         BufferedReader br = new BufferedReader(fread);
         String line;
         while((line = br.readLine()) != null) {
            lines.add(line);
            System.out.println(line);
         }
      } catch (IOException foo)
                System.out.println(foo.getMessage());
      
   }

}


Works flawless Smile

The Vector is much nicer to use than your array. To go through your vector you can do the following:
[code]
for (String oneLine: lines){
System.out.println(oneLine);
}
[/code]

There are Mad It's just broken Very Happy


Last edited by PumpAction on Thu, 24th Nov 2011 11:07; edited 1 time in total
Back to top
BearishSun




Posts: 4484

PostPosted: Thu, 24th Nov 2011 11:05    Post subject:
there are no code tags on the hump you NOOB Very Happy (edit: oh there are Sad I'll go back to my noob cave then)


OT: good solution although I can't figure out why wouldn't the first one work. Although my java is really rusty

EDIT: Oh right nm. You reach the end of the file when you count the lines, so when you try to actually print them it's already at the end so nothing gets read.


Last edited by BearishSun on Thu, 24th Nov 2011 11:13; edited 1 time in total
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Thu, 24th Nov 2011 11:08    Post subject:
Oh yes, I forgot to add the reason for the first one not working Smile
Back to top
CookieCrumb




Posts: 4670
Location: Celephaïs
PostPosted: Thu, 24th Nov 2011 13:45    Post subject:
I know it'd be much easier using vectors but I want to do it as pimitively as possible.
Anyway. Fiddled around a bit to see why it's not working and to be honest I still can't figure it out

Code:
public static void main(String[] args) {
      String line = "";
      
      
      try {
         FileReader fread = new FileReader("bin\\boots.txt");
         BufferedReader br = new BufferedReader(fread);
         bootsArray = new String[linecount(br)]; // everything seems to be working up to this point
         for(int i = 0; (line = br.readLine()) != null  ; i++) {
            bootsArray[i] = br.readLine();
            System.out.println(br.readLine());
         }
         
      }
            
   
      catch (IOException foo) {
         System.out.println(foo.getMessage());
      }
      
   }


Same code as before.
After I set the array with new String[linecount(br)];
I inserted some code to see if the array is initialised with the proper size. And it is. Had a loop there that just filled the array with a string and another to print the array. Worked.
So the problem must be with the readLine().

If I change the for loop to
Quote:
for(int i = 0; i <= count-1 ; i++) {
bootsArray[i] = br.readLine();
System.out.println(br.readLine());
}

- this should work since I know the length of the file already through linecount - all it prints is null. Seven times.
Oh and th txt file is not empty. So that can't be it (linecount wouldn't return the proper value in that case anyway).

(second one had to be a quote since apparently you're not allowed to use the code tag more than once :/ )
Back to top
BearishSun




Posts: 4484

PostPosted: Thu, 24th Nov 2011 13:48    Post subject:
BearishSun wrote:
You reach the end of the file when you count the lines, so when you try to actually print them it's already at the end so nothing gets read.


You call readLine() on br object in linecount() function until you reach the end of the file. Then when you leave the function you try to call readLine again, but you are already at the end of the file so there is nothing to be read.

you need to call "br.reset()" before calling readLine() after linecount(), so that you get back to the start of the file.

EDIT: Actually reading a little how mark/reset work you better no use them. Just create a new BufferedReader object after the linecount() method and call readLine on that. Be sure to call close() on the original one before.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Thu, 24th Nov 2011 13:59    Post subject:
Bearish already said that your file cursor is at the end of file. You have to close and reopen the file (ugly way) or just reset the file cursor to the beginning of the file Smile


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
CookieCrumb




Posts: 4670
Location: Celephaïs
PostPosted: Thu, 24th Nov 2011 22:23    Post subject:
Thanks. Gonna fix this later/tomorrow. Still not that used to java.


sabin1981 wrote:
Now you're just arguing semantics. Getting fucked in the ass with a broom stale is an "improvement" over getting stabbed in the eye with a fork Rolling Eyes
Back to top
CookieCrumb




Posts: 4670
Location: Celephaïs
PostPosted: Fri, 25th Nov 2011 10:48    Post subject:
I feel like such a noob for having to ask shit like this :/

Code:

public class foo {
 String a[];
 String b[];
 String c[];
 String d[];

 String list[] = new String[4];
 {
   list[0] = "a";
   list[1] = "b";
   list[2] = "c";
   list[3] = "d";
  }


Now. Is it possible to use the array a[] through list[0]?

so instead of a = linecount(whatever);
that I use list[0]? If that makes any sense at all.
Back to top
BearishSun




Posts: 4484

PostPosted: Fri, 25th Nov 2011 10:56    Post subject:
You have to make list an array of arrays. Just putting variable name between quotes "" will just make a string with that name. It will never point to any actual variable.
And you need to initialize you arrays to null unless you place something in them right away.

Code:

         String a[] = null;
         String b[] = null;
         String c[] = null;
         String d[] = null;
       
         String list[][] = new String[4][];
         {
           list[0] = a;
           list[1] = b;
           list[2] = c;
           list[3] = d;
          }


Although if you are using just linecount (which returns just an integer) then you only need a single array of ints to begin with:

int list[] = new int[4];
list[0] = linecount(whatever);
list[1] = linecount(whatever2);
list[2] = linecount(whatever3);
list[3] = linecount(whatever4);
Back to top
CookieCrumb




Posts: 4670
Location: Celephaïs
PostPosted: Fri, 25th Nov 2011 14:22    Post subject:
Decided to go a different route on that issue but thanks anyway. Next problem! xD

I have a function called readfile which read the lines of a txt file into a String array. So far so good. It's parameters are a string which contains the location of the .txt file I want it to read and the String array into which the lines are to be put.
Code:

public class test{
public static String a[];

public static String[] readfile(String foo, String[] bar) {
      
      try {
         count = 0;
         BufferedReader br = new BufferedReader (new FileReader (foo));
         bar = new String[linecount(br)];
         br = new BufferedReader (new FileReader (foo));
         for(int i = 0; i <= count-1 ; i++) {
            bar[i] = br.readLine();
         }
      }
      
      catch (IOException blargh) {
         System.out.println(blargh.getMessage());
      }
      
      return bar;      
   }
 
}

public static void main (String[] args) {
readfile("sourcefile.txt", a);


Silly me thought this would basically mean that a = bar.
Obviously it doesn't because if I try to check a's entries I get a Null Pointer Exception.

Where did I go wrong this time and what do I need to change so the entries of a = entries of bar and I can use a in my main function?

(Still hating that I need to ask stuff like this :/ )
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Fri, 25th Nov 2011 15:38    Post subject:
http://www.yoda.arachsys.com/java/passing.html

Read this Smile

Might sound a bit strange but the more you develop and the more time passes, this all will become very very intuitive. Just don't give up! Smile You already have some good ideas!
Back to top
CookieCrumb




Posts: 4670
Location: Celephaïs
PostPosted: Wed, 14th Dec 2011 17:01    Post subject:
Hadn't had much time lately to work on this (as it turned out more complicated than I had expected - well for me at least). Today I went back to it and after a period of "what the fuck did I do there?" I figured out where I was and what I wanted to do next.
Let's see. I managed to get it working so far. It ain't pretty and I'm sure I'll make it look better before I'm done with it but for now I'm happy with this:
http://pastebin.com/h3ZYJkv1

It ain't pretty but it works. As long as I run it from within Eclipse. I then tried running it through the Windows shell (yes, I'm ashamed to do this. I actually wanted to test it on a linux based system but I don't have one handy right now).
It throws a Null pointer exception. Which isn't really surprising as it apparently can't find the path I specify. I suspect I've made a mistake but I can't figure out how I should have specified the file locations in the code.

edit:
Think I figured it out.
Ecplise is looking for the file in the project folder ie c:\users\SomeDude\workspace\RandomCrap\ so I have to specifiy the bin folder for it.
However if I use and try to run it inside the bin folder it looks in
C:\users\SomeDude\workspace\RandomCrap\bin\ so the 'bin' prt shouldn't be necessary.
Is there any way to make it work in Eclipse and if I use the shell?
(Still not sure if I'm gonna do a GUI for this but meh that's not my problem right now xD)

edit²:
For the love of god I cannot figure out how to get an executable .jar file that from this that actually runs. Argh. Oh well. Back to google it is.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group