?
Solved

Problem in execution

Posted on 1998-11-23
2
Medium Priority
?
183 Views
Last Modified: 2010-03-30
Hello,

  Consider the following code.

import javlib.in.*;

class testforss1
{
            testforss1()
            {
                  prt.disp("ur in the base class");
            }
}
      
class testforss
{
            private testforss1[] t1 = new testforss1[10];
            private int x;

            testforss()
            {
                  System.out.println("Its okay");
            }
            Random rand = new Random();
            int x1 = rand.nextInt();
            
            System.out.println("The output is " + (int)x1);
            public static void main(String[] args)
            {
                        testforss[] t = new testforss[1];
                        System.out.println("The length is " + t.length);
            }
}

I am just trying to print the variable x1 when i compile this code it is saying the following error

C:\SSLIB\testforss.java:23: Type expected.
            System.out.println("The output is " + (int)x1);
                               ^
1 error
Finished

What might be the error??

regards
Nature
0
Comment
Question by:nature
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 60 total points
ID: 1228036

in the object definition you can have
-) field definitions (that is  Random rand = new Random(); )
-) method definitions (that is public void main(String[] args) {})
but not random pieces of code.

the error that you receive

>> C:\SSLIB\testforss.java:23: Type expected.
>> System.out.println("The output is " + (int)x1);

means that comiler is expecting another field or method definition, not a code ...

so - you must put this code in your constructor or some other method ...

hope this helps
  heyhey

0
 

Author Comment

by:nature
ID: 1228037
Yep it works thanks

nature
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question