I got error :At the constructor MyStackThreads "class , interface or enum expected"

import java.util.*;
import java.io.*;
class MyStackThreads extends Thread
{
 Stack thestack= new Stack();
 String expression;
 String splitexpr[];
 MyStackThreads[j] 
 {
   public void run() 
   {
   try
    {     
     DataInputStream dis=new DataInputStream(System.in);
     expression=dis.readLine();
     for(int i=0;i<expression.length();i++)
     {
         splitexpr=expression.split(expression);
         thestack.push(splitexpr[i]);
     }
    }
    catch(IOException i)
    {
     System.out.println("error in console io"); 
    }
  }
 }
MyStackThreads[j]
 {
  public void run()
   {
    System.out.println(thestack);
   }
 }
MyStackThreads[j] 
 {
  public void run()
   {
      System.out.println("popping out the top");
      thestack.pop();
   }
 }
}
public class StackThread 
{
  public static void main(String args[]) throws IOException
    {
      String exit="tata" ;
      String user_exit="null";
      int user_choice; 
      int j;
      MyStackThreads[] threadobj= new MyStackThreads[3];
      while(exit.equalsIgnoreCase(user_exit))
      {
       System.out.println("what operation do you wanna do with stack");
       System.out.println("Enter the appropiate key below");
       System.out.println("1-insert 2-evaluate 3-display or type tata ");
       DataInputStream dis = new DataInputStream(System.in);
       user_choice=dis.readInt();
       if(user_choice==1)
       {
         j=0;
         threadobj[j].start();
      
       }
       else if (user_choice ==2)
       {
        j=1;  
        threadobj[j].start();  
       }
       else if (user_choice ==3)
       {
          j=2;
           threadobj[j].start();
       }
      else
       {
         System.out.println("user has assigned nothing.May sign out ");
       }
    }
  }
}

Uhm, this MyStackThreads[j] is not a constructor.

A constructor looks like

public ClassName(ParamType param) {
  // do something to "initialise" the class
}

If that is not a constructor , then how can i call run method for individualthread in thread array. MyStackThreads() shows identifier expected error.Plz guide me how to modify to successfully run this program.

You would "create individual threads" by actually calling "new" for each of the elements in the array in your StackThread class.

See ,I am using object array to invoke start() method . If i create individual threads using new i cant use object array like threadobj[j]. that two lines object creation and invoking start() method shows no error.Error is only in the class MyStackThreads. Same error class, interface or enum expected at 28,30,33

See ,I am using object array to invoke start() method . If i create individual threads using new i cant use object array like threadobj[j].

Yes you can

public class Bogus1 {
	public static void main(String[] args) {
		Thread[] t = new Thread[5];
		for (int i = 0; i < t.length; i++) {
			final int value = i;
			t[i] = new Thread(
				new Runnable() {
					public void run() {
				    	try {
				    		Thread.sleep(5);
				    		int j = 0;
				    		while (j++ < 1000000) {
				    			System.out.println(value);
				    			Thread.sleep(1);
				    		}
				    	} catch (InterruptedException ie) {}
					}
				});
			t[i].start();
		}
	}
}

that two lines object creation and invoking start() method shows no error.Error is only in the class MyStackThreads. Same error class, interface or enum expected at 28,30,33

That is not showing an error because "syntactically" it is correct (i.e. those methods exist for that type), but by execution it would result in a NullPointerException.

Still line 8 shows "] expected,; expected" errors and also lines 28, 30 and 33 same "class,interface or enum expected" error.

Post your "new" code.

I tried your recommends for creating thread using "new" operator. Still line 8 shows "] expected,; expected" errors and also lines 28, 30 and 33 same "class,interface or enum expected" error.

import java.util.*;
import java.io.*;
class MyStackThreads extends Thread
 {
 Stack thestack= new Stack();
 String expression;
 String splitexpr[];
 MyStackThreads[j]
 {
  public void run()
   {
   try
    {     
     DataInputStream dis=new DataInputStream(System.in);
     expression=dis.readLine();
     for(int i=0;i<expression.length();i++)
     {
         splitexpr=expression.split(expression);
         thestack.push(splitexpr[i]);
     }
    }
    catch(IOException i)
    {
     System.out.println("error in console io"); 
    }
  }
 }
MyStackThreads[j]
 {
  public void run()
   {
    System.out.println(thestack);
   }
 }
MyStackThreads[j]
 {
  public void run()
  {
     System.out.println("popping out the top");
     thestack.pop();
  }
 }
}
public class StackThread 
{
  public static void main(String args[]) throws IOException
    {
      String exit="tata" ;
      String user_exit="null";
      int user_choice;
      int j;
      Thread[] threadobj= new Thread[0];
      for(int i=0;i<3;i++)
      {
        threadobj[i]=new Thread();
      }
      while(exit.equalsIgnoreCase(user_exit))
      {
       System.out.println("what operation do you wanna do with stack");
       System.out.println("Enter the appropiate key below");
       System.out.println("1-insert 2-evaluate 3-display or type tata ");
       DataInputStream dis = new DataInputStream(System.in);
       user_choice=dis.readInt();
       if(user_choice==1)
       {
         j=0;
         threadobj[j].start();
      
       }
       else if (user_choice ==2)
       {
        j=1;
        threadobj[j].start();
       }
       else if (user_choice ==3)
       {
          j=2;
           threadobj[j].start();
       }
      else
       {
         System.out.println("user has assigned nothing.May sign out ");
       }
    }
  }
}

You still haven't created an actual constructor for your class, and you should, of course, be creating an instance of your class in each of the elements, not just an instance of Thread, of course. That code I posted was just an example and is in no way, shape, or form, cut-n-paste compatible with yours.

And use code tags.

How to create that constructor, since MyStackThreads() shows "identifier expected" error. If i am asking "spoon feeding" excuse me. And i edited the object creation line like this: MyStackThreads[] threadobj= new MyStackThreads[3];
Threadobj= new MyStackThreads();
previously it was
Thread[] threadobj= new Thread[3];
Threadobj= new Thread();

Uhm, maybe with something like

public MyStackThread(int userChoice) {
    //assign some instance value
}

That is, of course, just the start.

But that wont going to use those multiple threads na. I want that each of the action should use a thread. If i pass user's choice inside constructor means there will be no work for threads. Can you guide me in designing such a program ? And other thing is I can have multiple run methods in a class na, but they should be in separate constructors, Am i correct?

In other words, you create an instance for each possible response (and the class should implement runnable rather than extending thread) and then, when the user enters a choice you create a thread using that Runnable from the array and start it.

I did it.But u didnt mention anything about constructor and run method. I started a new thread for each action . still i got errors in constructors and run() methods.
Here is the new code:

import java.util.*;
import java.io.*;
class MyStackThreads implements Runnable
{
 Stack thestack= new Stack();
 String expression;
 String splitexpr[];
 public MyStackThreads()
 {
  public void run()
   {
   try
    {     
     DataInputStream dis=new DataInputStream(System.in);
     expression=dis.readLine();
     for(int i=0;i<expression.length();i++)
     {
         splitexpr=expression.split(expression);
         thestack.push(splitexpr[i]);
     }
    }
    catch(IOException i)
    {
     System.out.println("error in console io"); 
    }
  }
}
public MyStackThreads()
 {
  public void run()
   {
    System.out.println(thestack);
   }
 }
public MyStackThreads()
 {
  public void run()
  {
     System.out.println("popping out the top");
     thestack.pop();
  }
 }
}
public class StackThread 
{
  public static void main(String args[]) throws IOException
    {
      String exit="tata" ;
      String user_exit="null";
      int user_choice;
      int j;
      MyStackThreads[] threadobj= new MyStackThreads[3];
      while(exit.equalsIgnoreCase(user_exit))
      {
       System.out.println("what operation do you wanna do with stack");
       System.out.println("Enter the appropiate key below");
       System.out.println("1-insert 2-evaluate 3-display or type tata ");
       DataInputStream dis = new DataInputStream(System.in);
       user_choice=dis.readInt();
       if(user_choice==1)
       {
         j=0;
         Thread tobj= new Thread(threadobj[j]);
         tobj.start();
       }
       else if (user_choice ==2)
       {
        j=1;
        Thread tobj= new Thread(threadobj[j]);
        tobj.start();
       }
       else if (user_choice ==3)
       {
          j=2;
          Thread tobj= new Thread(threadobj[j]);
          tobj.start();
       }
      else
       {
         System.out.println("user has assigned nothing.May sign out ");
       }
    }
  }
}

Bacause you have the same constructor signature three times. Like I said, use one constructor that simply sets an instance variable or two. Then, whether implemented or extended you still need to define a run method in the class definition (not inside the constructor, of course) and that run method can do different things depending on the values of those instance variables.

Do you have any clue as to what it is you are doing? Or are you just groping around in the dark hoping to find the door before falling down the stairs?

for the above pasted code there are 9 errors 2 "illegal start of exression for each run() " and 1 "; expected" for each run()

As masijade said, look at your constructors. You cannot have multiple constructors with the same parameters, which in this case, is no parameters. And you also wrote your run method inside the constructor. You cannot write a method inside another method.

And what did I say? The "run" method shjould not be defined inside the constructor, of course. And you need have only one constructor. Otherwise three constructors with a signature of the class name and no arguments will also not work, as I've already said. A constructor is essentially a method and two methods can have the same name only when their parameter list varies (by type).

Edit: This reply was "overlapped" by the previous reply. ;-)

This error i got :
run() already defined in class. How to overload it?
My new code is:

import java.util.*;
import java.io.*;
class MyStackThreads implements Runnable
{
 Stack thestack= new Stack();
 String expression;
 String splitexpr[];
 MyStackThreads(java.lang.Thread tobj)
    {
     
    }
 public void run()
   {
   try
    {     
     DataInputStream dis=new DataInputStream(System.in);
     expression=dis.readLine();
     for(int i=0;i<expression.length();i++)
     {
         splitexpr=expression.split(expression);
         thestack.push(splitexpr[i]);
     }
    }
    catch(IOException i)
    {
     System.out.println("error in console io"); 
    }
    }
   public void run()
    {
      System.out.println("poping out the top");
      thestack.pop();
    }
}

 
public class StackThread 
{
  public static void main(String args[]) throws IOException
    {
      String exit="tata" ;
      String user_exit="null";
      int user_choice;
      int j;
     
      while(exit.equalsIgnoreCase(user_exit))
      {
       System.out.println("what operation do you wanna do with stack");
       System.out.println("Enter the appropiate key below");
       System.out.println("1-insert 2-evaluate 3-display or type tata ");
       DataInputStream dis = new DataInputStream(System.in);
       user_choice=dis.readInt();
       Thread tobj[]= new Thread[3];
       if(user_choice==1)
       {
         j=0;
        
         MyStackThreads runablobj= new  MyStackThreads(tobj[j]);
         runablobj.run();
       }
       else if (user_choice ==2)
       {
        j=1;
        MyStackThreads runablobj= new  MyStackThreads(tobj[j]);
        runablobj.run();
       }
       else if (user_choice ==3)
       {
          j=2;
          MyStackThreads runablobj= new  MyStackThreads(tobj[j]);
          runablobj.run();
       }
      else
       {
         System.out.println("user has assigned nothing.May sign out ");
       }
    }
  }
}

By only defining it once?

I got it. that's threads are not useful for doing different operations or applications. But for doing same operation or application by different users or threads. I am bit confused with meaning of thread and i have to go through the syntaxes thoroughly. Thank you.

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.