Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is it possible to change the class string to read from a file instead of declaring there?  RSS feed

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i have a class called Class1 in that i have a string called name. that name is used to print the values. is it possible to change the class string to read from a file instead of declaring there?
here is the code


the code i have tried to use with a file


i am getting error as follows.

[clrg@localhost check]$ javac Class2.java
Class2.java:11: illegal start of type
while ((name = br.readLine()) != null) {
^
Class2.java:11: illegal start of type
while ((name = br.readLine()) != null) {
^
Class2.java:11: ')' expected
while ((name = br.readLine()) != null) {
^
Class2.java:11: ';' expected
while ((name = br.readLine()) != null) {
^
Class2.java:11: <identifier> expected
while ((name = br.readLine()) != null) {
^
Class2.java:11: ';' expected
while ((name = br.readLine()) != null) {
^
6 errors

what is wrong in the above coding. i am not sure whether i can used it. If my method is wrong. what is the correct method. Please suggest me.. Have a nice week....
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct it line no 11:-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Its (System.in) and add at top import java.io.*; put BufferedReader code in try catch block

try{

while ((name = br.readLine()) != null) {
// Print the content on the console
System.out.println (name);
}


}

catch(Exception e)
{
System.out.println(e);

}
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:what is wrong in the above coding

The problem is more basic than anything to do with reading from the file. You aren't allowed to have executable statements at the class level, only declarations. That entire while loop ought to be in a method.

Once you've fixed that it may reveal other errors (as Ganesh suggests), but that's the cause of the error you're seeing right now.
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either you can keep methods or member variables inside the class, why you are keeping while() statement inside class?? If you want to read from a file keep your code inside static block.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Pat

Correct it line no 11:-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Its (System.in) and add at top import java.io.*; put BufferedReader code in try catch block

try{

while ((name = br.readLine()) != null) {
// Print the content on the console
System.out.println (name);
}


}

catch(Exception e)
{
System.out.println(e);

}

After i change that and change the static String into public static String name; i am getting that errors.

Errors
[clrg@localhost check]$ javac Class2.java
Class2.java:15: illegal start of type
try{
^
Class2.java:15: ';' expected
try{
^
Class2.java:17: illegal start of type
while ((name = br.readLine()) != null) {
^
Class2.java:17: <identifier> expected
while ((name = br.readLine()) != null) {
^
Class2.java:17: <identifier> expected
while ((name = br.readLine()) != null) {
^
Class2.java:17: ';' expected
while ((name = br.readLine()) != null) {
^
Class2.java:17: illegal start of type
while ((name = br.readLine()) != null) {
^
Class2.java:17: ';' expected
while ((name = br.readLine()) != null) {
^
Class2.java:24: class, interface, or enum expected
catch(Exception e)
^
Class2.java:28: class, interface, or enum expected
}
^
Class2.java:31: class, interface, or enum expected
public void class1Method() {
^
Class2.java:33: class, interface, or enum expected
}
^
Class2.java:35: class, interface, or enum expected
public String getName() {
^
Class2.java:37: class, interface, or enum expected
}
^
Class2.java:39: class, interface, or enum expected
public void setName(String name) {
^
Class2.java:41: class, interface, or enum expected
}
^
16 errors


harshvardhan ojha
Either you can keep methods or member variables inside the class, why you are keeping while() statement inside class?? If you want to read from a file keep your code inside static block.



i want to read the contents inside a file from one class (i.e it should not be in one class) and the output of that file should be declared in main class.so i am using this code. If some other better way to perform. Kindly suggest me with the example. i am trying to move a string from one file that has class to another the main class.but unable to proceed. So i try to see whether this suits. Thanks for your replies.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is the same as before - all that code should be in a method, not directly in the class body.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have changed the coding with the method. but still some errors come. i am unable to correct it. some one help me.


Errors:
[clrg@localhost check]$ javac Class2.java
Class2.java:25: cannot find symbol
symbol : variable name
location: class Class1
System.out.println("Welcome in " + name);// + " Its id number : " + IdNo);
^
Class2.java:29: cannot find symbol
symbol : variable name
location: class Class1
return name;
^
Class2.java:33: cannot find symbol
symbol : variable name
location: class Class1
this.name = name;
^
Class2.java:75: cannot find symbol
symbol : constructor Class1()
location: class Class1
Class1 class1 = new Class1(); // Creating object of Class1
^
4 errors

If use the "}" braces. i am getting more errors in the coding.



Is it not possible to use public void inside the method?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:i have changed the coding with the method. but still some errors come. i am unable to correct it. some one help me.

Errors:
[clrg@localhost check]$ javac Class2.java
Class2.java:25: cannot find symbol
symbol : variable name
location: class Class1
System.out.println("Welcome in " + name);// + " Its id number : " + IdNo);

^

When you get these kind of errors, you can be certain that you have not declared the variable used, correctly.
Try to figure out whether the scope of variable "name" is available till the place where you are using it..
Hint: - Scope of a variable is confined to the Block in which it is defined.

deepika deepi wrote:
Class2.java:75: cannot find symbol
symbol : constructor Class1()
location: class Class1
Class1 class1 = new Class1(); // Creating object of Class1
^
4 errors

Have you defined a Zero- arg constructor??? or Even using the default constructor provided by Compiler?? FIND OUT.. and you are done..

deepika deepi wrote:
Is it not possible to use public void inside the method?

Why would you need to do that?
"public void" collectively is access modifier and return type of a method. (Neither of them you can use inside a Method.

Well, return type is almost always used before the methodName..
and modifier public is used with method or instance or class variable, or anything that is inside a class (Not inside a method)..


I Would suggest you to go with a simple Java program rather that jumping straight to File Reading.
Write a simple program where you declare all kinds of variables, constructor and Some methods... ANd then understand the flow of the Program..

It will be easy to you to move forward..
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:

Have you defined a Zero- arg constructor??? or Even using the default constructor provided by Compiler?? FIND OUT.. and you are done..


can you tell me the difference between Zero-arg constructor and default constructor ?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:
can you tell me the difference between Zero-arg constructor and default constructor ?

I think he want to say have you declared the default constructor or not ? because if you don't declare any constructor then compiler will give default one (Zero argument) and if you declared any constructor then you have to mention the default constructor as well, as compiler will not provide that constructor now.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:
deepika deepi wrote:
can you tell me the difference between Zero-arg constructor and default constructor ?

I think he want to say have you declared the default constructor or not ? because if you don't declare any constructor then compiler will give default one (Zero argument) and if you declared any constructor then you have to mention the default constructor as well, as compiler will not provide that constructor now.


Default constructor is a Zero-Arg constructor provided by the compiler if we haven't defined any constructor...

** Zero-Arg constructor that we define is not called as default constructor, it is just called Zero-Arg constructor..
Default is the one that Compiler gives...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:i have changed the coding with the method. but still some errors come

But you haven't changed the problem I told you about. A try block is not a method.

You really need to go back and re-read about what a class looks like. Compare your Class1 with Class2. In Class2 everything's in a method, as it should be.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote: . . . I think he want to say have you declared the default constructor or not ? because if you don't declare any constructor then compiler will give default one (Zero argument) and if you declared any constructor then you have to mention the default constructor as well, as compiler will not provide that constructor now.
R Jain has already told you that you cannot write a default constructor, only a no‑arguments constructor. You must only provide the constructors you need; a no‑arguments constructor may be unnecessary andit might be harmful to provide one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!