Results 1 to 7 of 7
- 02-17-2012, 11:20 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Begginer needs help with read in file assignment.
I am having a really hard time with this assignment. I have tried everything I can think of for 2 days and the more I try the more I get confused.
Here is what im trying to do. We are asked to read in a file that is in a format that looks like this
smith#john#8632247568#25
and so on.
each number sign represents the stop of a string that i need to pull out and assign to a variable and output in this format:
Name Phone Number SMS
John Smith (863)224-7568 25
and so on till the end of the file.
I first tried to use substring method to do this and select # symbol, but everytime i do it just deletes first name before it when it prints. Im trying delimiter method now, but it outputs somewhat right but then outputs and error. here is sample of my code. It is in very ruff shape so excuss the mess.
Java Code://******************************************************************** // URLDissector.java Java Foundations // // Demonstrates the use of Scanner to read file input and parse it // using alternative delimiters. //******************************************************************** import java.util.Scanner; import java.io.*; public class phoneRecords { //----------------------------------------------------------------- // Reads urls from a file and prints their path components. //----------------------------------------------------------------- public static void main (String[] args) throws IOException { String test; String last; String first; String name; String number; String sms; Scanner nameScan; System.out.println ("Name\tPhone\tSMS"); System.out.println (); nameScan = new Scanner (new File("phoneRecords.txt")); nameScan.useDelimiter("#"); while (nameScan.hasNext()) { last = nameScan.next(); System.out.println(); first = nameScan.next(); number = nameScan.next(); sms = nameScan.next(); System.out.println(first + last + number + sms); } } }Last edited by Norm; 02-17-2012 at 01:00 PM. Reason: added code tags
- 02-17-2012, 12:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Begginer needs help with read in file assignment.
I wouldn't use Scanner.
Just read each line in using a normal BufferedReader, that way you know you have a single entry.
Then split() the String using your delimiter.
Scanner has some quirks that can be counterintuitive to some.
For example, I'm not sure how the above code will handle newline characters (which I suspect is your problem).
- 02-17-2012, 02:55 PM #3
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
Re: Begginer needs help with read in file assignment.
On top ad:
and then:Java Code:import java.io.BufferedReader; import java.io.FileReader;
Then the string called line contains "smith#john#8632247568#25" and then you can split the string.Java Code:String line; FileReader vstream = new FileReader("phoneRecords.txt"); BufferedReader in = new BufferedReader(vstream); line=in.readLine();Last edited by F.S.; 02-17-2012 at 04:37 PM.
- 02-17-2012, 03:08 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Begginer needs help with read in file assignment.
thx for the suggestions.
here is the problem with that though.
teacher wants us to do it a certain way. also there are several more lines in the txt, that was just a sample of one line. the program takes a txt file of several peoples name, phone numbers, and how many messages they sent and each part is only seperated by a #
- 02-17-2012, 03:19 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Begginer needs help with read in file assignment.
OK then, Scanner.nextLine() as the last line in the loop.
If that is not a cure then you;re going to have to show us what is begin output with the current code.
- 02-17-2012, 03:49 PM #6
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
Re: Begginer needs help with read in file assignment.
I have no experience with Scanner, but this is how I solved it.
Maybe you can adjust this program to do it with Scanner:
Java Code:package readfile; import java.io.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * * @author F.S. */ public class Readfile { /** * @param args the command line arguments */ public static void main(String[] args) { String line, symbol="#"; String[] temp; String firstname, lastname, name, number, sms; try{ FileReader vstream = new FileReader("phoneRecords.txt"); BufferedReader in = new BufferedReader(vstream); while((line=in.readLine()) != null){ temp = line.split(symbol); lastname = temp[0]; firstname = temp[1]; number = "("+temp[2].substring(0,3)+")"+temp[2].substring(3,6)+"-"+temp[2].substring(6); sms = temp[3]; name=firstname+" "+lastname; System.out.println(name+" "+number+" "+sms); } in.close(); } catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }Last edited by F.S.; 02-17-2012 at 04:36 PM.
- 02-18-2012, 02:59 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Begginer needs help with read in file assignment.
ok im getting somewhere on it. I figured out how to get the first indexOf # and assign it to a variable so that it will print first name. Now i just need to repeat it for the next # in the file and so on. I cant for the life of me figure out how to get the second indexOf # and so on.
Similar Threads
-
begginer needs help..?
By lina in forum New To JavaReplies: 4Last Post: 01-15-2010, 04:48 PM -
Begginer and project
By Gym in forum New To JavaReplies: 6Last Post: 03-24-2009, 01:30 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
Student--begginer--help!!!
By AmandaIT in forum New To JavaReplies: 6Last Post: 05-09-2008, 03:37 AM -
Tic Tac Toe Ggame Help (Begginer)
By zero2008 in forum New To JavaReplies: 6Last Post: 01-11-2008, 10:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks