1

I have FileHandle class:

public class FileHandle {
public static String a;
public static String b;
public static String c;

public void openFile() throws FileNotFoundException {
    File dir = new File("C:/Folder/DB");
    if (dir.isDirectory()) {
        for (File file : dir.listFiles()) {
            Scanner s = new Scanner(file);
            //String f = file.getName();
           // System.out.println("File name:" + f );
            while (s.hasNext()) {
                a = s.next();
                b = s.next();
                c = s.next();
                System.out.printf("%s\n %s\n %s\n", a,b,c);
            }
        }
    }

And Constants class:

public class Constants {

 FileHandle h = new FileHandle();
public static final String[] LIST_DATA = {FileHandle.a,FileHandle.b,FileHandle.c};
public static final int NEW_ELEMENT_ID = 0;

}

The main question : why in my Constants class i get only last scanned document information. By the way want to mention that FileHandle class scanner is working ok everything is fine. The only real struggle is with sending variables to Constants class there as i mentioned i get only last scanned document information.

TheDude
  • 135
  • 4
  • 17

1 Answers1

1

Unsure to have understood your question. But assuming that what to want is to keep track of the different calls, you can either:

  • concatenate strings in a, b and c:

            a = (a == null) ? s.next() : a + " " + s.next();
            b = (b == null) ? s.next() : b + " " + s.next();
            c = (c == null) ? s.next() : c + " " + s.next();
    
  • make a, b and c lists:

    public static List<String> a = new ArrayList<String>;
    public static List<String> b = new ArrayList<String>;
    public static List<String> c = new ArrayList<String>;
    ...
            a.add(s.next());
            b.add(s.next());
            c.add(s.next());
    

Because a static value is shared by all instances of same class, so when you assign to it to overwrite all previous values.

BEWARE: above uses no synchronization and is not thread safe...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • So my problem is that static overwrite all previous values as you said ? – TheDude Feb 25 '16 at 14:28
  • but maybe u can help me more a little ? :) – TheDude Feb 25 '16 at 14:32
  • but method 1 printing info to my list like this : name1 name2 name3, maybe can i make them to be in new line? – TheDude Feb 25 '16 at 14:38
  • @OvidijusMaračinskas: just replace the space with a new line then: `a = (a == null) ? s.next() : a + "\n" + s.next();` and same for `b`and `c`... – Serge Ballesta Feb 25 '16 at 14:56
  • You see, maybe its my fault in explaining or i need to open new question. But i printing all this to JList , and that sepretor wont work. Sorry for your time :( – TheDude Feb 25 '16 at 14:58
  • @OvidijusMaračinskas: If you want to display values in a JList, you'd better make a, b and c be `List` and populate the JList from the List... – Serge Ballesta Feb 25 '16 at 15:02
  • I tried your second method but its not working its asking import after importing all array list and list package it tells error about diamond <> spliters, and later error goes to Constants class, that bad methods used for static...... – TheDude Feb 25 '16 at 15:05
  • @OvidijusMaračinskas You should learn to read older Java syntax and translate it into newer. Java 7 or 8 syntax would be: `public static List a = new ArrayList<>;` with the diamond on second parameter that can be deduced from first. I'm a now old man that learned old Java :-) – Serge Ballesta Feb 25 '16 at 15:41