Results 1 to 2 of 2
  1. #1
    NDBASS is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    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

  1. Storing a LOT of strings / data?
    By Atriamax in forum Advanced Java
    Replies: 5
    Last Post: 05-07-2010, 09:07 AM
  2. Assigning a good Icon to the executable jar file
    By praveen_has in forum AWT / Swing
    Replies: 3
    Last Post: 04-28-2010, 03:39 PM
  3. Manipulating URLs
    By TheFlying_Boy in forum Networking
    Replies: 0
    Last Post: 08-03-2009, 05:01 PM
  4. Manipulating XML
    By JosephMConcepcion in forum XML
    Replies: 2
    Last Post: 04-26-2009, 12:01 AM
  5. Replies: 8
    Last Post: 02-20-2009, 12:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •