我已将实时JavaScript数据从公共网站提取到处理中,每4秒更新一次。
需要将数字更改为整数,以便将它们映射到椭圆的实时更新半径。
以下链接最接近解决方案,但对我没有用处:
Processing: How to convert a char datatype into its utf-8 int representation? Convert int to char in java Java: parse int value from a char
我的代码:
String url = "http://avail.mdx.ac.uk:8090/avail.js";
String[] site = loadStrings(url);
int[] list = int(split(site[0], ';'));
println(list[0]);
println(list[1]);
println(list[2]);
Prints:
0
0
0
我试图从以前使用
时打印的字符中获取整数String[] list = split(site[0], ';');
替换int [] list = int(split(site [0],';'));
作为测试,你可以在上面交换它以便更好地理解。所有代码都进入
void draw(){
}
我认为这是一个变数问题,' int'似乎并不知道数据的来源,尽管我试图这样做:
String[] list = split(site[0], ';');
在同一个文件中,虽然错误代码:'重复的局部变量出现'。
将字符串中的数字字符变成整数的任何解决方案都有最佳的映射到椭圆的方法吗?
答案 0 :(得分:0)
我不知道这是否能解决你的问题,但也许会指出你正确的方向。关于java编码的技巧是将字符串转换为字节数组和从字节数组转换。
byte[] dataFromJavaScript = {49,50,51,52};
String value = new String(dataFromJavaScript, Charset.forName("UTF-8"));
System.out.println(value);
答案 1 :(得分:0)
据我所知,从链接来看,它本质上就是你试图重现的页面:
据我了解,您尝试提取的数字是计算机的总数和使用数,这些数字似乎是js文件中 avail 变量中的尾随数字。你把它拆分到有“;”的地方。如果你在那里分裂,你会得到像“HESLGFOA01:84:32”这样的字符串,它们基本上似乎归结为“ID:Total:Used”。因此,如果您再次获取该字符串并将其拆分为其元素,只要您看到“:”就会得到三个元素的数组(下面代码中的榆树),ID:“HESLGFOA01”,总计:“84”,使用:“32 ”。之后,您只需将元素1和[2]转换为整数即可获得数字!
作为旁注: availgroup 变量包含有关引用上述相同ID的每个房间的信息,因此当我们找到包含该变量的字符串时,我们会停止循环!
String url = "http://avail.mdx.ac.uk:8090/avail.js?";
// don't remove the questionmark otherwise the page won't update every time you run it
String [] site = loadStrings(url);
String [] list = split(site[0], ';');
for (int i = 0; i < list.length; i++) {
if (list[i].contains("availgroup")) break; //stops the loop
String [] elm = list[i].split(":");
String ID = elm[0];
int total = int(elm[1]);
int used = int(elm[2]);
println("Total: " + total + " Used:" + used + " Free: " + (total - used));
}
以下是代码的简化版本,其中包含更多注释以解释每个步骤。为简单起见,这个只占第一个元素(第一个计算机房)
void setup() {
// do this every 4 seconds as it is refreshed. We don't want to spam
// the website... This method changes how many times should draw() be
// executed per second. The default value is 24 frames per second. Here
// we set it to .25 which essentially means 1 draw() every 4 seconds...
frameRate(0.25);
}
void draw() {
// set the background to a boring gray colour...
background(204);
// set the website url. Don't forget the "http://"
String url = "http://avail.mdx.ac.uk:8090/avail.js?";
// get an array of big strings from the website, one element per line
// as it is in the page there is actually only one line...
String [] site = loadStrings(url);
// break the first line (site[0]) into an array of strings where
// there is a ";" and store the array into the 'list' variable
// Each element looks like this: "HESLGFOA01:84:32"
String [] list = split(site[0], ';');
// break the first element of list (list[0]) where there are ":" and
// store the result into the 'elm' variable
String [] elm = list[0].split(":");
// ID if the first element in the elm array (elm[0]): HESLGFOA01
String ID = elm[0];
// total is the second element in the elm array (elm[1]): 84
int total = int(elm[1]);
// used is the third element in the elm array (elm[2]) 32
int used = int(elm[2]);
// calculate the free desks by subtracting the used from the total
int free = total - used;
// print stuff / draw ellipses / do fun stuff
println("Total: " + total + " Used:" + used + " Free: " + free);
float x1 = map(mouseX, 0, width, 50, 150);
ellipse(x1, 75, 50, 50);
float x2 = map(free, 0, width, 0, 200);
ellipse(x2, 125, 50, 50);
}
为了把事情放在一起,这是现在第一块avail.js:
var avail =“HESLBAOA01:61:20; HESLGFOA01:84:31; HESLGFLSS12A:16:6; HESLGFLSS12B:16:8; HESLGFLSS13A:16:0; HESLGFLSS13B:16:0; HESL01OA01L:52:22; HESL01LSS106 :30:4; HESL01LSS111:10:3; HESL01LSS112:33:0; HESL01LSS118:59:26; HESL02OA01L:11:4; HESL02LSS206:11:4; HESL02LSS216:14:4; HESL02LSS217:35:5; HESL02LSS219:6 :3; HESL03LSS304:17:1; HESL03LSS305:33:5; HEWIGFOA01:36:25; CAL:9:0" ;