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.
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); } }
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); } }
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 :)
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.