call function to read more than one java file call it in main program?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 804 Views - Last Post: 08 March 2016 - 06:38 PM Rate Topic: -----

#1 MarNa12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

call function to read more than one java file call it in main program?

Posted 08 March 2016 - 01:56 PM

Hi all,
I read file using bufferedreader, and (try, catch),
Now this section is repeated in my code , how can I call function to do that ? By passing filepath ..I try it but I don't get any line of file ...
How can I get lines of files using function when I call it? ?


public class  fn {
public static String readfile(String filepa) throws IOException{
try {
            FileReader fr = new FileReader(filepa);
            BufferedReader br = new BufferedReader(fr);
            line = br.readLine();
            while (line !=null) {
            int    offset = line.indexOf("//");
             if (-1 != offset) {
             line = line.substring(0, offset);                                    
                }
           System.out.println(line);
            line = br.readLine(); 
        }
        }
        catch (IOException e) {
         e.printStackTrace();
        }
    return(line);   
    }
public static void main(String[]argv) throws ClassFormatException, IOException {
{String filepa= "C:/NetBeansProjects/filename.java";
line=readfile(filepa);
System.out.println(line);
}}


Is This A Good Question/Topic? 0
  • +

Replies To: call function to read more than one java file call it in main program?

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 02:10 PM

Quote

How can I get lines of files

What do you want go get? Can you post a simple example of a file and what you want to get?
What happens when the program is executed now?

NOte: THe code is poorly formatted. The {s and }s are very messy in the main() method.

This post has been edited by NormR: 08 March 2016 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 MarNa12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 03:46 PM

I want to make a function to read file ...using try catch and passing the filepath and get lines of file in main program by return of function .....
Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 03:53 PM

Ok, please explain what is wrong with the posted code.
Give an example of an input file and show what you want the method to return.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,617
  • Joined: 20-September 08

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 04:37 PM

You're reading into a variable called 'line' until that variable is null (standard practice), so if you return that variable, what is it going to contain ..?

If your objective is to put the whole file into one String, then you don't need to read it line by line - that's an unnecessary inefficiency

This post has been edited by g00se: 08 March 2016 - 04:39 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#6 MarNa12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:15 PM

I want to get lines of file ...and then i apply my processing on these lines in main program ...
can i get line by variable line ...

in my code ,,, the section of try ,catch is repeated more than one ,,,now rather than write it more than once,i want make function to do that by passing files path and then process these lines in main program.
Was This Post Helpful? 0
  • +
  • -

#7 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:17 PM

Ok, please explain what is wrong with the posted code.
Give an example of an input file and show what you want the method to return.

This post has been edited by NormR: 08 March 2016 - 05:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 MarNa12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:21 PM

View PostNormR, on 08 March 2016 - 03:53 PM, said:

Ok, please explain what is wrong with the posted code.
Give an example of an input file and show what you want the method to return.


I want to get all line of file by this method but i think the error in return way ....how can get lines from method to main program ??and then process it?
Was This Post Helpful? 0
  • +
  • -

#9 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:22 PM

Quote

error in return way

Please explain what is wrong with the current program.

This post has been edited by NormR: 08 March 2016 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

#10 MarNa12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:30 PM

if i want to return lines of file in main program ,how can i do this using a method?? ..

View PostNormR, on 08 March 2016 - 05:22 PM, said:

Quote

error in return way

Please explain what is wrong with the current program.

Was This Post Helpful? 0
  • +
  • -

#11 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:31 PM

Please explain what is wrong with the current method. You need to understand what is wrong so you can fix it.
Was This Post Helpful? 0
  • +
  • -

#12 MarNa12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:33 PM

When I call function resdfile in main program I get null; whereas I want to get lines of my files by function , how can repair this error or what way is usefull to get lines of files when I have more than one file in program ?
Was This Post Helpful? 0
  • +
  • -

#13 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:37 PM

Quote

I get null;
That is the value in the line variable when the loop exits.
If you want to return the contents of the lines that were read inside the loop, there needs to be another variable that accumulates all the lines that were read by concatenating them to that variable and then the value of that variable should be returned.
Was This Post Helpful? 1
  • +
  • -

#14 MarNa12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:42 PM

View PostNormR, on 08 March 2016 - 05:22 PM, said:

Quote

error in return way

Please explain what is wrong with the current program.

Was This Post Helpful? 0
  • +
  • -

#15 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: call function to read more than one java file call it in main program?

Posted 08 March 2016 - 05:43 PM

Also posted at: https://www.daniweb....in-main-program
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2