Results 1 to 7 of 7
Like Tree4Likes
  • 1 Post By KevinWorkman
  • 1 Post By KevinWorkman
  • 1 Post By quad64bit
  • 1 Post By quad64bit

Thread: Scanner doubt

  1. #1
    Wryfyng is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Scanner doubt

    Hello, first of all sorry for my English. I'm trying to do a program to read a file using Scanner. The problem is that I need to read what is the content of each line twice and while I use entrada.nextLine() it jumps to the next line. Here it is a part of the program. Thanks.

    public static void cargaDatosP(){
    Scanner entrada= null;
    try {
    entrada = new Scanner (new FileInputStream("personas.txt"));
    } catch (FileNotFoundException e) {
    System.out.println("Error abriendo el fichero");
    System.exit(-1);
    }
    while(entrada.hasNextLine()){
    String[] campos=new String[10];
    for(int i=0;entrada.hasNextLine();i++) { //Here I need to stop and go back to the while when I find a "*" in the file
    campos[i]=entrada.nextLine();
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,693
    Rep Power
    8

    Default Re: Scanner doubt

    If you need to examine the content of whatever is returned by nextLine(), simply store it in a String value and do whatever you want with it.
    Wryfyng likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Wryfyng is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Scanner doubt

    ok, thanks, that's solved, now the problem is that when I compare the String I've used with a "*" it doesn't match with it, though when I print the content of the String it appears "*". do u know why does it happen?
    I've heard sth in class about putting "\\*" instead or sth like that.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,693
    Rep Power
    8

    Default Re: Scanner doubt

    You'll have to post the code you're using to match the String. Are you using a method from the String class? Are you using Patterns?
    Wryfyng likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Scanner doubt

    How are you doing your string compare? Not with == I hope?
    Wryfyng likes this.

  6. #6
    Wryfyng is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Scanner doubt

    lol solved, I was doing it with ==
    thanks a lot for the quick answers

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Scanner doubt

    No problem! Common mistake. Have fun!
    Wryfyng likes this.

Similar Threads

  1. a doubt
    By Thia in forum New To Java
    Replies: 13
    Last Post: 11-24-2009, 02:26 AM
  2. have doubt
    By ras_pari in forum Advanced Java
    Replies: 3
    Last Post: 10-01-2009, 09:51 AM
  3. doubt
    By Chris555 in forum Networking
    Replies: 0
    Last Post: 08-05-2009, 02:58 PM
  4. Doubt
    By winkodes in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 12-20-2008, 08:34 PM
  5. doubt in jms
    By veena in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 01-05-2008, 01:42 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
  •