how to use scanner correctly

Reply

Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

how to use scanner correctly

 
0
  #1
Sep 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: how to use scanner correctly

 
0
  #2
Sep 25th, 2008
Make it a class level variable - not a public static member.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 18
Reputation: Aashath is an unknown quantity at this point 
Solved Threads: 5
Aashath's Avatar
Aashath Aashath is offline Offline
Newbie Poster

Re: how to use scanner correctly

 
0
  #3
Sep 25th, 2008
Yes ..
it has to be simply

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:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
"The most important single aspect of software development is to be clear about what you are trying to build." - Bjarne Stroustrup
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

Re: how to use scanner correctly

 
0
  #4
Sep 25th, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: how to use scanner correctly

 
0
  #5
Sep 25th, 2008
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC