We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › question about converting data types
Page Index Toggle Pages: 1
question about converting data types (Read 754 times)
question about converting data types
Sep 7th, 2009, 11:14am
 
Hi all,

I'm trying to use the twitter API as part of an interactive installation and I'm having a hard time converting data types for the visualization. I would like people to be able to tweet hexidecimal color codes to a given hash tag and then use the colors to update the visualization. But when I get the hex code back from the API and turn it into an int it isn't formatted correctly.

Here is the code I'm using:

String input = titles[i];
   String regex = "#twitstallation #";  //set the regex to the hashtag
   String output = input.replaceAll(regex,""); // pull off the hashtag
   
   textColor = int(output); //set the text color
   println("THIS IS THE hashtag HERE: " + textColor);

the println indicates that text color is 0 instead of the tweeted value (#990000). Does anyone know how I can get the value it is formatted correctly?  

Thanks


Re: question about converting data types
Reply #1 - Sep 7th, 2009, 11:48am
 
I think you should use the unhex() method instead of int().
Re: question about converting data types
Reply #2 - Sep 7th, 2009, 2:07pm
 
Thanks for the reply antiplastik, Now it's throwing some cryptic java errors though:

Oopsies.
java.lang.reflect.InvocationTargetException
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
     at java.lang.reflect.Method.invoke(Method.java:585)
     at simpleML.XMLRequest.run(XMLRequest.java:89)
     at java.lang.Thread.run(Thread.java:613)

any other suggestions?
Re: question about converting data types
Reply #3 - Sep 8th, 2009, 12:22pm
 
Please post the full code, or try to debug your app:

Code:
String input = titles[i];
String regex = "#twitstallation #";
String output = input.replaceAll(regex,"");
println(output); // <- what does it print?
textColor = unhex(output);
println(textColor); // <- what does it print?


Which line is throwing the exception?
Re: question about converting data types
Reply #4 - Sep 8th, 2009, 11:42pm
 
I set up some println statements to see what values are being returned and it looks like changing int to unhex is formatting the color correctly, but for some reason it is crashing anyway.

The full code is below, note that I had to change the url for the query because, as a new member, I can't post urls yet.

import simpleML.*;
import java.util.regex.*;

XMLRequest xmlRequest;

int textColor = #009900;

void setup() {
 size(600,600);
 background(0);
  PFont fontA = loadFont("CourierNew36.vlw");
 textFont(fontA, 12);
 
 // Creating and starting the request
 // An array of XML elements can be retrieved using getElementArray.
 // This only works for elements with the same name that appear multiple times in the XML document.
 xmlRequest =  new XMLRequest(this, "http : / / search . twitter . com / search.atom?q=%23twitstallation" );
}

void draw() {

delay(1000);
  xmlRequest.makeRequest();
}

// When the request is complete
void netEvent(XMLRequest ml) {
   int yline = 10;
 // Retrieving an array of all XML elements inside"
title*
"tags
 String[] titles = ml.getElementArray( "title" );
 String[] name = ml.getElementArray( "name" );
 
 for (int i = 0; i < titles.length; i++ ) {
   //int wordLength = 10 + titles.length;
 
   print("titles at i: " + i + " " + titles[i]);
   println( " " +  name[i]);
   fill(textColor);
   text("Working" + " " + titles[i]+ " ", 20, yline );
   yline= yline + 12;
   println();
   println("yline " + titles[0]);
 
   String input = titles[i];
   String regex = "#twitstallation ";  //set the regex to the hashtag
   String output = input.replaceAll(regex,""); // pull off the hashtag
   
   String newTextColor = output;
   println("THIS IS THE newTextColor: " + newTextColor);
   
   textColor = int(newTextColor);
   text("text should be different" + " " + titles[i]+ " ", 20, yline );
 }
 
}

Thanks for your help!

-Jared
Re: question about converting data types
Reply #5 - Sep 9th, 2009, 4:00am
 
Looks like the exception has to deal with SimpleML library, not the conversion function. Maybe a compatibility problem.

I don't know which version of Processing you are using, but starting from release 149 there's a library included which purpose is to handle XML data. I think you should better use theses built-in methods instead of SimpleML library.

http://processing.org/reference/XMLElement.html

That should solve your problem.
Page Index Toggle Pages: 1