Results 1 to 6 of 6
Thread: Using scanner method with Split
- 02-28-2013, 04:34 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Using scanner method with Split
Hi guys,
I am trying to make a GUI program which opens a specific TXT (JFileChooser) and I am using Scanner to Read the TXT file. So far so good, now I want to implement the Split(" "); method to split lines with space and throw them in a Array. Here is where my problem begins.
The reason I want to use Split is because I want to manipulate the numbers, like add them all together to a SUM.
The TXT files looks like this:
Java Code:JHJHDFJ 2621646 KJSKFSd 4564123 JHSDGdf 1546545 SDFKBN 156545 NAME NUMBER .......... ........
The Code:
I am not sure about my while loop and how to implement Split so it displays NAME \n NUMBER succesfully in the CONSOLE, and how I can use NUMBER and calculate the sum of all numbers and display it in CONSOLE.Java Code:btnFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JFileChooser chooser = new JFileChooser("C:\\"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); FileNameExtensionFilter filter = new FileNameExtensionFilter( " Only .txt", "txt"); chooser.setFileFilter(filter); int code = chooser.showOpenDialog(null); if (code == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); Scanner input; try { input = new Scanner(selectedFile); String line = null; String name = null; String number = null; |||||||-> while (input.hasNextLine()){ line = input.nextLine(); String[] splits = line.split(" "); name = splits[0]; number = splits[1]; System.out.println(name); System.out.println(number); } <-||||||| input.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Its my first time working with Scanner and Split method so maybe I didnt get the basics right. Can anyone please guide me in the right direction?
Thanks.
- 02-28-2013, 04:42 PM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Using scanner method with Split
Is your text file contents formatted in a fixed-length column? I see in the example you post that the number is aligned at a specific column. If that you can use substring instead of split.
Website: Learn Java by Examples
- 02-28-2013, 04:47 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Using scanner method with Split
No it can vary. Its also an assignment so it has to be the Split method.
- 02-28-2013, 06:02 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Using scanner method with Split
For summing the numbers you need to have somewhere to hold your ongoing total as you loop over the file.
Then you need to add each number to it, which means you need to use parse the String holding the number.Please do not ask for code as refusal often offends.
- 02-28-2013, 06:39 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Using scanner method with Split
Tolls, thanks for replying.
I am new to Java, so if you can give me code example that would be very helpfull.
- 03-01-2013, 10:52 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
String split method
By DavidG24 in forum New To JavaReplies: 5Last Post: 12-14-2010, 02:01 PM -
String split method
By counterfox in forum New To JavaReplies: 2Last Post: 11-12-2010, 11:06 PM -
Please help me with Split method> String
By javanew in forum New To JavaReplies: 5Last Post: 04-02-2010, 06:15 PM -
split method question
By Chasingxsuns in forum New To JavaReplies: 3Last Post: 11-19-2009, 07:19 PM -
problem with split method
By abhiN in forum New To JavaReplies: 7Last Post: 02-10-2009, 01:54 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks