Home Answers Viewqa Java-Beginners how to pass a string from one class to another

 
 


Deepika
how to pass a string from one class to another
4 Answer(s)      9 months ago
Posted in : Java Beginners

hi all, Good morning. I am trying to develop a coding pass a string from one class and declare in other class. the string which i need to pass is the data from an file and i want to move it to the main method. i am trying to find a solution for this. Thank's in Advance... :)

this is the code.

import java.io.*; 
class Class1 {                       //Class1
public Class1(String name){
try{
   // private String name = "Class1";
FileInputStream fstream = new FileInputStream("1.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));

    //private String name;// = "Dot saw I was Tod";
  //  private int IdNo = 20;

while ((name = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (name);
  }in.close();
}catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
}
}
public class Class2 {   
    public static void main(String[] args) {
        Class1 class1 = new Class1(); // Creating object of Class1
        String name = "Class2";
        System.out.println("Welcome in " + name);
 System.out.println (name);
//        System.out.println("Another Class variables value:" + class1.getIdNo()+ " and " + class1.getName());
//           class1.class1Method();
    }
}
// i want to display the output of 1.txt in the Class2

input is 1.txt

001 Apple
001 mango
002 Pappaya
003 orange
004 gauva
006 stawbery

i am getting an error in this line: Class2.java:75: cannot find symbol symbol : constructor Class1() location: class Class1 Class1 class1 = new Class1(); // Creating object of Class1 ^ 1 error

am i in the correct flow? if i am wrong. correct it.

View Answers

August 28, 2012 at 3:43 PM


Here is a simple example that passes string from one class to another.

import java.util.*;
 class A{
    static String st;
    public static String get() {
    Scanner input=new Scanner(System.in);
    System.out.print("Enter String:");
    String str=input.nextLine();
    st=str;
    return st;
    }
    }

 public class PassString {
     public static void main(String[]args){
     String b=A.get();
     System.out.println("Get String from class A: "+b);
     }
}

August 28, 2012 at 3:50 PM


Here is another example of passing string from one class to another.

import java.io.*;

class Class1 { 
    public static String get() {
        String data = ""; 
        try{ 

         String name="";
         FileInputStream fstream = new FileInputStream("1.txt"); 
         DataInputStream in = new DataInputStream(fstream);
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
      while ((name = br.readLine()) != null) { 
     data+=name+"\n";
     }

     in.close(); 
     }catch (Exception e){
         System.err.println("Error: " + e.getMessage()); 
         }
         return data;
         }
         }
         public class Class2 {
    public static void main(String[] args) {
    Class1 class1 = new Class1(); 
     String data =class1.get();
     System.out.println("Data of class1: " + data);
  }
}

August 28, 2012 at 4:32 PM


hi,Thank's a lot.. in deed this was a great help.. for my stuck up for past few day's... thank you.. this will really help all.. you have done a great job... :) thank's :)


August 30, 2012 at 3:01 PM


hi, still one one doubt i have got. I want to display the results of both numbers and fruits name separately. In this the return statement i am facing some problem. I want to use two types of return . and siaply the result. It showing some error in the code

code is

import java.io.*;

class Class1 { public static String get() { String data = "";

String data1= "";

    try{ 

     String name="";
     FileInputStream fstream = new FileInputStream("sendfile.txt"); 
     DataInputStream in = new DataInputStream(fstream);
     BufferedReader br = new BufferedReader(new InputStreamReader(in));
  while ((name = br.readLine()) != null) {

String[] split = name.split("\t");

String no = split[0];

String fruits = split[1];

data1+=fruits+"\n";
 data+=no+"\n";

 }

 in.close(); 
 }catch (Exception e){
     System.err.println("Error: " + e.getMessage()); 
     }
     return data;
 return data1;    
     }
     }

     public class Class2 {
public static void main(String[] args) {
Class1 class1 = new Class1(); 
 String data =class1.get();
String data1 =class1.get();
     System.out.println("Data of class1 numbers: " + data);
 System.out.println("Data of class1 fruits: " + data1);

} }

Error is : Class2.java:27: unreachable statement return data1;

Till i have seen. there is only one return used in one class. But i want two kinds of input. How to do such thing with return ?

Help me.









Related Pages:
how to pass a string from one class to another
how to pass a string from one class to another  hi all, Good morning. I am trying to develop a coding pass a string from one class and declare... string from one class to another. import java.util.*; class A{ static String
How to pass variable from one class to another in java
How to pass variable from one class to another in java  How to pass variable from one class to another in java?   Example: public class...; } } //Another class Class2. public class Class2 { public static void main(String
How To Pass data from one GUI to another in java swing
How To Pass data from one GUI to another in java swing  I'm new to java and part of our assignment is to build a GUI and display a result set from data input. I'm stuck at how to get the user's input from JTextFields and combobox
how to pass class variable to another class
how to pass class variable to another class  i need to access a variable from a class to another class currently running at the same time. using... class B { static int b=A.get(); public static void main(String[]args
how to pass an array from one jsp to another jsp - JSP-Servlet
how to pass an array from one jsp to another jsp  hi friedns, can any One tell me how to send an array from one jsp to another jsp,pls any one send the code for this.also porvid the code how to retrive the arry in another jsp
passing data from one jframe to another
passing data from one jframe to another  I have two JFrame,built by using the GUI Editor netbeans 6.9.i have to pass a data from a Jtextfield in the first Jframe to another JLabel in the other JFrame.Can you please help me,how
How to pass Array of string from action class to jsp page
How to pass Array of string from action class to jsp page  this is my action class package login.ipm; import java.sql.*; import java.util.ArrayList... class DiagservicesAction extends org.apache.struts.action.Action { private static
how to pass arraylist data from one jsp to another jsp - JSP-Servlet
how to pass arraylist data from one jsp to another jsp  hi to all iam not able to pass the data which is stroed in arraylist, actually i need... the following >XXX/ ? is required to send a value from one jsp to another
how to change password into one jsp to another jsp
how to change password into one jsp to another jsp  i have two jsps in one jsp i give one password and confirm password, in another jsp i want change..."; Connection con=null; String pass=""; int id=0; try{ Class.forName
Copy one file into another
;This string is copied from one file to another\n"); bf.close(); InputStream...Copy one file into another In this section, you will learn how to copy content... : This string is copied from one file to another 
how to go from one frame to another frame in swings - Java Beginners
how to go from one frame to another frame in swings  Hi, My task is to go from one frame to another.......... let us think that iam having two... this task in swings or applet......... i have gone through one example of login
how to write function of copy from one to another location in this code
how to write function of copy from one to another location in this code  I need to write a code to copy a file from one location to another using... class Browse extends JFrame implements ActionListener { JButton button1
Pass cookie from one site to another within the same domain
Pass cookie from one site to another within the same domain  Hi, I want to pass a cookie from one website to another within the same domain. This is just to identify whether the same user is logged in or not. Can anyone help me
Copy data from one JTextField to another JTextField
, you will learn how to copy the data from one JTextField into another... Copy data from one JTextField to another JTextField... and then paste into another JTextField. JTextField is used in the swing
Moving file or directory from one directory to another
Moving file or directory from one directory to another... directory is moved from one directory to another directory. This program... from one directory to another. In this section an example is given
Java - Copying one file to another
the contents from one specified file to another specified file. copyfile(String...;    This example illustrates how to copy contents from one... to another specified file from the first one specified file. // For creating a byte
How to call the run method from another class?
How to call the run method from another class?  Sorry, if this is a stupid question. I would like to find out how to call the run method... run() { new FereastraPrincipala().setVisible(true); from the class
how to pass string array from action class to jsp page? pls reply me.
how to pass string array from action class to jsp page? pls reply me.  how to pass string array from action class to jsp page? pls reply me.  .... In your action class, String[] arr = {"A","B","C","D"}; for (int x = 0; x < arr
Java - Copying one file to another
how to copy contents from one file to another file. This topic is related... called copyfile which copies the contents from one specified file to another... of one file and write to another specified file from the first one specified
navigating the value of JTextField from one JFrame to another - Swing AWT
navigating the value of JTextField from one JFrame to another  hello I m doing one project where I m retriving the data from databse in JTextField...=conn.createStatement(); ResultSet rs=st.executeQuery("Select * from data"); String
how can we pass parameters from jsp href to another servlet
how can we pass parameters from jsp href to another servlet  how can we pass parameters from jsp href to another servlet
How to pass an arraylist from servlet to jsp?
How to pass an arraylist from servlet to jsp?  Hello, Can anyone please tell me how to pass an arraylist from servlet to jsp? I have two arraylist one of type String and the other int. How to send both the arraylists from
navigating the value of JTextField from one JFrame to another - Swing AWT
navigating the value of JTextField from one JFrame to another  Hello... is working also.. but only first record is comming... how can I fetch all the records from database using up and down keys of keyboard? And also I wanted
How to copy files from one folder to another based on lastmodified date - Java Beginners
How to copy files from one folder to another based on lastmodified date  I have to make a simple utility to copy files from a folder to another... java.io.*; public class CopyFile{ private static void copyfile(String srFile
get the value from another class - Java Beginners
get the value from another class  Hello to all, I have stupid...().trim(); [/code] How I can get String alsl = ((Node)flnameTEXT.item(0... javax.xml.transform.stream.StreamResult; public class xmlRead{ static public void main(String[] arg
How to forward the control from one jsp to another?
How to forward the control from one jsp to another?  Hi! This is Prasad Jandrajupalli. I have the 3 JSP's, but I want communicate with each... is not communicate with the Third JSP. I want forward the control from first jsp to second
Copying an array to another
Copying an array to another       Java Copy Array Example:In this tutorial, you will learn how to copy data from one array to another. Here, providing you an example with code
Use varibles from another class
Use varibles from another class  **I have two classes. class 1:** public class askfigures extends JFrame { method from class1: public void... in another class. another class: public class Askvalues extends JFrame
How to navigate from one jsf to another in eclipse
How to navigate from one jsf to another in eclipse  Hi there is my...; <from-view-id>/JSF/UserDetail.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <
Drag and Drop components from one frame to another frame
Drag and Drop components from one frame to another frame... button etc.) from one frame to another frame. In this code we have used...;One frame has five component, user can drag and drop component from one
ArrayList as an argument to another class
ArrayList as an argument to another class  how do you pass arraylists as arguments to another class, and how does the method/class accept this arraylist for use
How to write content of one file to another file.
the use of CheckedOutputStream class. The CheckedOutputStream class is from the java.util.zip package. The CheckedOutputStream  class is used.... And at the same time the CheckedOutputStream  class calculates checksum
How can i pass the valus from a JSP to the action class???
How can i pass the valus from a JSP to the action class???  hewllo wevryone... can anyone help me with how i can pass the value of menuId in my JSP and pass it in the action class
Class
and Perimeter of the square and another class is rectangle which is using for getting... one it's own constructort. Constructor creates a instance for the class... that how to to implement the constructor feature in a class. This program
Pass parameters from JSP to Servlet
Pass parameters from JSP to Servlet In this section, you will learn how to pass parameters from JSP page to servlet. For this purpose, we have used... Name","Roseindia"); request.setAttribute("Address","Rohini,Delhi"); String
Pass values from form to form
Pass values from form to form       Java program to pass values from one form to another form... values from one page to another consecutive page which is being called then we can
how to pass variable from simple java main class(not servlet) to the jsp page?
how to pass variable from simple java main class(not servlet) to the jsp page?  I have a simple Java class in which I invoke a call to a JSP page... from the simple Java class to the JSP page. How can I do that? Here is my
swings:how to link from one form to another form
swings:how to link from one form to another form  how to link from one form to another form
Java call method from another class
Java call method from another class       In this section, you will study how to access  methods of another class. For this we have created two java files
how to find a dynamic string in a string & how to replace tgat wth with another string - JSP-Servlet
how to find a dynamic string in a string & how to replace tgat wth with another string  Dear sir, i have a ArrayList in that i have stored a values from a excel sheet specified column values and i have one string
File copy from one directory to another in java
File copy from one directory to another in java  I am trying to find very good code example for copying a file from one directory to another... from a directory to another directory. How to achieve this? Is there any API
how to copy file from one directory to another in java with examples
how to copy file from one directory to another in java with examples  how to copy file from one directory to another in java with examples? I want... copy from one directory to another in java. Thanks
copying data from one table to another
copying data from one table to another  i need to copy data from one table to another table.. i.e the 1st eid of employee table must be copied...... ...................... String sq="insert into employee values(emp_seq.nextval,'"+ename+"','"+eadd
Java : String Case Conversion
Java : String Case Conversion In this section we will discuss how to convert a String from one case into another case. Upper to Lower case Conversion... : This example shows conversion of String from one case to another case. '
How to compare two tables, and insert values which r not in one table to another table?
How to compare two tables, and insert values which r not in one table..."; String pass = "root"; Connection connection = null... = DriverManager.getConnection(connectionURL,user,pass); String
How to pass multiple values from a servlet to JSP?
How to pass multiple values from a servlet to JSP?  hi, I want to pass multiple values form a servlet to JSP. Hw do i do that? I am able to send one value at a time without any prb, but I am unable to carry multiple (from two
How to return a string from a JTextField
How to return a string from a JTextField  Hi, Am new to java, and am trying to get a string from a JTextField so that I can compare it with another String using either compare or equals or compareTo methods of the String class
how to pass form values from javascript of html page to jsp page
how to pass form values from javascript of html page to jsp page   ... the geocode and tries to return the lattitude and longitude .But my question is how to submit all the form values with lattitude and longitude returned from
How to pass value from dijit.Tree to dijit.MenuItem - Ajax
How to pass value from dijit.Tree to dijit.MenuItem  Hi Friends, I... node whicl will apper on right click. What I have already done. From my server side(Struts action class) I have passed a JSON structure through URL
Data Conversion from String to another type
Data conversion is the conversion of data from one type to another type. In this section we will learn about data conversion from String primitive type to another data type like int, boolean and char etc