Results 1 to 14 of 14
Thread: Changing Scanner input type
- 03-18-2010, 10:50 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
[Solved]Changing Scanner input type
Hiya, I am reading data into a class contructor, using the keyboard with the Scanner class like this:
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?Java Code://removed
Thanks.Last edited by mlad; 03-19-2010 at 02:10 AM.
- 03-18-2010, 11:13 AM #2
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
- 03-18-2010, 11:27 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
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..
- 03-18-2010, 12:36 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 16
- Rep Power
- 0
what you mean that you can open file .txt in java??
- 03-18-2010, 01:38 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
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.
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!Java Code://removed
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 02:24 PM.
- 03-18-2010, 01:43 PM #6
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
- 03-18-2010, 02:01 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
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.
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.Java Code://removed
Last edited by mlad; 03-18-2010 at 02:24 PM.
- 03-18-2010, 02:08 PM #8
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
- 03-18-2010, 02:23 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
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 02:10 AM.
- 03-18-2010, 03:21 PM #10
Split it up and see what's going on. Like this:
But calling the wrong c'tor without exceptions looks like you've forgot to recompile one or more files.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 hereMath problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-18-2010, 03:53 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
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.
- 03-18-2010, 03:59 PM #12
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
- 03-18-2010, 04:21 PM #13
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
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!
- 03-18-2010, 04:22 PM #14
Glad it works now! Have fun.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
problem with Scanner in Getting users input
By kliane in forum New To JavaReplies: 8Last Post: 01-17-2010, 04:37 PM -
speech redirection from one app to other; changing input from mic to socket
By johnyjj2 in forum New To JavaReplies: 0Last Post: 12-11-2009, 11:29 PM -
Taking args consisting of a scanner input
By Implode in forum New To JavaReplies: 1Last Post: 09-27-2009, 09:16 PM -
Persistence problem when changing type to java.sql.Time
By Inks in forum JDBCReplies: 0Last Post: 03-08-2009, 01:52 PM -
Scanner input problem
By slayer_azure in forum New To JavaReplies: 3Last Post: 05-26-2008, 10:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks