Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Class in class

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Class in class

    i have this code here:

    import java.io.*
     
    public class ReadLastLine {
     
     public static void main(String[] args) throws Exception {
     
      FileInputStream in = new FileInputStream("test.txt");
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
     
      String strLine = null, tmp;
     
      while ((tmp = br.readLine()) != null)
      {
         strLine = tmp;
      }
     
      String lastLine = strLine;
     
      System.out.println(lastLine);
     
      in.close();
     
     
     }
    }

    and i want to call the function lastLine in a other class, how am i supposed to do that?


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Location
    hyderabad
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Class in class

    what you asking is not clear are you suppose to like this :
    import java.io.*;

    public class ReadLastLine {

    public static void main(String[] args) throws Exception {

    FileInputStream in = new FileInputStream("D:/123/test.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine = null, tmp;

    while ((tmp = br.readLine()) != null)
    {
    strLine = tmp;
    }

    String lastLine = strLine;

    System.out.println(lastLine);
    LastLine l = new LastLine(lastLine);

    in.close();

    }
    }

    class LastLine{
    public LastLine(String str){
    System.out.println("LastName is ------"+str);
    }
    }

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Class in class

    i have my first method

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;  
     
    public class login implements ActionListener  {
     
    	//Center
    	public final JTextArea Center = new JTextArea(400, 500); //laver en final fordi ellers kan action center ikke finde værdien
     
    	public void actionPerformed(ActionEvent event) {
     
    //East
    		JPanel East = new JPanel(new GridLayout(8,2));
    		East.add(konti1);
    		East.add(new JLabel((new ReadLastLine().getValue())));     // here is where i want the last line output
    		East.add(konti2);
    		East.add(new JLabel("Tilrådighed"));
    		East.add(new JButton("Konti"));
    		East.add(new JLabel("Tilrådighed"));
    		East.add(new JButton("Konti"));
    		East.add(new JLabel("Tilrådighed"));
    		East.add(new JLabel(""));
    		East.add(new JLabel(""));
    		East.add(new JLabel(""));
    		East.add(new JLabel(""));
    		East.add(new JLabel(""));
    		East.add(new JLabel(""));
    		East.add(new JLabel(""));
    		East.add(new JLabel(""));

    and then i have my other method

    	public class readLastLine{
    		public String readLastLine(){
    		FileInputStream in = new FileInputStream("test.txt");
     	   BufferedReader br = new BufferedReader(new InputStreamReader(in));
     
     		String strLine = null, tmp;
     
     		while ((tmp = br.readLine()) != null){
         		strLine = tmp;
      		}
     
      		String lastLine = strLine;
     
      		in.close();
    		return lastLine;
     
    		}
     
    	}

    but i simple can't get it to print the last line it just comes up with a different error everytime i try

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM