10 Replies - 1062 Views - Last Post: 01 October 2007 - 05:34 PM Rate Topic: -----

#1 itsjustme82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-September 07

Help with Illegal start of expression

Posted 01 October 2007 - 11:43 AM

Hi ,
I have written a program to compute number of words,letters,number

i have closed all the brac. properly
but still getting an error message saying Illegal start of expression.


here is my code

public class WordCount {

{
		public static void main(String arg[]) throws Exception
		{
				int char_count=0;
				int word_count=0;
				int line_count=0;
				String s;
				StringTokenizer st;
				BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
				System.out.print("Enter filename : ");
				s=buf.readLine();
				buf=in.readLine;
				while((s=buf.readLine())!=null)
				{
						line_count++;
						st=new StringTokenizer(s," ,;:.");
						while(st.hasMoreTokens())
						{
								word_count++;
								s=st.nextToken();
								char_count+=s.length();
						}
				}
				System.out.println("Character Count : "+char_count);
				System.out.println("Word Count : "+word_count);
				System.out.println("Line Count : "+line_count);
				buf.close();
		}
}

}






Please help me with this.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Illegal start of expression

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Illegal start of expression

Posted 01 October 2007 - 11:47 AM

True, your open brackets have matching closing brackets, but take a closer look. How many brackets do you need for a class definition?
Was This Post Helpful? 0
  • +
  • -

#3 itsjustme82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-September 07

Re: Help with Illegal start of expression

Posted 01 October 2007 - 11:53 AM

Hi ,
Thanks,
i was able to fix it but when i compile it it shows that cannot find symbol - variable in.

here i want to take system.in and read that line and count how many letters,words etc are in there.
Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Illegal start of expression

Posted 01 October 2007 - 12:10 PM

Did you declare a variable, "in?"
Was This Post Helpful? 0
  • +
  • -

#5 itsjustme82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-September 07

Re: Help with Illegal start of expression

Posted 01 October 2007 - 12:35 PM

View PostProgrammist, on 1 Oct, 2007 - 12:10 PM, said:

Did you declare a variable, "in?"


no but i wanted to take system.in and use the input function to calculate.
Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Illegal start of expression

Posted 01 October 2007 - 12:43 PM

I know, but your error is a result of you not declaring that variable.

To read user input from the console, many pepe use the java.util.Scanner class. Doing a quick search on this site (using "java scanner") reveals lots of posts that talk about how to use it.

This post has been edited by Programmist: 01 October 2007 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 itsjustme82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-September 07

Re: Help with Illegal start of expression

Posted 01 October 2007 - 12:49 PM

View PostProgrammist, on 1 Oct, 2007 - 12:43 PM, said:

II know, but your error is a result of you not declaring that variable.

To read user input from the console, many pepe use the java.util.Scanner class. Doing a quick search on this site (using "java scanner") reveals lots of posts that talk about how to use it.




Thank you very much.

I think you have helped me a lot.

I am new to programming in java i think you can help me out with my entire program...

I have written a program (server/client) where server is a stand alone and just displays what ever client send to it.

in the program i have written everything and working good till displaying the messages at server end.

now i want to add the above code to my program where it calculates the Lower case,Uppercase ,no of four letter words and total number of words sent to server.


if you want to check my code i can post it too. i was able to get the programming up to here but counting the letters etc is giving me a hard time.

This post has been edited by Programmist: 01 October 2007 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 itsjustme82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-September 07

Re: Help with Illegal start of expression

Posted 01 October 2007 - 12:55 PM

View PostProgrammist, on 1 Oct, 2007 - 12:43 PM, said:

I know, but your error is a result of you not declaring that variable.

To read user input from the console, many pepe use the java.util.Scanner class. Doing a quick search on this site (using "java scanner") reveals lots of posts that talk about how to use it.




The below are my client and server programs.


// Client program
import java.io.*;
import java.net.*;


public class TCPClient
{
private static InetAddress host;

public static void main(String[] args)
{


try {

// Get server IP-address
host = InetAddress.getByName(args[0]);

}
catch(UnknownHostException e){
System.out.println("Host ID not found!");
System.exit(1);
}
run(Integer.parseInt(args[1]));


private static void run(int port)
{
Socket link = null;

try{

// Establish a connection to the server
link = new Socket(host,port);


// Set up input and output streams for the connection
BufferedReader in = new BufferedReader(
new InputStreamReader(link.getInputStream()));

PrintWriter out = new PrintWriter(
link.getOutputStream(),true); 

//Set up stream for keyboard entry
BufferedReader userEntry = new BufferedReader(
new InputStreamReader(System.in));

String message, response;

// Get data from the user and send it to the server
do{
System.out.print("Enter message: ");




message = userEntry.readLine();
out.println(message); 
}while (!message.equals("DONE"));

// Receive the final report and close the connection
response = in.readLine();
System.out.println(response);

}
catch(IOException e){
e.printStackTrace();
}

finally{
try{
System.out.println("\n!!!!! Closing connection... !!!!!");
link.close(); 
}
catch(IOException e){
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}

  



and my Server program is 



/**
 * Write a description of class TCPServer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.io.*;
import java.net.*;

public class Server
{
   private static ServerSocket servSock;
	public static int portNo;
	String test;
	
   public static void main(String[] args)
   {
	
	  System.out.println("Opening port...\n");
	  
	
	  if (args== null|| args.length<1)
	{
		portNo=25000;
	}
else
{ 

   // Create a server object   
		// servSock = new ServerSocket(Integer.parseInt(args[0]));  
	portNo= Integer.parseInt(args[0]);
}
try{
servSock =new ServerSocket(portNo);
   
	  }
	  catch(IOException e){
		 System.out.println("Unable to attach to port!");
		 System.exit(1);
	  }
	  do
	  {
		 run();
	  }while (true);
   }

   private static void run()
   {
	  Socket link = null;
							 
	  try{
   
  // Put the server into a waiting state
		 link = servSock.accept();			   

  // Set up input and output streams for socket
		 BufferedReader in =
			  new BufferedReader(
				   new InputStreamReader(link.getInputStream())); 
		 PrintWriter out = new PrintWriter(link.getOutputStream(),true); 


// print local host name
	 
	String  host = InetAddress.getLocalHost().getHostName(); 
	
	System.out.println("Client has estabished a connection to  " + host);

	

  // Receive and process the incoming data 
		 int numMessages = 0;
		 String message = in.readLine();		
		 while (!message.equals("DONE"))
		 {
			System.out.println(message);
			numMessages++;
			message = in.readLine();
		 }







  // Send a report back and close the connection
		 out.println("Server received " + numMessages + " messages");	
	  }
	  catch(IOException e){
	 e.printStackTrace();
	  }

	//

	  finally{
	try{
	   System.out.println("!!!!!  Closing connection... !!!!!\n" +
							  "!!! Waiting for the next connection... !!!");
	   link.close();				  
	}
	catch(IOException e){
	   System.out.println("Unable to disconnect!");
	   System.exit(1);
	}
	  }
   }
}






Was This Post Helpful? 0
  • +
  • -

#9 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Illegal start of expression

Posted 01 October 2007 - 01:04 PM

I could help you with your entire program, but it's doubtful that I'm going to have time for that. Why don't you take one problem at a time (be specific) and post the any error messages you are receiving. Then, if I have time I'll help. If not, there are numerous people on this site who will be glad to step in.
Was This Post Helpful? 0
  • +
  • -

#10 itsjustme82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-September 07

Re: Help with Illegal start of expression

Posted 01 October 2007 - 01:13 PM

View PostProgrammist, on 1 Oct, 2007 - 01:04 PM, said:

I could help you with your entire program, but it's doubtful that I'm going to have time for that. Why don't you take one problem at a time (be specific) and post the any error messages you are receiving. Then, if I have time I'll help. If not, there are numerous people on this site who will be glad to step in.




Thanks buddy,


Thanks a lot....

I want to take an input and allocate it to buf and then allocate buf to string s once S is assigned i want to count how many Upper case ,lower , numbers and 4 letter words are there in the program.
Was This Post Helpful? 0
  • +
  • -

#11 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help with Illegal start of expression

Posted 01 October 2007 - 05:34 PM

View Postitsjustme82, on 1 Oct, 2007 - 01:13 PM, said:

View PostProgrammist, on 1 Oct, 2007 - 01:04 PM, said:

I could help you with your entire program, but it's doubtful that I'm going to have time for that. Why don't you take one problem at a time (be specific) and post the any error messages you are receiving. Then, if I have time I'll help. If not, there are numerous people on this site who will be glad to step in.




Thanks buddy,


Thanks a lot....

I want to take an input and allocate it to buf and then allocate buf to string s once S is assigned i want to count how many Upper case ,lower , numbers and 4 letter words are there in the program.

Ok. So...what's the problem? Are you getting some sort of error message?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1