Those error messages contain valuable information that you aren't sharing with us.
What are the exact error messages and which line(s) do they refer to?
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
h and d are variables in the Poco class, so other classes can't just refer to them as h or d without any qualification. Check your lecture notes (or the web) for how to refer to another class's variables.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
It's part of how Java works: every public class MUST be defined in a .java file whose name is the same as the name of the class. EG public class Poco MUST be defined in a file called Poco.java
"i declared them public isnt it enough??" No, it isn't. Check your lecture notes (or the web) for how to refer to another class's variables.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
1. what are you trying to do here: String public class Poco
2. public String getText()// an error:illegal start of expression
{
return h;
return d;
}
you can have only one return statement executed, so, if you want several returns in one method, it would be a bit like this:
if ( expression )
return h; // is returned if expression = true
return d; // is returned if expression = false
3. public static void main(String args[])throws IOException
{
public String getText()// an error:illegal start of expression
you can't create methods within other methods
4. public static void main(String args[])throws IOException
never have your main method throw an Exception, your main method is the very last place where you can catch and handle any thrown Exception.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
To use instance variables from another class you need an instance of that class, eg
Point p = new Point(); // an instance of a point (x,y coordinates)
p.x // the x instance variable for Point p
To use static variables from another class you don't need an instance, just the class name, eg
Color.red // the public static variable red from the Color class
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Of course it does. That wasn't intended to be a complete statement. I was just posting some fragments of code you show you how to refer to a variable in another class.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
"i want to..." Yes, I understand what you want. I'm telling you the answers
"when i did it.." Did what exactly?
"it says class,interface or enum expected? why this occur?" You have a syntax error - probably incorrect { or }
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
at p.x;
it displays error:not a statement
Okay let me see how I can help, you have a class called Poco which is the main class, yo then have 2 other classes Login and Display... In your main class main method Poco your logic should be to Create an instance of Login class which has 2 private Strings h-username and d-password, and a method-excluding run().
Next I'd take out that while(true) loop in your Login class or else how would you want to get the console to stop prompting for input.... Next in your Login class is the important method: getData() or something similar(but of course awesomely name :P) this method would return a string array of size 2, the first index 0, will store the username, and index 1 will have the password.
class Login extends Thread {
private String h, d;
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner s = new Scanner(System.in);
//no more while true
...
}
public String[] getData() {
String[] arr = new String[2];
arr[0] = h;
arr[1] = d;
return arr;
}
}
So now you'd create an instance of you Login class, and execute the thread, you'd then wait for the thread to die by using ThreadName.join(); once the thread has died you'd use the instance of login class which you ran the thread from and call the getData method which will return 2 string arrays... You'd store the return and then create an instance of the Display class like so in Poco class:
Login l = new Login();
Thread t1 = new Thread(l);
t1.start();
t1.join();//wait
String[] arr = l.getData();//get the data after the thread has ended, instance is still alive
..
Display a = new Display(arr[0], arr[1]);//initiat display class with appropriate user and pass
...
The Display class would have a constructor that takes 2 strings as an argument(username and password) and sets the values of its 2 private stribgs used for the user and pass like so:
class Display extends Thread {
private String h;
private String d;
public Display(String name, String pass) {//constructor
h = name;//sets the h and d values to the arguments passed to constructor upon creation
d = pass;
}
public void run() {
//no more while true
....
}
}
Well i hope that helps...
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
Hi cOrRuPtG3n3t!x
Before taking out the loops and using thread join, and passing the values by return, it would be good to know what the point of this exercise is.
For example - maybe it's suppose to show the values being printed from the Display thread changing each time you enter new ones in the Logon thread. I'm not saying that is right - just saying that we don't know. But if that, or anything like it, is the case, then it calls for concurrent loops and shared variables rather than a return value.
Maybe the OP can clarify?
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Hi cOrRuPtG3n3t!x
Before taking out the loops and using thread join, and passing the values by return, it would be good to know what the point of this exercise is.
For example - maybe it's suppose to show the values being printed from the Display thread changing each time you enter new ones in the Logon thread. I'm not saying that is right - just saying that we don't know. But if that, or anything like it, is the case, then it calls for concurrent loops and shared variables rather than a return value.
Maybe the OP can clarify?
@JamesCherrill Yes, I do see and agree with you, but even if so, at least it will give the OP a working platform to begin extending to what you mentioned. but yes lets see what the OP says and we can help him further :)
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
CoRuPtG3n3t!x
thank u so much,it was nice.but i kept that in loop,as many users simultaneously use this thread Login,we don't have to call that method getdata() from main(),if i use loop it automatically does it,its enough if i just start() the thread. ur array idea is awesome..is it possible to return a value from loop??
and what JamesCherill is absolutely correct i want shared variables..i get this problem very frequently and want to solve it at any cost..
and i want change that password to double type..because i got errors i leaved it as a string..
if both String d,h are diff data types.den how return works??
well i'd just use an object array and convert it where needed, check in the display class how i convert from an object to a string and a double
package poco;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.logging.Level;
import java.util.logging.Logger;
class Login extends Thread {
private String h, d;
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner s = new Scanner(System.in);
//no more while true
System.out.println("Enter username:");
try {//added try catch to get any exception when reading input
h = br.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Enter password:");
d = s.nextLine();
}
public Object[] getData() {
String[] arr = new String[2];
arr[0] = h;
arr[1] = d;
return arr;
}
}
class Display extends Thread {
private String h;
private double d;
public Display(Object name, Object pass) {
h = String.valueOf(name);
d = Double.parseDouble(String.valueOf(pass));
}
public void run() {
//no more while true
System.out.println("ur username is:" + " " + h);
System.out.println("ur password is:" + " " + d);
}
}
public class Poco {
public static void main(String args[]) throws IOException, InterruptedException {//Please Note its not the best to throw execeptions i did this for readability
Login l = new Login();
Thread t1 = new Thread(l);
//added this
t1.start();
t1.join();
Object[] arr = l.getData();//get the data after the thread has ended, instance is still alive
Display a = new Display(arr[0], arr[1]);
Thread t2 = new Thread(a);
t2.start();
t2.join();
System.exit(0);
}
}
i do not though have the knowledge to suggest nothing on the simultaneous side :), however maybe initiating a new Display class each time new values are entered in the Login class? and storing each instance of Display in an Display[] array?
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169