Results 1 to 12 of 12
  1. #1
    gradiente99 is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default error in this code

    hi, i trying to create a object of a string[].
    the code is this
    Java Code:
    package Caminando;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Collection;
    import java.util.LinkedList;
    
    
    import javax.swing.text.StyledEditorKit.BoldAction;
    
    public class Solucion {
      public static void main (String[] arr){
         try{
         File f = new File("C:\\Documents and Settings\\gradiente\\Escritorio\\laberinto.txt");
         FileReader fr = new FileReader(f);
         BufferedReader br = new BufferedReader(fr);
         Collection<String> laberinto = new LinkedList<String>();
         String linea;
    
          while((linea = br.readLine())!=null){ 
          laberinto.add(String.valueOf(linea));
          }
    
          for(String i:laberinto){ 
          System.out.println(i);
          }
    
    
         String arreglo = laberinto.toString(); 
    
         [COLOR="Red"]int encontrarEnString(String arreglo, char 0) { //ERROR[/COLOR]
       for (int i = 0; i < arreglo.length(); i++) {
          if (arreglo.charAt(i) == 0)
          System.out.println(i);
           }
        }
    
        }catch(IOException e){
        System.out.println("IOException");
    
           }
    
    
       }
    
    
    }
    the String[] arreglo contains this [XXXXXXXXXX, X0XXXXXXXX, X XXXXXXX, XX XXXXXX, XXX XXXXXX, XXX XX , XXXXXXX XX, XXXXXXX X, XXXXXXXXZX, XXXXXXXXXX, ]
    im need create a object whit char "0" and other object with all "X"
    but I have error.
    Thanks
    Last edited by gradiente99; 07-20-2010 at 03:22 PM. Reason: code tags

  2. #2
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    Replace your code with the following.. which will resolve your prob..

    Java Code:
    int encontrarEnString(String arreglo, char [B]cVar[/B]) { 
    
    [B]cVar = 0;[/B]
    
    for (int i = 0; i < arreglo.length(); i++) {
    if (arreglo.charAt(i) == 0)
    System.out.println(i);
    }
    }
    Arun K R,Bangalore,India
    :)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You need to use CODE tags when posting code, otherwise the code is almost unreadable.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    char cVar) {

    cVar = 0;
    That doesn't make any sense. cVar is passed as a value to be used for some purpose.
    Changing it to 0 will destroy its usefulness.

    I think this test might be wrong:
    if (arreglo.charAt(i) == 0)

    Are you testing for a byte that has a zero value
    or are you testing for a char '0' which does not have a value of zero

  5. #5
    gradiente99 is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That doesn't make any sense. cVar is passed as a value to be used for some purpose.
    Changing it to 0 will destroy its usefulness.

    I think this test might be wrong:
    if (arreglo.charAt(i) == 0)

    Are you testing for a byte that has a zero value
    or are you testing for a char '0' which does not have a value of zero
    yeah you are right, changing this

    Java Code:
    for (int i = 0; i < arreglo.length(); i++) {
    				 if (arreglo.charAt(i) == '0')
    				 System.out.println(arreglo.charAt(i));
    				 }
    				   return ;
    I can show the '0', but how can i create an object?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    how can i create an object
    One way is to use the new statement to create an object.

    What kind of object do you want to create? For what purpose?

  7. #7
    gradiente99 is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    One way is to use the new statement to create an object.

    What kind of object do you want to create? For what purpose?
    i have a .txt with this:

    Java Code:
    XXXXXXXXXX
    X0XXXXXXXX
    X   XXXXXXX
    XX   XXXXXX
    XXX XXXXXX
    XXX        XX 
    XXXXXXX XX
    XXXXXXX   X
    XXXXXXXXZX
    XXXXXXXXXX
    this is a "labyrinth" 0=human, X=walls X=exit
    the firts lines create a collection laberinto, after is converter to string arreglo
    Java Code:
     ( String arreglo = laberinto.toString();)
    resulting this:
    [XXXXXXXXXX, X0XXXXXXXX, X XXXXXXX, XX XXXXXX, XXX XXXXXX, XXX XX , XXXXXXX XX, XXXXXXX X, XXXXXXXXZX, XXXXXXXXXX, ]
    i need create a object with "0" and other object with all "X"
    Thanks

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    need create a object with "0" and other object with all "X"
    What is the "object"? A String?

    String a0 = "0"; // create an object with "0"

    String allXs = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // an object with all "X"

    I guess you want to change the contents of the file. The file contains about 10 lines.
    Each line has some Xs and maybe a 0 and maybe some spaces.

    What are the rules for changing the contents of each row in the file?

  9. #9
    gradiente99 is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What is the "object"? A String?

    What are the rules for changing the contents of each row in the file?
    the human ("0") can only move through space, until exit ("Z").
    no need to change the file, just show the steps in java.
    Thanks.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Ok.
    Can you repeat your question?
    And describe your java coding problem.

    Or is this a program logic problem? You need to find the logic for some game to work.

  11. #11
    gradiente99 is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Ok.
    Can you repeat your question?
    And describe your java coding problem.
    how can i successfully create an object with a "0" and another with the "X" from the String arreglo in the code up?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    create an object with a "0" and another with the "X"
    Here's how to create those objects:
    Java Code:
    Object a0 = new String("0"); // create an object with a "0"
    Object aX = new String("X"); // create an object with a "X"
    from the String arreglo in the code up
    How is this related to creating the objects?

Similar Threads

  1. Code counting error
    By Adde1986 in forum New To Java
    Replies: 2
    Last Post: 03-18-2009, 01:02 AM
  2. Error Code???
    By andmartha in forum New To Java
    Replies: 11
    Last Post: 10-04-2008, 02:16 AM
  3. Help with code (static error)
    By oceansdepth in forum New To Java
    Replies: 1
    Last Post: 03-28-2008, 04:32 AM
  4. Pls help with a code error.
    By saytri in forum New To Java
    Replies: 8
    Last Post: 12-24-2007, 08:10 PM
  5. error in code
    By dirtycash in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 11:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •