943,715 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6605
  • Java RSS
Sep 25th, 2008
0

how to use scanner correctly

Expand Post »
how do i declare a scanner object, filescan publicly so that i may use filescan in varios functions

i found this statement in a forum---->
private static Scanner scanner = new Scanner (1 3 4 4");

when I modified it to----->
public static Scanner filescan = new Scanner(new File("inputdata.txt"));

//declared in the main

i get the following error message
Illegal modifier for parameter scanner; only final is permitted

Thank you guys
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 25th, 2008
0

Re: how to use scanner correctly

Make it a class level variable - not a public static member.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,759 posts
since May 2007
Sep 25th, 2008
0

Re: how to use scanner correctly

Yes ..
it has to be simply

Quote ...
Scanner filescan = new Scanner(new File("inputdata.txt"));
FYI :

Scanner is from - java.util.Scanner

A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

For example, this code allows a user to read a number from System.in:

Quote ...
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
Reputation Points: 11
Solved Threads: 5
Newbie Poster
Aashath is offline Offline
18 posts
since Dec 2007
Sep 25th, 2008
0

Re: how to use scanner correctly

Java Syntax (Toggle Plain Text)
  1. public class praogram1
  2. {
  3. Scanner scanner = new Scanner (new File ("t.txt"));
  4. public static void main(String[] args)
  5. {
  6. }
  7. }

ok..im tryin to define it at the class level and i run into this issue now. How do i throw an IOException at this level

Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Sep 25th, 2008
0

Re: how to use scanner correctly

Java Syntax (Toggle Plain Text)
  1. public class Program1 {
  2.  
  3. Scanner scanner = null;
  4.  
  5. public Program1(){
  6. try {
  7. scanner = new Scanner(new File("t.txt"));
  8. } catch (FileNotFoundException fe){
  9. fe.printStackTrace();
  10. }
  11. }
  12.  
  13. public static void main(String[] args) {
  14.  
  15. }
  16. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,759 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to Store two different class objects in Same array. - new to Java ..Need Help
Next Thread in Java Forum Timeline: help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC