Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Unknown Character

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Unknown Character

    Hey, I have a question where I just need like a brainstorming thing to happen.

    I am writing a Fantasy Football Offline Draft Manager Program. I read in an Excel workbook that contains tables copied and pasted from ESPN's Fantasy Football Projections. In those tables, the Players are referenced by the following format:
    FirstName LastName, Team Position
    Now, the problem is with the Team. Teams are named via their 2 or 3 letter abbreviation. Immediately proceeding the Team is, what appears to be, a space. And for most of the Teams with 3 letter abbreviations, there is no problem. But for the Teams with 2 letter abbreviations, there seems to be a character before that space that I cannot recognize.

    For example, when I read in Green Bay, I read in the initials GB. BUT, it reads in like this 'GB '. Notice the extra space after the abbreviation? I attempted to use the String.trim() method that gets rid of those pesky spaces, but it didnt do anything. I tried adjusting my hardcoded array of teams to include spaces for teams with 2 letter abbreviations, but the program cannot recognize the abbreviation. This tells me that there is a character that isnt a space that is there as a filler or something. I know it is not an indent because Excel would have reacted when I pasted in the table. Any clues as to how I can find out what the hell it is and/or how to deal with it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Unknown Character

    To see what char is there, use the String charAt() method and then the Integer.toHexString() method to display the char in hex.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    if(nflTeam.substring(0,2).equals("GB"))
        					System.out.println(Integer.toHexString(nflTeam.charAt(2)));
    Based on what you said, I did that. It returns:
    a0
    What is that character in English terms?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Unknown Character

    What is that character in English terms?
    Better question is: What is that character in ASCII terms?
    Find a table of ASCII code values and look it up. The largest ASCII char you can type from a keyboard has a value of 7E: (~)
    I don't know what they are above that exactly. Some are for European letters that aren't used in English.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    ASCII Code - The extended ASCII table
    says it is a "Non-breaking space". So, if it is in fact a space, then why isnt Java treating it so?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Unknown Character

    if it is in fact a space, then why isnt Java treating it so?
    Can you explain: "treating it so"?

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Unknown Character

    Non-breaking space is a common element in html (if you are copying and pasting from a website, I wouldn't be surprised you'd ended up finding some). Just replace the character if you want it gone...

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    is there anyway to do that programmatically? Because I've got 7 sheets ranging from 32 to 107 rows that will be changed by the user and I'd rather make it so the user has to do the least amount of data editing. As is, the user already has to copy from the table, paste into Excel, copy from Excel, paste into notepad, copy from notepad, and paste back into Excel. No idea why I have to go through that process (probably some more html elements), but it is the only way I can make it so the Excel sheet is readable.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Unknown Character

    theString.replace((char)0xA0, ' '); // replace the non-breaking space with regular spaces

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    Quote Originally Posted by helloworld922 View Post
    theString.replace((char)0xA0, ' '); // replace the non-breaking space with regular spaces
    nflTeam.replace((char)0xA0, ' ');
        				nflTeam = nflTeam.trim();
        				if(nflTeam.substring(0,2).equals("GB"))
                            System.out.println(Integer.toHexString(nflTeam.charAt(2)));
        				System.out.println("'"+nflTeam+"'");

    Still returns a0 as the character there, and still appears to be unable to trim or recognize the a0 as a space when "GB " is compared to "GB ". Thoughts?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Unknown Character

    What does the printlln show for the value of nflTeam?
    Can you make a small complete program that compiles and executes to demonstrate what is happening?

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Unknown Character

    Quote Originally Posted by aussiemcgr View Post
    nflTeam.replace((char)0xA0, ' ');
        				nflTeam = nflTeam.trim();
        				if(nflTeam.substring(0,2).equals("GB"))
                            System.out.println(Integer.toHexString(nflTeam.charAt(2)));
        				System.out.println("'"+nflTeam+"'");

    Still returns a0 as the character there, and still appears to be unable to trim or recognize the a0 as a space when "GB " is compared to "GB ". Thoughts?
    Strings are immutable, and the replace function returns the resulting string rather than changing the calling object. So you must reassign the string value
    nflTeam = nflTeam.replace((char)0xA0, ' ');

  13. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    Well, its kind of hard to do that without providing you with the data, and its kind of hard to provide the data because you cant actually see the character. I could provide the Excel Sheet, but unless you can read the sheet in java, its useless.

    I'll attempt to do this as a theoretical thing.
    Lets say the inputs are the following:
    "Hines Ward, Pit*WR"
    "Dwayne Bowe, KC*WR"
    "Donald Driver, GB*WR"
    "Percy Harvin, Min*WR**P"

    The format here is: "FirstName LastName (can be multiple words), Team (2 or 3 letter initial) Position Injury (if applicable)"

    I trimmed as much fat as I could out of the code and explained the best I could.
    //ArrayList that contains all players
    public static ArrayList<Player> players;
    //ArrayList that contains players split up by their positions. This is used when filtering players in the GUI.
    public static ArrayList<ArrayList<Player>> playersBP;
    //ArrayList of all positions that are created by the user
    public static ArrayList<Position> positions;
     
    public static void main(String args[])
    {
    	readPlayerDatabase();
    }
    /*Method to Read in Names.
     *For the given example case, this method will go through each item in the case
     *and organize the players in their correct ArrayLists of Player Objects.
     */
    public static void readPlayerDatabase()
    {
    	//Initialize players list whenever reading from the database
    	players = new ArrayList<Player>();
    	//Initialize playersBP list whenever reading from the database
        	playersBP = new ArrayList<ArrayList<Player>>();
    	//Create an ArrayList for each position and insert into playersBP
        	for(int i=0;i<positions.size();i++)
        	{
        		playersBP.add(new ArrayList<Player>());
        	}
    	//Start IO Stuff for SmartXLS
        	WorkBook workBook = new WorkBook();
        	try{
        		workBook.readXLSX("PlayerDatabase.xlsx");
    		//End IO Stuff for SmartXLS
    		//Go through each sheet in the WorkBook
        		for(int i=0;i<workBook.getNumSheets();i++)
        		{
    			//Set the active Sheet
        			workBook.setSheet(i);
    			//Set the row value
        			int r=1;
    			//Set the column value
        			int c=1;
    			//Read text at cell reference Row:1 Column:1 (Cell B2)
        			String text = workBook.getText(r,c);	
    			//Recurse through rows until empty row
        			while(!text.equals(""))
        			{	
    				//Seperate player's Name from other information
        				String[] sep = text.split(", ");
    				//Player's First Name is up to first space
        				String fName = sep[0].substring(0,sep[0].indexOf(" "));
    				//Player's Last Name is the rest of text (can be multiple words)
        				String lName = sep[0].substring(sep[0].indexOf(" ")+1);
    				//Player's NFL team is the first 3 letters of sep[1]
        				String nflTeam = sep[1].substring(0,3).trim();
     
    				//Attempt to replace html space with regular space for 2 letter abbreviations
        				nflTeam.replace((char)0xA0, ' ');
    				//Attempt to trim extra space for 2 letter abbreviations
        				nflTeam = nflTeam.trim();
    				//Print out to console results
        				if(nflTeam.substring(0,2).equals("GB"))
                            		System.out.println(Integer.toHexString(nflTeam.charAt(2)));
        				System.out.println("'"+nflTeam+"'");
     
    				//Assign Player's Position, which the name of the sheet
        				Position pos = new Position(workBook.getSheetName(i));
    				//Assign Player's Points, which are found in the 13th column (12th column index) of each row
        				int points = (int)workBook.getNumber(r,12);
    				//Create Player Object
        				Player tPlayer = new Player(fName,lName,nflTeam,pos,points);
    				//Call Add Player Method to add to each ArrayList
        				addPlayer(tPlayer);
    				//Increment Row
        				r++;
    				//Read new text value
        				text = workBook.getText(r,c);
        			}
        		}
     
     	//Catch errors that may have occured   		
        	}catch(Exception ex){System.out.println("Error in ReadPlayerDatabase: "+ex);	}
    }
     
    /*Method to add Players to each ArrayList
     *This method will always be implemented after ArrayLists have been initialized
     */
    public void addPlayer(Player play)
    {
    	//Get Player's Position Object 
       	Position pos = play.position;
    	//Set Default Index to -1 to error check
        	int index = -1;
    	//Find index in positions that are assigned the Player's Position
        	for(int i=0;i<positions.size();i++)
        	{
        		if(positions.get(i).Name.equals(pos.Name))
        		{
        			index = i;
        			break;
        		}
        	}
    	//Get the ArrayList that cooresponds to Player's Position
        	ArrayList<Player> tList = playersBP.get(index);
    	//Add Player to their Position ArrayList in order of Points
        	for(int i=0;i<tList.size();i++)
        	{
        		if(tList.get(i).Points<=play.Points || i==tList.size()-1)
        		{
        			tList.add(i,play);
        			break;
        		}
        	}
    	//Add Player to the ArrayList of all Player in order of Points
        	for(int i=0;i<players.size();i++)
        	{
        		if(players.get(i).Points<=play.Points || i==players.size()-1)
        		{
        			players.add(i,play);
        			break;
        		}
        	}
    	//Add Player if Size of ArrayLists are empty
        	if(tList.size()==0)
        		tList.add(play);
        	if(players.size()==0)
        		players.add(play);
    }

  14. #14
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    Well, its kind of hard to do that without providing you with the data, and its kind of hard to provide the data because you cant actually see the character. I could provide the Excel Sheet, but unless you can read the sheet in java, its useless.

    I'll attempt to do this as a theoretical thing.
    Lets say the inputs are the following:
    "Hines Ward, Pit WR"
    "Dwayne Bowe, KC WR"
    "Donald Driver, GB WR"
    "Percy Harvin, Min WR P"

    The format here is: "FirstName LastName (can be multiple words), Team (2 or 3 letter initial) Position Injury (if applicable)"

    I trimmed as much fat as I could out of the code and explained the best I could.
    //ArrayList that contains all players
    public static ArrayList<Player> players;
    //ArrayList that contains players split up by their positions. This is used when filtering players in the GUI.
    public static ArrayList<ArrayList<Player>> playersBP;
    //ArrayList of all positions that are created by the user
    public static ArrayList<Position> positions;
     
    public static void main(String args[])
    {
    	readPlayerDatabase();
    }
    /*Method to Read in Names.
     *For the given example case, this method will go through each item in the case
     *and organize the players in their correct ArrayLists of Player Objects.
     */
    public static void readPlayerDatabase()
    {
    	//Initialize players list whenever reading from the database
    	players = new ArrayList<Player>();
    	//Initialize playersBP list whenever reading from the database
        	playersBP = new ArrayList<ArrayList<Player>>();
    	//Create an ArrayList for each position and insert into playersBP
        	for(int i=0;i<positions.size();i++)
        	{
        		playersBP.add(new ArrayList<Player>());
        	}
    	//Start IO Stuff for SmartXLS
        	WorkBook workBook = new WorkBook();
        	try{
        		workBook.readXLSX("PlayerDatabase.xlsx");
    		//End IO Stuff for SmartXLS
    		//Go through each sheet in the WorkBook
        		for(int i=0;i<workBook.getNumSheets();i++)
        		{
    			//Set the active Sheet
        			workBook.setSheet(i);
    			//Set the row value
        			int r=1;
    			//Set the column value
        			int c=1;
    			//Read text at cell reference Row:1 Column:1 (Cell B2)
        			String text = workBook.getText(r,c);	
    			//Recurse through rows until empty row
        			while(!text.equals(""))
        			{	
    				//Seperate player's Name from other information
        				String[] sep = text.split(", ");
    				//Player's First Name is up to first space
        				String fName = sep[0].substring(0,sep[0].indexOf(" "));
    				//Player's Last Name is the rest of text (can be multiple words)
        				String lName = sep[0].substring(sep[0].indexOf(" ")+1);
    				//Player's NFL team is the first 3 letters of sep[1]
        				String nflTeam = sep[1].substring(0,3).trim();
     
    				//Attempt to replace html space with regular space for 2 letter abbreviations
        				nflTeam.replace((char)0xA0, ' ');
    				//Attempt to trim extra space for 2 letter abbreviations
        				nflTeam = nflTeam.trim();
    				//Print out to console results
        				if(nflTeam.substring(0,2).equals("GB"))
                            		System.out.println(Integer.toHexString(nflTeam.charAt(2)));
        				System.out.println("'"+nflTeam+"'");
     
    				//Assign Player's Position, which the name of the sheet
        				Position pos = new Position(workBook.getSheetName(i));
    				//Assign Player's Points, which are found in the 13th column (12th column index) of each row
        				int points = (int)workBook.getNumber(r,12);
    				//Create Player Object
        				Player tPlayer = new Player(fName,lName,nflTeam,pos,points);
    				//Call Add Player Method to add to each ArrayList
        				addPlayer(tPlayer);
    				//Increment Row
        				r++;
    				//Read new text value
        				text = workBook.getText(r,c);
        			}
        		}
     
     	//Catch errors that may have occured   		
        	}catch(Exception ex){System.out.println("Error in ReadPlayerDatabase: "+ex);	}
    }
     
    /*Method to add Players to each ArrayList
     *This method will always be implemented after ArrayLists have been initialized
     */
    public void addPlayer(Player play)
    {
    	//Get Player's Position Object 
       	Position pos = play.position;
    	//Set Default Index to -1 to error check
        	int index = -1;
    	//Find index in positions that are assigned the Player's Position
        	for(int i=0;i<positions.size();i++)
        	{
        		if(positions.get(i).Name.equals(pos.Name))
        		{
        			index = i;
        			break;
        		}
        	}
    	//Get the ArrayList that cooresponds to Player's Position
        	ArrayList<Player> tList = playersBP.get(index);
    	//Add Player to their Position ArrayList in order of Points
        	for(int i=0;i<tList.size();i++)
        	{
        		if(tList.get(i).Points<=play.Points || i==tList.size()-1)
        		{
        			tList.add(i,play);
        			break;
        		}
        	}
    	//Add Player to the ArrayList of all Player in order of Points
        	for(int i=0;i<players.size();i++)
        	{
        		if(players.get(i).Points<=play.Points || i==players.size()-1)
        		{
        			players.add(i,play);
        			break;
        		}
        	}
    	//Add Player if Size of ArrayLists are empty
        	if(tList.size()==0)
        		tList.add(play);
        	if(players.size()==0)
        		players.add(play);
    }


    This should output:
    'PIT'
    'KC'
    'GB'
    'Min'

    But it outputs:
    'PIT'
    'KC '
    'GB '
    'Min'
    Last edited by aussiemcgr; September 1st, 2010 at 09:51 AM.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Unknown Character

    nflTeam.replace((char)0xA0, ' ');
    You missed copeg's post. The replace method RETURNs a modified String.

  16. The Following User Says Thank You to Norm For This Useful Post:

    aussiemcgr (September 1st, 2010)

  17. #16
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    Quote Originally Posted by Norm View Post
    You missed copeg's post. The replace method RETURNs a modified String.
    Oh, the miracle of subtle changes. Thanks, worked like a charm.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Unknown Character

    Actually it all comes with reading the API doc when a method doesn't do what you expect.

  19. #18
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: Unknown Character

    In my defense, I was at college during the day and the internet is so ungodly slow that loading a page is a 10 minute endeavor.

  20. #19
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Unknown Character

    download the JavaSE Javadoc It lets you easily browse through the Java API documentation without having to wait for slow internet connections.

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Unknown Character

    Also I find that searching the local doc will find better references that doing the search on the doc's web site.

Similar Threads

  1. Replies: 6
    Last Post: May 25th, 2010, 02:15 AM
  2. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM
  3. [SOLVED] last character position
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 05:31 AM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 11:13 AM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM