-1

I have a input file formatted in below.

ders : bilgisayargiris
hoca : erdogan
kod : 101
akts : 5
gtukred : 3
donem : 1
info : bu derste bilgisayar  falan .Also ogretiliyor confusing,blah,words,blah
Also ogretiliyor someshit.
soru : flip-flop devre nedir
cevap : erdoz
-

Basically i am reading a file with txt filled like this , getting right sides of colons and assigning them to my data in class. Character "-" used as an indicator that file is going for another information struct/piece.

Here's part of code i am struggling with below.

public void read(){
    className = this.getClass().getSimpleName();
    className = className + ".txt";
    openFile(className);

    readFile();

    System.out.println(className);
    closeFile();

}

public void openFile(String filer){
    try{
        scan =  new Scanner(new File("/home/paypaytr/IdeaProjects/yallah/src/a.txt")); //test purposes

    }
    catch (Exception e){
        System.out.println(className+"couldnt found.");
        //some safe quit mechanicsm
    }

}



 public void readFile(){
        while (scan.hasNextLine()){
            if(scan.nextLine()=="ders"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
               name = scan.nextLine();
            }
            else if(scan.nextLine()=="hoca"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                tutor = scan.nextLine();
            }
            else if(scan.nextLine()=="kod"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                code = scan.nextInt();
            }
            else if(scan.nextLine()=="akts"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                akts = scan.nextInt();
            }
            else if(scan.nextLine()=="gtukred"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                gtukred = scan.nextInt();
            } else if(scan.nextLine()=="donem"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                donem = scan.nextInt();
            }
            else if(scan.nextLine()=="info"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                while(scan.nextLine()!="soru")
                info += scan.nextLine();
            }
            else if(scan.nextLine()=="soru"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                soru = scan.nextLine();
            }
            else if(scan.nextLine()=="cevap"){
                scan.nextLine(); //this is for skipping ":" but would love to delim it.
                cevap = scan.nextLine();
            }


        }
    }
public void closeFile(){
    scan.close();
}

Can someone please help ?

Om Sao
  • 7,064
  • 2
  • 47
  • 61
opricnik
  • 23
  • 5
  • Please try to write question in a way: what exactly you are stuck at ? Sometimes longer questions deter people. – Om Sao Aug 10 '17 at 12:33
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 10 '17 at 12:36

5 Answers5

1

You can try using: BufferedReader with readLine

import java.io.*;  
public class BufferedReaderExample {  
    public static void main(String args[])throws Exception{    
          FileReader fr=new FileReader("D:\\testout.txt");    
          BufferedReader br=new BufferedReader(fr);    

          String line;   

          while ((line = br.readLine()) != null)  //Checks what in C++ you do EOF
          {

           line = line.substring(line.indexOf(":")+1);  //Here I am just implementing, how to print string after ":", you can process the string stored in variable line as per your convenience

           System.out.print(line);  
          }  
          br.close();    
          fr.close();    
    }    
}  

I hope you got the idea, you can accept the answer and close the question if it helped.

Om Sao
  • 7,064
  • 2
  • 47
  • 61
  • Though i am having problems with exception throwing. So can't you declare exceptions in methods? Because i have a public void Read function that should throw the exception.I dont do code on main – opricnik Aug 10 '17 at 13:32
  • If you are writing code in eclipse IDE, then it will give you suggestions to edit/declare exceptions. – Om Sao Aug 10 '17 at 13:33
1

You can try FileReader

String p = ":";
    FileReader fr = new FileReader(new File("C:\\raw.txt"));
    BufferedReader br = new BufferedReader(fr);
    long startT = System.currentTimeMillis();
    String val = null;
    String[] finalAr = new String[1048576];
    while(br.ready()){
        finalAr[n] = br.readLine();
        n++;
    }
    for(int i = 1; i < finalAr.length; i++){
        if(finalAr[i].contains(p)) {
            System.out.println(finalAr[i].split(",")[1]);
        }
    }
The N..
  • 70
  • 1
  • 5
1

I would recommend that you make this simpler.

Read each line as a String in its entirety. Deal with the tokenizing and parsing after you have the line in memory.

You can look for '#' in the first character and ignore what comes after as a comment.

You can look for '-' in the first character to indicate a new structure.

You can ignore blank lines.

You can split anything else that comes along using the String.split(":") method to get the key/value pair.

You should also consider something that is well understood, like .properties files, YAML, or JSON. Why invent a new one?

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    Exactly. This is basically asking "how to re-invent" java properties again. The only difference is to use : instead of = – GhostCat Aug 10 '17 at 12:38
  • I am quite actually first time hearing those java methods/file types. Though i like using my own creations instead of libraries if its possible. Thanks for help will try and fix code. – opricnik Aug 10 '17 at 12:45
  • It's good that you want to try things on your own and learn how the language works, but once you're familiar you should not turn your nose up at libraries. You will want to know about all those file formats. You can't do web development without knowing JSON. Properties files are already built into the JVM you're using. Why not try that first and see what you think? – duffymo Aug 10 '17 at 12:48
0

I suggest you to use the BufferedReader class which implement a readLine() method, that'll ease your work.

Example right here.

The line readed can then be splited using the split(":") method of a string object.

Example right here.

Asew
  • 374
  • 2
  • 13
0

By default, Scanner uses whitespace to separate tokens. You have to set another delimiter: scan.useDelimiter("\\s*:\\s*")

Next point is, that you usually cannot compare Strings with ==, you have to use equals().

Claus Radloff
  • 357
  • 1
  • 11