Results 1 to 2 of 2
- 11-04-2010, 02:32 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
assigning strings and int from a file and then manipulating the data
Hi, I am very, very new to Java and I have a homework assignment, which (obviously) I have left to the very last minute.
I have read from a file and displayed the information, and seemed to have spilt the information up into strings. Now I need to use those strings: change two of them into int and then add up the int seperately...which I have no idea how to do. Tried a number of things, such as a Demlimiter but didn't work - possibly as I did not use it correctly!
This is the information I am reading from the file
"Leeds United : Liverpool : 1 : 2
Chelsea : Manchester City : 1 : 1
Aston Villa : Middlesbrough : 3 : 1
Tottenham Hotspur : Stoke City : 0 : 0"
Can anyone help please??!!
Here is my code that I have got so far
"
Java Code:import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Generator { /** * @param args * @throws FileNotFoundException when the file cannot be loaded */ public static void main(String[] args) throws FileNotFoundException { Scanner s = new Scanner(new File("Demo.txt")); // create a scanner which scans from a file String line; // stores the each line of text read from the file while ( s.hasNext() ) { line = s.nextLine(); // read the next line of text from the file String [] splitupText = "Demo.text".split(":"); // split the text into multiple elements for ( int i = 0; i < splitupText.length; i++ ) { // loop over each element String nextBit = splitupText[i]; // get the next element (indexed by i) nextBit = nextBit.trim(); // use the trim method to remove leading and trailing spaces try { // "try" is a special statement which allows us to deal with "exceptions" int num = Integer.parseInt(line); // attempt to convert the String into an Integer type value } catch (NumberFormatException e) { // The number did not parse for some reason System.out.println("Invalid number entered"); } System.out.println(line); // output the line of text to the console } } } }
"
The try method doesn't parse properly and displays the error message after each string.
Like
"Invalid number entered
Leeds United : Liverpool : 1 : 2
Invalid number entered
Chelsea : Manchester City : 1 : 1
Invalid number entered"Last edited by Eranga; 11-04-2010 at 04:29 PM. Reason: code tags added
- 11-04-2010, 02:42 PM #2
Take the problem one step at a time. Which part of this are you working on now? Post an SSCCE that demonstrates just that part, then post a specific question about that specific part.
Programming is hard. Waiting until the last minute isn't a great idea.
When posting code, make sure you use the code tags to preserve formatting. Otherwise people won't want to read it.
Similar Threads
-
Storing a LOT of strings / data?
By Atriamax in forum Advanced JavaReplies: 5Last Post: 05-07-2010, 09:07 AM -
Assigning a good Icon to the executable jar file
By praveen_has in forum AWT / SwingReplies: 3Last Post: 04-28-2010, 03:39 PM -
Manipulating URLs
By TheFlying_Boy in forum NetworkingReplies: 0Last Post: 08-03-2009, 05:01 PM -
Manipulating XML
By JosephMConcepcion in forum XMLReplies: 2Last Post: 04-26-2009, 12:01 AM -
comparing strings in notebook to names of a file
By debz in forum New To JavaReplies: 8Last Post: 02-20-2009, 12:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks