Results 1 to 3 of 3

Thread: method

  1. #1
    Syahrul is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default method

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    class ReturnFileLine {
    	private String files;
    	private String fileread;
    	public ReturnFileLine(String filename) {
    	files = filename;
    	}
    	public int total() throws IOException {
    	File in = new File(files);
    	Scanner read = new Scanner(in);
    	int inc=0;
    	do {
    	inc = inc+1;
    	fileread = read.nextLine();
    	} while (read.hasNextLine());	
    	return inc;
    	}
    	public String readLine(int num) throws IOException {
    	File in = new File(files);
    	Scanner read = new Scanner(in);
    	String readLine=null;
    	for (int n=1;n<=num;n++) {
    	readLine = read.nextLine(); 
    	}
    	return readLine;
    	}
    }
    class ReadFile {
    	public static void main(String[] args) throws IOException {
    	ReturnFileLine rfl = new ReturnFileLine("E:\\Web\\web.txt");
    	System.out.println("Total lines " +rfl.total());
    	System.out.println("Text at line 40 are:" +rfl.readLine(40));
    	}
    }
    just wondering, do i have to put
    File in = new File(files);
    Scanner read = new Scanner(in);
    in method every time i want to read file?
    Thank you

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    no you can make those static variables(global variables???). Declare them inside your class but outside your methods.

    static String files = new String("some file location");
    static File in = new File(files);
    static Scanner read = new Scanner(in);

    You might also be able to link it all together.

    static Scanner read = (new File(new String("File name"));

    I don't reccomend doing it this way though unless you don't need to use files or in outside of that line.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Store the File rather than the String. That is create the File object inthe constructor from the supplied filename. Other than that, you need to reopen the file for each method since they do different things. Sharing an IO resource between them doesn't make sense to me.

    There's absolutlely no need to make things static. In fact I would argue it's a mistake, since you have defined a perfectly good class there with perfectly good attributes.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •