Can you input data from a file & place it outside the main method?

When I input integers into my program from an outside text file, I can easily use those numbers when assigned to variables inside the main method, but when I try to use them outiside the main method, the program will not recognize them.

I am bringing the integers into the main method, assigning them to variables, and then trying to use them here:

publicclass Maze

{

//These should be imported from a text file.txt

(This is where I am trying to assign the integers input from the file)

finalstaticint mazeWidth = msg, mazeHeight=15, enterRow=14, enterCol=17, exitRow=0, exitCol=33,

north=0, east=1, south=2, west=3;

//#ABE

staticprotectedchar maze[] =newchar[ mazeWidth * mazeHeight ];

//Main Method

staticpublicvoid main(String[] args)

{

(THIS is where the integers are being brought in from the text file)

}

}

Thanks for your help.

Message was edited by:

louy

[1614 byte] By [louya] at [2007-10-3 8:10:20]
# 1
Declare the variables outside the main method. Variables that are defined in a method can only be used in that method.
CaptainMorgan08a at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 2
But I can't used BufferedReader to read in integers from a text file outside the main method....right?
louya at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 3

No, that needs to be inside the method.

public class SomeClass

{

int someInt, someOtherInt;//the variables you need to use

public static void main(String[] args)

{

//initialize the variables in here

}

}

CaptainMorgan08a at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 4
What do you want to do?
Norweeda at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 5

> No, that needs to be inside the method.

> > public class SomeClass

> {

> int someInt, someOtherInt;//the variables you

> need to use

>

> public static void main(String[] args)

>{

>//initialize the variables in here

>

> }

When I try to do that, it says that the variable is final and, "cannot assign a value to final variable mazeWidth." So if I make it so mazeWidth is no longer 'final' then I get this error: 'constant expression required' (in my switch)

louya at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 6
Virtually nothing should be inside your main method, actually. Just a little bit of code to bootstrap your program.Message was edited by: paulcw
paulcwa at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 7
Try posting your code. "final" variables can never be changed once they have been initialized.
CaptainMorgan08a at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 8
> What do you want to do?I want to bring in an integer from a text file and assign it to mazeWidth. That is basically what I want to do (except to many different variables).
louya at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 9
Use a properties file and a java.util.Properties object.
paulcwa at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 10

I should have known that assigning a new value to a FINAL value would be impossible. This means my entire algorithm is off, and the only way to cure this is to write a new one. (I think)

Unless there is some way to initialize and declare the variable inside the main method, but then I think the rest of the program wouldn't recognize it anyway. I think I tried that anyways.

Message was edited by:

louy

louya at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 11
> This means my entire> algorithm is off, and the only way to cure this is to> write a new one. (I think)That sounds like a good idea. Youre code sounds very flawed to me, but its hard to tell without actually seeing it.
CaptainMorgan08a at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 12
I doubt that final-ness would break an algorithm. Or an algorithm that's any good, anyway.What were you planning on doing exactly?
paulcwa at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 13
I am using the variables to construct a maze. It already works, I just need to add a feature that allows the variables to be input from a file. It looks like it is not going to happen. But you win some, you lose some.
louya at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 14
You can easily add variables from a file.But why did it depend on the variables being final?
paulcwa at 2007-7-15 3:14:42 > top of Java-index,Java Essentials,New To Java...
# 15

Well I already have added variables from a file, it is just that the variables can't be accessed from outside the main method. The variables need to be final because they are part of a switch block. Also, they are used in a char array that is defined outside the main method because it has to be used throughout the program in many methods.

Thank you so much for helping, however.

Message was edited by:

louy

louya at 2007-7-21 12:20:20 > top of Java-index,Java Essentials,New To Java...
# 16

You can read from a file and set variables from its contents, completely outside of a main method. The variables would be accessible outside of a main method.

Variables do not need to be final to be part of a switch.

I suggest that you stop trying to get help with a flawed implementation; rather explain what you're actually trying to accomplish -- not just give glimpses of your flawed implementation.

paulcwa at 2007-7-21 12:20:20 > top of Java-index,Java Essentials,New To Java...
# 17

I am generating a maze of X's, then knocking down walls (X's) , then travelling through the maze and marking my path (o's). Is that what you are asking me? The variables I want to input are used in generating the maze. I.E. maze height, maze width, entrance column, entrance row, exit column, exit row, etc.

louya at 2007-7-21 12:20:20 > top of Java-index,Java Essentials,New To Java...
# 18

import java.io.*;

import java.util.Properties;

public class Maze {

private int height; // non-final, set in constructor

private final int width; // final, set in constructor

public static void main(String[] argv) {

(new Maze(argv[0])).start();

}

public Maze(String filename) {

Properties ps = new Properties();

try {

FileInputStream fis = new FileInputStream(filename);

ps.load(fis);

} catch (IOException e) {

e.printStackTrace();

}

height = Integer.parseInt(ps.getProperty("height"));

width = Integer.parseInt(ps.getProperty("width"));

}

public void start() {

switch(height) { // switching on a non-final

case 10: System.out.println("height is 10"); break;

case 20: System.out.println("height is 20"); break;

default: System.out.println("height is not 10 or 20");

}

switch(width) { // switching on a final

case 10: System.out.println("width is 10"); break;

case 20: System.out.println("width is 20"); break;

default: System.out.println("width is not 10 or 20");

}

}

}

Properties file:

$ cat stuff.properties

height: 20

width: 10

Run:

$ java Maze stuff.properties

height is 20

width is 10

Please don't say "I have to do it this way" until you have more experience.

paulcwa at 2007-7-21 12:20:20 > top of Java-index,Java Essentials,New To Java...
# 19

To add to what Paul said about there should be virtually nothing in the main method, take a look at this example.

class Maze {

// declare variables here

Maze() {

initialise();

run();

}

private void initialise() {

// read in values and assign to variables

}

private void run() {

// solve maze or whatever you are trying to do

}

public static void main(String[] args) {

new Maze();

}

}

All you main method does is create a new Maze object. All the actual code will be in the methods of the Maze class.

floundera at 2007-7-21 12:20:20 > top of Java-index,Java Essentials,New To Java...
# 20
Thanks.
louya at 2007-7-21 12:20:20 > top of Java-index,Java Essentials,New To Java...