import java.io.*;
import java.util.*;
import java.lang.*;

 class Login extends Thread
{

       
    public void run()
      {        
             
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            Scanner s=new Scanner(System.in);
             while(true)
               { 
                System.out.println("Enter username:");  
            
              String h=br.readLine();
                   System.out.println("Enter password:");

                     String d=s.nextLine();
               
                 }
                
        }

}

 class Display extends Thread
{
        
       public void run()
         {
                 
               while(true)
                  {
                      System.out.println("ur username is:"+" "+h);
                      System.out.println("ur password is:"+" "+d);
      
                   }
           }
}


public class Poco
{
      
           public String h;
              public double d;
       
     public static void main(String args[])throws IOException
       {
             
     
            Login l=new Login();
              Display a=new Display(); 
            Thread t1=new Thread(l);
              Thread t2=new Thread(a);

         }

}

even if i declare the variables as public,it still shows can not find symbol
and i get an unreported exception and it says that it should be thrown

help.........

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?

poco.java:18: unreported exception java.io.IOException;must be caught or declared to be thrown
String h=br.readLine();
poco.java.37:cannot find symbol
symbol:variable h
location class display
System.out.println("ur username is:"+" "+h);

poco.java.38: cannot find symbol
symbol:variable d
location class display
System.out.println("ur password is:"+" "+d);

if i use Scanner the exception is gone but still cannot find symbol error comes

i forgot to start() the threads
i did that too but still no use

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.

i declared them public isnt it enough??

and if i make the display class public too,then it shows an error that it should be declared in a file named display.java??
i frequently get that error why??

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.

String public class Poco
{

          String h;
         String d;
     public static void main(String args[])throws IOException
       {

           public String getText()// an error:illegal start of expression

           {
            return h;
            return d;
           }

            Login l=new Login();
              Display a=new Display();

            Thread t1=new Thread(l);
              Thread t2=new Thread(a);
         t1.Start();
         t2.Start();

         }

}    

i tried this,but still

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.

iam trying to use the other classes variables by using return()
for that i have to specify the return type to that class na..

2.wat if want two values??wat chould i do??

3.nested methods..i kept that in class Login but still no use

4.thanks i will do that

please help..

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

at p.x;
it displays error:not a statement

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.

i want to those variables in other class that has no main()
wen i did it,it says class,interface or enum expected??
why this occur??i hate that error

"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 }

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...

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?

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 :)

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??

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?

The main problem with shared variables is that when classA changes a value, classB doesn't know that it's changed. In your original code you had a loop that just kept re-reading the variable, but that's not something you can do in real life.

In real life you would chose one class to be the "owner" of the data. That class would have the variables private, and have public get/set methods for them.
When you instantiate the other classes you pass a ref to the "owner" class to they can call its get/set methods.
Finally, you implement a "listener" pattern in the owner class. This is just like all the event listeners in Swing - classB implements a callback method, and add itself as a listener to the owner class. As part of the set method, the owner class calls back all its listeners to let them know the new value.

Maybe that's all too much for this exercise, but if you are looking towards real-life techniques, I can supply more details and some useful code fragments.

here is what i ment... I dont know if it will work for your specfic needs but..:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
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 {

    Display[] dispArr = new Display[100];
    int i = 0;
    private String h, d;

    public void run() {

        while (true) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            Scanner s = new Scanner(System.in);
            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();
            dispArr[i] = new Display(h, d);
            dispArr[i].start();
            try {
                dispArr[i].join();
            } catch (InterruptedException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            i++;

        }


    }
}

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() {
        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();
        System.exit(0);

    }
}

however ofcourse it might be better to have the Display[] in your main... but i just did mine as an example

thank you so much,i will try to improve my skills,i started learning java just 20 days back,,and learning one by one module slowly.i dont know what this swing and listener do..
i think i should improve my skill before i do this..

thank you so much,i will try to improve my skills,i started learning java just 20 days back,,and learning one by one module slowly.i dont know what this swing and listener do..
i think i should improve my skill before i do this..

Just wanted to give you maybe some sort of other example that uses synchronized variable:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package synthread1;

import java.util.Scanner;

class Display extends Thread {

    public static int count = 0;

    Display(String threadname) {
        super(threadname);
    }

    @Override
    public void run() {
        display(getName());
    }

    public synchronized void display(String threadN) {
        while (true) {
            System.out.println(threadN + count);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}

class GetInput {

    Display s2;

    public void askforever() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int tmp = sc.nextInt();
            Display.count = tmp;
        }
    }
}

public class SynVar {

    public static void main(String[] argse) {

        Display s1 = new Display("Thread1: ");
        s1.start();

        Display s2 = new Display("Thread2: ");
        s2.start();
        GetInput gi = new GetInput();
        gi.askforever();
    }
}

Maybe not the best example but still shows how i had a main class SynVar and a main class which initiates two instances of the class GetInput and Display and calls a method in GetInput to get the user input, and then another in Display which displays what number the user entered each time after 3 second intervals

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.