This site requires JavaScript, please enable it in your browser!
Greenfoot back
CKL
CKL wrote ...

2012/11/1

How to use Scanner

1
2
CKL CKL

2012/11/1

#
my code is like this
    public void load() throws Exception   
    {
        File inputFile = new File("a.txt");
        Scanner input = new Scanner("a.txt");
        int num;
        boolean state;
        while(input.hasNext())    
        {
            int a = input.nextInt();
            startPerson = a;
            
        }
        input.close();
    }     
and my txt file is like this
1 0 6 true 2 1 false 0 5 false 6 1 false 0 5 true 4 2 false 0 4 false 2 0 false 0 4 false 5 2 false 0 3 false 3 2 false 0 3 false 1 0 false 0 2 true 1 2 false 0 2 false 0 1 false 0 1 false 4 1 false 0 1 false 7 0 false 0 0 false 0 0 false 0 0 true 4 0 false 0 0 false 3 1 false
How can I read the first int as the "int startperson"that i decide before second and third to be another thing, each "false" can be used to be boolean type As this is a save, the lines in the txt files will be varied every time. How can I get the number of the lines on the text file? Thank you
zhuvell zhuvell

2012/11/1

#
Read the first line before starting the while loop, for the "int startperson". For the line count, simply introduce a new int which will hold the number of lines(it should equal 0 at first), then add +1 to it every time you read a new line (in the while loop). I hope this helps.
CKL CKL

2012/11/2

#
thanks for your answering. i would like to ask which function I should use to call the first line?? and for example Could I get the third no. in the third line which means the x coordinate, the forth one means the y coordinate?
kiarocks kiarocks

2012/11/2

#
I would advise you to use BufferedReader and FileInputStream's, they are much better for files. Example:
public void load() throws Exception     
{  
    BufferedReader in = new BufferedReader(new FileReader("a.txt"));
    int num = 0;  
    String t = "";
    //boolean state;  //What is this used for?
    while((t = in.readLine()) != null) //Ensures that there is something to read
    {  
        num++; //Increase num by 1
        startPerson += safeParse(t)+" ";
    }  
    in.close();  
}       
Add a "safeParse" method:
public String safeParse(String in)
{
     try {
          return ""+Integer.parseInt();
     }
     catch(NumberFormatException nfe) {
        return in;
     }
}
And this will give you a string called startPerson which will contain (based on your example) the string "1 0 6 true 2 1 false 0 5 false 6 1 false 0 5 true 4 2 false 0 4 false 2 0 false 0 4 false 5 2 false 0 3 false 3 2 false 0 3 false 1 0 false 0 2 true 1 2 false 0 2 false 0 1 false 0 1 false 4 1 false 0 1 false 7 0 false 0 0 false 0 0 false 0 0 true 4 0 false 0 0 false 3 1 false " with no line breaks. If you want me to give you a place to store the X and Y coords I can help with that as well.
CKL CKL

2012/11/2

#
Oh I got it. However should be easier to store the data like an array matrix? As the first line would only contain int, and move to next line. The column of the array won't change, it is only the rows of the array changes every time I write the game.txt
CKL CKL

2012/11/2

#
I have edited something but still don't know how to achieve it The problem is I want to store as two array one is to store true/false, one is to store the number and the first line which has only one number represents the startperson
    public void load() throws Exception   
    {
        File inputFile = new File("a.txt");
        Scanner input = new Scanner("a.txt");
        int [][] num;  // <--Could I do this to store a nx4 array where [0][0] is like the text file shown eg. 0
        String [][] state;//<-- Could I do this to store a nx2 array which only contain true and false
        
boolean exists = inputFile.exists();
        if( exists ){   
            while(input.hasNext()){    
                 startPerson = ???; //<--How can I get the first number in the text    
                 for(int row=0;row<the number of line in the text ; row++){//how to set the number of line in the text    
                      for(int col=0;col<2;col++){    
                           String state[row][col] = input.next(); //<--I want this to store true false in that nx2 array    
                      }    
                 }    
                 for(int row=0;row<the number of line in the text ; row++){ //how to set the number of line in the text    
                      for(int col=0;col<2;col++){    
                         num[row][col] = input.nextInt(); // I want to store the numbers into a array starting to read from the second line    
                      }    
                 }    
                 for (int i=0;i<number of line in the text;i++) { //this loop need to be correct but dun know how    
                     Object c = new Object(num[i][0], num[i][1], state[i][0]);      
                     state[i][1] = c.selected;    
                     addObject(c, num[i][2], num[i][3]);    
                 }                                      
            }
                 input.close();    
            }else return;   
kiarocks kiarocks

2012/11/3

#
You seem to persist in your usage of Scanner, would you like me to keep Scanner or would you like me to use BufferedReaders?
CKL CKL

2012/11/3

#
its all ok as I dun know how to use BufferedReaders-.-
CKL CKL

2012/11/3

#
Sorry I have a question to ask how can I store this text file(I have editted) into a single string the first number means the game start or not from -1 to 1, 0 start by first player, 1 start by second player, -1 means the game has not yet started the second number means how many rows in the below array. How can I store this array into string
0 32 0 6 4 2 0 0 0 5 1 1 0 0 0 5 6 2 0 0 0 4 1 3 0 0 0 4 7 1 0 0 0 3 3 0 0 0 0 3 7 0 0 0 0 2 5 2 0 0 0 2 5 1 0 0 0 1 2 2 0 0 0 1 6 0 0 0 0 0 4 0 0 0 ......
kiarocks kiarocks

2012/11/3

#
I'd like to know what you are trying to do with this save/load, so that I can maybe write a better method for you and clean up some code.
CKL CKL

2012/11/3

#
ok actually I want to store the info in the world at that moment just like a game save. when I press reset and click void load() , what the world look like in the save will immediately come out
CKL CKL

2012/11/3

#
the first line I store is " which player start" , player 1 or 2 , I use 1 means the player 1, 0 means player 2. the second line (the number) is to count the how many lines below (the array) i use the following code to write the array. .... writer.println(startPerson); for(int i=0;i<numOfObjects;i++){ Object object = objectss.get(i); writer.print(object.color() + " " + object.which() + " " + object.ex() + " " + object.getX() + " " + object.getY() + " " + object.selected); writer.println(); } writer.close();
kiarocks kiarocks

2012/11/4

#
that code will not work. Object has no methods.
CKL CKL

2012/11/4

#
Sorry for making you confuse. Maybe I should ask the question step by step. First, I would like to ask how can I make this text file into a string like what you did in the beginning eg. 1 0 6 0 0 0 0 0 5 0 1 0 0 0 5 0 3 0 0 0 4 6 0 0 0 0 4 1 0 0 0 ......
1 0 6 0 0 0 0 0 5 0 1 0 0 0 5 0 3 0 0 0 4 6 0 0 0 0 4 1 0 0 0 0 3 0 2 0 0 0 3 4 2 0 0 0 2 2 1 0 0 0 2 2 0 0 0 0 1 3 3 0 0 0 1 7 1 0 0 0 0 4 1 0 0 0 0 3 0 0 0 0 0 4 0 0 0 0 0 5 1 1 0 0 0 3 1 1 0 1 6 2 3 0 0 1 5 1 1 0 0 1 5 6 2 0 0 1 4 3 2 1 1 1 4 5 0 0 0 1 3 5 2 0 0 1 3 1 2 0 0 1 2 1 3 0 0 1 2 7 2 0 0 1 1 7 3 0 0 1 1 2 2 1 0 1 0 7 0 0 0 1 0 4 3 1 0 1 0 6 3 0 0 1 0 5 3 0 0 1 0 6 1 0 0
CKL CKL

2012/11/4

#
Second, how can I get the number of line in the string? In the above text file, could I use text.length() since I have some functions need to be done in every line? Third, how can I call each number in the string back in every line. Can I use "int value = (int) text.charAt(i);" in a for loop where "i" is the position of the numbers in the string of each line? The for loop is doing every line in the String.
There are more replies on the next page.
1
2