Lost in Java File and GUI

[b][red]This message was edited by Darkstorm at 2002-10-21 14:23:11[/red][/b][hr]
Hi!
I am currently writting a program, that packs a number of boxes, with the sizes read from a text file and the size of the board where the boxes are to be put in is also read from the text file. On the board, the boxes's position as which will be put first are randomly choosen.
The first problem is I don't know whether my program is reading from the file or not.
Secondly I don't know how to draw the boxes out after it is placed on the board.

Below are my code. Thanks a lot for your time and help.
(Code for reading from file text)
try{
BufferedReader in = new BufferedReader(new FileReader(file));


String line;
while ((line = in.readLine()) !=null)
{

line=in.readLine();
totalBoxes = Integer.parseInt(line);
line=in.readLine();


StringTokenizer st=new StringTokenizer(line);
boardRow=Integer.parseInt(st.nextToken());
System.out.println ("E");
originalBoardCol=Integer.parseInt(st.nextToken());
boardColumn=originalBoardCol * 2;


for(int i=0; i<totalBoxes; i++)
{
line=in.readLine();
//StringTokenizer st=new StringTokenizer(line);
rectangles[0][i]=Integer.parseInt(st.nextToken());
rectangles[1][i]=Integer.parseInt(st.nextToken());

}


} //while close bracket
in.close();
}
catch (IOException e)
{
System.out.println ("E");
}


Comments

  • : Hi!
    : I am currently writting a program, that packs a number of boxes, with the sizes read from a text file and the size of the board where the boxes are to be put in is also read from the text file. On the board, the boxes's position as which will be put first are randomly choosen.
    : The first problem is I don't know whether my program is reading from the file or not.
    : Secondly I don't know how to draw the boxes out after it is placed on the board.


    // here are some tips that hopefully will be helpful
    **********************************************************
    //what if u pass an argument to your static main.

    public static void main( String [] args) {


    NameOfyourClass identifier = new NameOfyourClass(args[0]);
    // if I were u Id put it in a try block just in case the file is
    not present in your directory.

    } // end of static main

    // then u have the constructor
    public NameOfyourClass( String whatever) {

    //After u created a String up there like String nameOfyourString = "";

    nameOfyourString = whatever;

    //and use nameOfyourString to be StringTokenized or whatever u need to
    //do. To make sure it is the right file, use print command to test it
    //out, after any while loop.
    //Run the program: java nameOfyourFile nameOftheFileTobeReadIn
    :
    //: then have try - catch block and use InputStreamReader like that

    -> : try {

    InputStreamReader inr = new InputStreamReader(nameOftheString);

    } // end of try

    catch (Exception e) {

    e.printStackTrace();
    }

    : (Code for reading from file text)
    : try{

    : BufferedReader in = new BufferedReader(new FileReader(file));
    //Instead, do in = new BufferedReader(inr);
    // The rest of your code looks fine to me
    :
    :
    : String line;
    : while ((line = in.readLine()) !=null)
    : {
    :
    : line=in.readLine();
    : totalBoxes = Integer.parseInt(line);
    : line=in.readLine();
    :
    :
    : StringTokenizer st=new StringTokenizer(line);
    : boardRow=Integer.parseInt(st.nextToken());
    : System.out.println ("E");
    : originalBoardCol=Integer.parseInt(st.nextToken());
    : boardColumn=originalBoardCol * 2;
    :
    :
    : for(int i=0; i<totalBoxes; i++)
    : {
    : line=in.readLine();
    : //StringTokenizer st=new StringTokenizer(line);
    : rectangles[0][i]=Integer.parseInt(st.nextToken());
    : rectangles[1][i]=Integer.parseInt(st.nextToken());
    :
    : }
    :
    :
    : } //while close bracket
    : in.close();
    : }
    : catch (IOException e)
    : {
    : System.out.println ("E");
    : }

    } //END OF YOUR CONSTRUCTOR
    :

  • Hi!
    Thanks a lot but I am still kind of confuse...i have in the constructor write this initialise(filename). Initialise is a method that i call. Then how can i work on it and how can i change it? I am really confused and lost.
    Thanks a lot and i hope you can help me further.
  • : Hi!
    : Thanks a lot but I am still kind of confuse...i have in the constructor write this initialise(filename). Initialise is a method that i call. Then how can i work on it and how can i change it? I am really confused and lost.
    : Thanks a lot and i hope you can help me further.
    :
    *******************************************

    are u telling me that you dont have a "static void main" method ? If it is the case, the only for you to read from a file is to create that flle in your prgram. FileInputStream in = new FileInputStream(nameOFfile);

    But with the static void main, u dont need to. U pass arg[0], itll add a command line thats y to run it , u'd have to type :
    "java FileName input". Otherwise, using a method "initialise" will complicate things I think. Maybe itll be easier for you.

    Sorry man I cant help further, Im as new in java as u are. Maybe newer, but I faced your problem before, thats y I can talk otherwise... blank...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories