9 Replies Latest reply on Oct 21, 2009 5:30 PM by 843789

    Scanner to String problem. It seems like i can't convert the input to a str

    843789
      Hello folks
      I made this program to compare to text files. It reads the two files and prints out the difference between them. It works with the code i commented out, and when i know the exact name of the files. But i want some kind of interaction that keeps asking for a files that exist inside the directory. But when I try to run the code that i made for that, I get a error. It seems like i can't convert the input to a string. Can anyone tell me what i'm doing wrong?
      Thanks.
      import java.io.*;
      import java.util.*;
      
      
      public class Ch6pp2
      {
          public static void main(String[] args) throws FileNotFoundException
          {
               Scanner tastatur = new Scanner(System.in);
              //calls the getInput method
              Scanner input1 = getInput(tastatur);
               Scanner input2 = getInput(tastatur);
               String one = input1.toString();
               String two = input2.toString();
      
              //calls the compare two files method
               compareTwoFiles(one, two);
          }
              //this method ask for a file name until it finds a file name that exist inside the directory
           public static Scanner getInput(Scanner tastatur)
          {
              Scanner result = null;
              while (result == null)
              {
                  System.out.print("input file name? ");
                  String name = tastatur.nextLine();
                  try
                  {
                      result = new Scanner(new File(name));
                  } catch (FileNotFoundException e)
                  {
                      System.out.println("File not found.  "
                                         + "Please try again.");
                  }
              }
              System.out.println();
              return result;
          }    
      //it works with this code for input. but only if you know the exact name to type
          /*
               System.out.println("Enter a first file name: ");
               Scanner tastatur = new Scanner(System.in);
              String one = tastatur.next();
              System.out.println("Enter a second file name: ");
               Scanner tastatur2 = new Scanner(System.in);
              String two = tastatur.next(); 
              compareTwoFiles(one, two); 
      */
      
           
           //this method compares the two Strings
          public static void compareTwoFiles(String one, String two) throws FileNotFoundException
          {
                
                File f = new File(one);
              Scanner input = new Scanner(f);
              File g = new File(two);
              Scanner input2 = new Scanner(g);
                
                int count = 1;
                String text = "";
                String textSecond = "";
                System.out.println("Difference found:");
                while (input.hasNextLine())
                {
                     text = input.nextLine();
                     textSecond = input2.nextLine();     
                     if (!text.equals(textSecond))     
                     {
                          System.out.println("Line " + count + ":");
                          System.out.println("< " + text);
                          System.out.println("> " + textSecond);
                          System.out.println();                     
                     }     
                     count++; 
                }
                          
                
           }
      }
        • 1. Re: Scanner to String problem. It seems like i can't convert the input to a str
          843789
          1) You never told us what error you were getting.

          2) Your commented out code and you getInput() method do different things.
          Take a look at what the variables "one" and "two" are both cases.
          I think you will see that given the same inputs they are not the same in both cases.
          (I mean that the value of "one" is different between the two cases.)
          • 2. Re: Scanner to String problem. It seems like i can't convert the input to a str
            843789
            I'd say the problem is that you're reading the file names, creating Scanners for them, returning the Scanners, and then trying to get the filenames back from the Scanners. If all you really need is the filename, then the intermediary Scanners are unnecessary.

            Either that or you should be using those Scanners in your compare method rather than trying to send it filenames.

            Edited by: staphen on Oct 21, 2009 7:37 AM
            • 3. Re: Scanner to String problem. It seems like i can't convert the input to a
              843789
              sorry i forgot. I get this error:
              Exception in thread "main" java.io.FileNotFoundException: java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E] (File name too long)
                   at java.io.FileInputStream.open(Native Method)
                   at java.io.FileInputStream.<init>(FileInputStream.java:106)
                   at java.util.Scanner.<init>(Scanner.java:621)
                   at Ch6pp2.compareTwoFiles(Ch6pp2.java:57)
                   at Ch6pp2.main(Ch6pp2.java:14)
              • 4. Re: Scanner to String problem. It seems like i can't convert the input to a
                843789
                to johndjr: I don't understand what you mean when you say: "Take a look at what the variables "one" and "two" are both cases.
                I think you will see that given the same inputs they are not the same in both cases".
                (I mean that the value of "one" is different between the two cases.)

                to staphen: what intermediary Scanners ?
                • 5. Re: Scanner to String problem. It seems like i can't convert the input to a
                  843789
                  baudits wrote:
                  to staphen: what intermediary Scanners ?
                  To clarify, your first method call grabs a filename from System.in using tastatur, then creates an (intermediary) Scanner, and returns it. You then proceed to use the (intermediary) Scanner's toString() method to get the filename so you can send them to your second method. If you plan on sending filenames to your second method, then there's no reason to use the intermediary Scanners. If you plan to use the Scanners returned from the first method in your second method, then you will need to modify your second method.
                  • 6. Re: Scanner to String problem. It seems like i can't convert the input to a
                    843789
                    Im planning to send filenames to the second method compareTwoFiles(). So if I don't need the intermediary Scanners how do I prompt the user to type in a file. I don't know how i else can do that. What do you suggest?

                    Edited by: baudits on Oct 21, 2009 8:13 AM
                    • 7. Re: Scanner to String problem. It seems like i can't convert the input to a
                      843789
                      baudits wrote:
                      Im planning to send filenames to the second method compareTwoFiles(). So if I don't need the intermediary Scanners how do I prompt the user to type in a file. I don't know how i else can do that. What do you suggest?

                      Edited by: baudits on Oct 21, 2009 8:13 AM
                      Notice that I didn't specify tastatur as being one of the intermediary Scanners. Also, think about what you're sending to your compare method (you already told me you're sending filenames), then decide what you need to return from your getInput() method.
                      • 8. Re: Scanner to String problem. It seems like i can't convert the input to a
                        843789
                        baudits wrote:
                        to johndjr: I don't understand what you mean when you say: "Take a look at what the variables "one" and "two" are both cases.
                        I think you will see that given the same inputs they are not the same in both cases".
                        (I mean that the value of "one" is different between the two cases.)
                        In the code that you commented out you did this
                        //it works with this code for input. but only if you know the exact name to type
                        System.out.println("Enter a first file name: ");
                        Scanner tastatur = new Scanner(System.in);
                        String one = tastatur.next();
                        but in the code that does not work you did this
                        Scanner input1 = getInput(tastatur);
                        String one = input1.toString();
                        What is the one in each of these cases?
                        If you can not figure it out, try printing them out.

                        Also here's a hint, I don't think that Scanner.toString() does what you think it does.
                        • 9. Re: Scanner to String problem. It seems like i can't convert the input to a
                          843789
                          Thanks for the help both of you. I finally got the code to do what i want. Sometimes you just get blocked on one solution. You look at it one way and just want to solve it that way. Probably because it worked when you did something similar.