Results 1 to 14 of 14
  1. #1
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default [Solved]Changing Scanner input type

    Hiya, I am reading data into a class contructor, using the keyboard with the Scanner class like this:
    Java Code:
    //removed
    However I'd like also to like to be able to read in data from a text file using a method in another class. Is there a way to change the input type using (eg, using a parameter in the constructor) so that I can replace "System.in" with "new FileReader("H:\\...\\...\\file.txt")" when needed?

    Thanks.
    Last edited by mlad; 03-19-2010 at 03:10 AM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Sure
    Java Code:
    public Person()
        {
        Scanner in = null;
        if(readfromFile){
            in = new Scanner(new File(path));
        } else {
            in = new Scanner(System.in);
        }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Of course, thanks very much for helping. I think I have realised it won't work however, because I need the constructor to be called repeatedly (while in.hasNextLine()) for the text file, meaning the scanner has to be declared outside of the constructor. Stupid me..

  4. #4
    karno is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

    Default

    what you mean that you can open file .txt in java??

  5. #5
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    No, I can just use the scanner to keep reading the next occurance of a string or int from it.

    Incidentally, I'm now using the scanner to read details from the file and then use them as parameters for a constructor, but again it's not working.

    Java Code:
    //removed
    I get an exception when it tries to create the new Demonstrator in the readDemonstratorData method. Is it possible to do it this way? I was trying to use a text file with the information in order seperated by spaces which didn't work, but putting them on seperate lines (and deleting the spaces) doesn't work either. I believe it is having trouble reading the strings. Any ideas? Help is appreciated!

    payrollArray is a ArrayList field of the Payroll class, which is why I'm using the .add() method.
    Last edited by mlad; 03-18-2010 at 03:24 PM.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    First you should find out what you are reading:
    Java Code:
    public static void readDemonstratorData()
        {
            try
            {
                Scanner in = new Scanner(new FileReader("H:\\CSC1012\\Coursework\\Payroll\\DemonstratorData.txt"));
                
                while(in.hasNextLine())
                {
                    String line = in.nextLine();
                    System.out.println(line);
                    //split the line i
                } 
            } catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    I tried that code and it helped me figure it out, it was working, I just hadn't written the text file correctly :s Thanks! However, it is calling the default constructor to the class instead of the one that takes parameters. The default one uses it's superclass Person which is similar but asks the user to enter different details.
    Java Code:
    //removed
    So when I run the readDemonstratorData it opens the terminal and asks me to type the details in that way, instead of just entering the data it is reading.
    Last edited by mlad; 03-18-2010 at 03:24 PM.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Now you've lost me. Post the code you have now and a few lines of the file you're trying to read.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Alllll the code? There are about 300 lines in 6 seperate classes :s, but I can if you need.

    Basically, what I mean is it turns out the Scanner is correctly reading the file, as when I tried your suggested test code it reproduced the text exactly. The problem initally was that I had not written the text file properly.

    Now, the demonstrator has two constructors, the first for manually entering the details (with no parameters, but it's own Scanner class for keyboard entry into the terminal), and the second with parameters. My code is using the first one despite having the parameters listed when it is intialised, but I need it to use the second.

    It is concerning this code again:
    Java Code:
    //removed
    Last edited by mlad; 03-19-2010 at 03:10 AM.

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Split it up and see what's going on. Like this:
    Java Code:
    Scanner in = new Scanner(new FileReader("H:\\CSC1012\\Coursework\\Payroll\\DemonstratorData.txt"));
                
                while(in.hasNextLine())
                {
    
                    String one = in.next();
                    String two = in.next();
                    System.out.println(one);
                    System.out.println(two);
                    // same for all c'tor parameters.
                    Demonstrator newDemonstrator = new Demonstrator(one, two, in.nextInt(), in.nextInt(), in.nextInt(), in.next(), in.nextInt(), in.nextDouble(), in.nextDouble()); //**calls incorrect constructor here
    But calling the wrong c'tor without exceptions looks like you've forgot to recompile one or more files.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  11. #11
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Ok, I've tried that. It still prints out the correct information, but still the wrong constructor. I don't know what I've done here.

    The Demonstrator class is a subclass to a Person class, and it's default constructor (the one that's being called, but I don't want) has a super(); call in it, but the second one (that I want to be used) doesn't. Could that have any effect?

    I can't have forgotten to complie anything as I'm using BlueJ, you can't run anything if a part is not compiled.

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    It might be compiled, but in an earlier version. What do I know? To help you you need to create an SSCCE
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  13. #13
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Ok, it's because some of the variables I was assigning were inherited from the superclass, and I didn't have a constructor that corresponded to the one I wanted to use from the Demonstrator. I had assumed that as Demonstrator was a subclass I could assign inherited variables without calling the super constructor. Thanks very much for helping!

  14. #14
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Glad it works now! Have fun.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. problem with Scanner in Getting users input
    By kliane in forum New To Java
    Replies: 8
    Last Post: 01-17-2010, 05:37 PM
  2. Replies: 0
    Last Post: 12-12-2009, 12:29 AM
  3. Taking args consisting of a scanner input
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-27-2009, 10:16 PM
  4. Replies: 0
    Last Post: 03-08-2009, 02:52 PM
  5. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 11:49 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
  •