Results 1 to 7 of 7
  1. #1
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default Scanner and bufferreader

    Hi everyone,

    im new to the forum so i hope ive posted in the correct section, i have a method to write which loads a single text file with predefined text in it which basically is a team name with their points, the team name is always the first line in the text file, the code below is an attemp at this method, if the first line of the text file is equal to the name of the file being loaded, it should print out the line "this is correct" however no matter what the varable group name is being comparede to it always jumps in the loop, i could compare it to a random string "bbddshdokdih" and it would still print out the "this is correct" could anyone please advise, thanks.

    Java Code:
     public void loadTeams()
       {
          OUDialog.alert("Select input file for " + this.getGroupName());
          String pathname = OUFileChooser.getFilename();
          File aFile = new File(pathname);
          BufferedReader bfr = null;
          try
          {
               String currentLine;
               bfr = new BufferedReader(new FileReader(aFile));
               currentLine = bfr.readLine();
               Scanner aScanner = new Scanner(currentLine);
               aScanner.useDelimiter(",");
               String groupName = aScanner.next();
                if(groupName==(this.getGroupName()));
               {
                  System.out.println("this is correct");
                }
               
               }
             catch (Exception anException)
          {
             System.out.println("Wrong file selected");
          }
          finally
          {
             try
             {
                bfr.close();
             }
             catch(Exception anException)
             {
                System.out.println("Could not close the file");
             }
          }
          }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Use the equals() method to compare strings instead of ==.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Your code also has another evil in it.
    You have if(groupName==(this.getGroupName()));
    {
    ...

    Surely you didn't want to put that ; at the end of the if line?

  4. #4
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    haha thanks so much guys, thats sorted it, both those two mistakes where preventing it from working correctly, i owe u a pint :p

  5. #5
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default Further Aid

    hey again, stuck on another issue, wonder if any kind sole could point me in the rite direction, the code below similar to my previous, instead of printing out this is correct,i have to carry on readin the rest of the file using CSV and extracting the strings in there, the problem i have however is how i have set this method up currentLine only ever tests the first line, do i need a while loop in there somewhere to iterate through the rest of the text file, thanks

    Java Code:
    public void loadTeams()
       {
          OUDialog.alert("Select input file for " + this.getGroupName());
          String pathname = OUFileChooser.getFilename();
          File aFile = new File(pathname);
          BufferedReader bfr = null;
          try
          {
               String currentLine;
               bfr = new BufferedReader(new FileReader(aFile));
               currentLine = bfr.readLine();
               Scanner aScanner = new Scanner(currentLine);
               aScanner.useDelimiter(",");
               String groupName = aScanner.next();
                if(groupName.equals(this.getGroupName()))
               {
                  System.out.println("this is correct");
                }
               
               }
             catch (Exception anException)
          {
             System.out.println("Wrong file selected");
          }
          finally
          {
             try
             {
                bfr.close();
             }
             catch(Exception anException)
             {
                System.out.println("Could not close the file");
             }
          }
          }

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Yes, you need a loop. Also, where is your data going to go? You should check the first line first, then pass the BufferedReader to a separate method that will read the rest of the file.

    Also, you should be more careful checking your Exceptions. Your first catch block seems to assume it will only receive a FileNotFoundException, but it could conceivably get a NoSuchElementException from the Scanner as well. You should explicitly specify FileNotFoundException in your catch statement to avoid confusion.

    -Gary-

  7. #7
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    my data is going to be used to create a new object with a specific string argument, this will be extracted from the text file, i know what im doing with tht bit, its just the loop and where it goes is my issue, thanks for your time dude

Similar Threads

  1. Help With Scanner
    By jtmoney0511 in forum New To Java
    Replies: 10
    Last Post: 10-12-2009, 11:24 PM
  2. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 02:11 PM
  3. Question: BufferReader/StringTokenizer
    By VinceGuad in forum New To Java
    Replies: 1
    Last Post: 01-26-2009, 05:50 PM
  4. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  5. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 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
  •