473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to fix "illegal start of type" for while loop?

I'm just starting to learn java as part of a class I am required to take as a math major. I am feeling completely lost. I guess we're supposed to learn this as independently as possible. I was watching a video and tried copying the author's code into bluej and it gives me an error.

I apologize in advance if I am not copying it here correctly.
class WhileLoop
{
int counter=0;
while (counter<10)
{
System.out.println (counter);
counter++;
}
}

The error is at the line "while (counter<10) and it is "illegal start of type". I did try putting curly braces before while and at the end of it all but that also got the same error.

I'm still learning and I've tried reading a few posts before posting here. Other posters indicated curly braces were a problem for other problem posts but I don't think I'm missing a complete pair anywhere.

So any advice would be greatly appreciated.

Christina
Feb 12 '11 #1
2 22155
Dheeraj Joshi
1,123 Expert 1GB
While loop has to go inside the method. Java is not similar to C or any other functional oriented languages.

You are allowed to use while and other loop structures inside the method.

Expand|Select|Wrap|Line Numbers
  1. class test{
  2. int count = 0;
  3. public void print_count(){
  4. while(count<10){
  5. system.out.println(count);
  6. count++;
  7. }
  8. }
  9. }
  10.  
  11. class main{
  12. public static void main(string args[]){
  13. test t_obj = new test();
  14. t_obj.print_count();
  15. }
  16. }
  17.  
Try printing it like this. I suggest you read the OOP concepts before starting coding.
This is nice place to start.

Regards
Dheeraj Joshi
Feb 13 '11 #2
Oralloy
985 Expert 512MB
Christina,

Dheeraj has the right answer.

Basically, a class definition is only a declaration of an "object", which contains the definition of the methods of that object.

So, the problem becomes one of determining where your program starts. That is the "static" method named main. There are strict forms for main(), but the one that Dheeraj has shown you is fine for your purposes:
Expand|Select|Wrap|Line Numbers
  1. public static void main(string args[])
  2. {
  3.   // your code here
  4. }
Remember, that the definition of main() must lie inside a class declaration.

Cheers,
Oralloy
Feb 14 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: qe_Cui | last post by:
Hello everyone: I am using vb.net 2003, sqlserver2000 database, and windows 2000 professional OS. In the program, I encrypt the login user's userid and passed with RSACryptoServiceProvider....
0
by: Dave Veeneman | last post by:
I'm using XML Serialization on a class hierarchy headed by a Project Class. When I attempt to serialize, I get an InvalidOperationException from system.xml.dll with the explanation that there was...
2
by: Ray Mitchell | last post by:
Hello, I don't know the correct term for it (tooltip?), but it's the nice feature that, when you are writing source code, pops up a little explanation of the syntax of the particular...
5
by: Alfredo Magallón Arbizu | last post by:
Hi, I have an ASP.NET app that works perfectly in Windows Server 2003, but fails in Windows 2000. It fails when trying to read the data in an Excel Workbook (range.value)... The error is: ...
2
by: David Reynolds | last post by:
Hi Everybody, I have a vb.net webform where I create the Excel application/workbook/worksheet. On Windows 2000 server for some reason, I keep getting an error("Bad variable type") when trying...
6
by: PraZ | last post by:
Hi all. Here is a simple code, which when compiled with gcc results in the warning "incompatible pointer type" for arg 1, as expected. But this is just what I want to do, because it makes it...
0
by: =?Utf-8?B?cmZsYXphcm8=?= | last post by:
Hi All, We are trying to build an automation utility to configure OS. I found a way to automate the process below via registry: Control Panel -Taskbar and Start Menu ->Start Menu Tab --Select...
3
by: Akino877 | last post by:
Hello, I am trying to user a File Chooser with DIRECTORIES_ONLY set. And I would like to be able to hide the "Files of type" label and the associated combo box. I wonder if there is a way to do...
1
by: cppcompiler1000 | last post by:
when i was writing a program i got an error of " illegal structure operation" , in which situation compiler gives this type of error?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.