getting data from WML page using J2me - string

I have a program in j2me that get strings and data from an wml/asp page.
Using this code:
HttpConnection con = (HttpConnection) Connector.open(
"http://localhost:"+port+"/MobileWebWIthConnection/ShowCourseinsemester.aspx?StudentId="+ID+"&Year="+Year+"&Semester="+Semester);
DataInputStream in = new DataInputStrea(con.openInputStream());
int len = (int) con.getLength();
byte[] info = new byte[len];
in.readFully(info);
result = new String(info);
switchDisplayable(null, getStudentCourses());
stringItem2.setText(result);
When my j2me application try to read and store the data from this page:
"http://localhost:"+port+"/MobileWebWIthConnection/ShowCourseinsemester.aspx?StudentId="+ID+"&Year="+Year+"&Semester="+Semester
the text which is placed in the string called (result) is nothing similar to expected figure below:
It's taking the content without formatting as below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<wml>
<card>
<p><b>Student Name :</b> Arin Rizk </p>
<p><b>Student ID</b> : 20111</p>
<p>first Semester ,2011</p>
1 - Course Name : DDD | Credits Number : 3 | Mark : 70 </br> 2 - Course Name : EEE | Credits Number : 3 | Mark : 65 </br> 3 - Course Name : EEE | Credits Number : 3 | Mark : 65 </br> 4 - Course Name : EEE | Credits Number : 3 | Mark : 90 </br>
</card>
</wml>
so when I assigned this text to the StringItem it's showing as below in the fig.
stringItem2.setText(result);
How can get my j2me to view the strings as the original formatted page?

I solved it , it was a little bit tricky specially that in j2me there is no (split method) .
so simply I created one.
I decleared it
String[] split (String x){
int num=0;
for(int i=0; i<x.length(); i++) // count the number of ','
if(x.charAt(i)==',')
num++;
String[] r=new String[num];
for(int i=0; i<num; i++)
{
int loc=x.indexOf(","); //loc is the location of each ','
r[i]=x.substring(0,loc);
x=x.substring(loc+1);
}
return r;
}
and then I applied it and display the results in a list
HttpConnection con = (HttpConnection) Connector.open("http://localhost:"+port+"/MobileWebWIthConnection/ShowCourseinsemester.aspx?StudentId="+ID+"&Year="+Year+"&Semester="+Semester);
DataInputStream in = new DataInputStream(con.openInputStream());
int len = (int) con.getLength();
byte[] info = new byte[len];
in.read(info);
result = new String(info);
String[] a=split(result);
getList().deleteAll();
for(int i=1; i<a.length; i++)
getList().append(a[i], null);
switchDisplayable(null,getList());
and the results were as wanted ( in rows ) without the full source code from the wml page.

Related

Count of range of columns with null rows in excel

I have run into a weird problem. It may be simple one, but I am unable to trace out a solution for the same.
I have 10 columns in excel where 8 columns has data and 2 columns doesn't have the data, Now want to get the count of the null columns.
a b c d e(null count)
10 10 2
5 3
4
I tried using ISBlank, CountBlank but nothing is woking for me.
Edit
When I use count bank I am getting some weird symbol equal to zero (sorry, I am unable to attach screen shot).
My code is
COUNTBLANK(D9:I9)=0
Edit
This is my code:
=IF(AND(COUNTBLANK(D9:I9)=0,COUNTBLANK(K9:P9)=0,Q9="",R9="",T9="",COUNTBLANK(V9:AF9)=0,AH9="",AK9="",AM9="",AO9=""),"",SUM(D9:I9,K9:P9,Q9,R9,T9,V9:AF9,AH9,AK9,AM9,AO9))
personal data:
<header id="head">
<!-- <img src='Images/12.jpg' width=screen.width > -->
this is god
</header>
<script>
/* <!--function slide(){ */
var ima = ['12.jpg','Pooja Vidhanam Pic.jpg' ];
/* document.getElementById("head").innerHTML ="'"+"Images/"+ima[1]+"'";
var txt="'"+"Images/"+ima[0]+"'"; */
for (i = 0; i < ima.length; i++) {
var x = document.createElement("IMG");
x.setAttribute("src", "Images/"+ima[i]);
x.setAttribute("width", "304");
x.setAttribute("height", "228");
x.setAttribute("alt", "The Pulpit Rock");
//x.setAttribute(name, value)
//document.getElementById("head").appendChild(document.createElement("<img src= txt width=screen.width >"));
document.getElementById("head").appendChild(x);
//sleep(1000);
}
/* for (i=0;i<ima.length;i++){
<img src="Images/"+images[i]+" width=screen.width" >
document.getElementById("head").innerHTML=ima[i];
} */
</script>
<!-- <script>
var cars = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
document.getElementById("head").innerHTML = cars[0];
var text = "";
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
document.getElementById("head").innerHTML = text;
}
</script> -->

how to find decode way to decode a USSD Command's result in c#?

I'm working on my GSM modem (Huawei E171) to send USSD commands.
to do this i use this commands at the first:
AT+CMGF=1
AT+CSCS=? ----> result is "IRA" this is my modem default
after that i sent these commands and i have got these results and everything works fine.
//*141*1# ----->to check my balance
+CUSD:
0,"457A591C96EB40B41A8D0692A6C36C17688A2E9FCB667AD87D4EEB4130103D
0C8281E4753D0B1926E7CB2018881E06C140F2BADE5583819A4250D24D2FC
BDD653A485AD787DD65504C068381A8EF76D80D2287E53A55AD5653D554
31956D04",15
//*100# ----> this command give me some options to charge my mobile
+CUSD:
1,"06280627062C06470020062706CC06310627064606330644000A0030002E062E0
63106CC062F00200634062706310698000A0031002E067E062706330627063106A
F0627062F000A0032002E0622067E000A0033002E06450644062A000A003
4002E06330627064506270646000A0035002E067E0627063106330
6CC06270646000A002300200028006E0065007800740029000A",72
i found some codes to decode these result:
to decode checking balance result i used:
string result141="457A591C96EB40B41A8D0692A6C36C17688A......."
byte[] packedBytes = ConvertHexToBytes(result141);
byte[] unpackedBytes = UnpackBytes(packedBytes);
//gahi in kar mikone gahi balkaee nafahmidam chera
string o = Encoding.Default.GetString(unpackedBytes);
my function's codes are:
public static byte[] ConvertHexToBytes(string hexString)
{
if (hexString.Length % 2 != 0)
return null;
int len = hexString.Length / 2;
byte[] array = new byte[len];
for (int i = 0; i < array.Length; i++)
{
string tmp = hexString.Substring(i * 2, 2);
array[i] =
byte.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
}
return array;
}
public static byte[] UnpackBytes(byte[] packedBytes)
{
byte[] shiftedBytes = new byte[(packedBytes.Length * 8) / 7];
int shiftOffset = 0;
int shiftIndex = 0;
// Shift the packed bytes to the left according
//to the offset (position of the byte)
foreach (byte b in packedBytes)
{
if (shiftOffset == 7)
{
shiftedBytes[shiftIndex] = 0;
shiftOffset = 0;
shiftIndex++;
}
shiftedBytes[shiftIndex] = (byte)((b << shiftOffset) & 127);
shiftOffset++;
shiftIndex++;
}
int moveOffset = 0;
int moveIndex = 0;
int unpackIndex = 1;
byte[] unpackedBytes = new byte[shiftedBytes.Length];
//
if (shiftedBytes.Length > 0)
{
unpackedBytes[unpackIndex - 1] =
shiftedBytes[unpackIndex - 1];
}
// Move the bits to the appropriate byte (unpack the bits)
foreach (byte b in packedBytes)
{
if (unpackIndex != shiftedBytes.Length)
{
if (moveOffset == 7)
{
moveOffset = 0;
unpackIndex++;
unpackedBytes[unpackIndex - 1] =
shiftedBytes[unpackIndex - 1];
}
if (unpackIndex != shiftedBytes.Length)
{
// Extract the bits to be moved
int extractedBitsByte = (packedBytes[moveIndex] &
_decodeMask[moveOffset]);
// Shift the extracted bits to the proper offset
extractedBitsByte =
(extractedBitsByte >> (7 - moveOffset));
// Move the bits to the appropriate byte
//(unpack the bits)
int movedBitsByte =
(extractedBitsByte | shiftedBytes[unpackIndex]);
unpackedBytes[unpackIndex] = (byte)movedBitsByte;
moveOffset++;
unpackIndex++;
moveIndex++;
}
}
}
// Remove the padding if exists
if (unpackedBytes[unpackedBytes.Length - 1] == 0)
{
byte[] finalResultBytes = new byte[unpackedBytes.Length - 1];
Array.Copy(unpackedBytes, 0,
finalResultBytes, 0, finalResultBytes.Length);
return finalResultBytes;
}
return unpackedBytes;
}
but to decode second result i used:
string strHex= "06280627062C06470020062706CC06310......";
strHex = strHex.Replace(" ", "");
int nNumberChars = strHex.Length / 2;
byte[] aBytes = new byte[nNumberChars];
using (var sr = new StringReader(strHex))
{
for (int i = 0; i < nNumberChars; i++)
aBytes[i] = Convert.ToByte(
new String(new char[2] {
(char)sr.Read(), (char)sr.Read() }), 16);
}
string decodedmessage= Encoding.BigEndianUnicode.
GetString(aBytes, 0, aBytes.Length);
both of theme works current but why i should different decoding way to decode these results?
from where i can find, i should use which one of these two types of decoding?
USSD command responses +CUSD unsolicited responses are formatted as follows:
+CUSD: <m>[<str_urc>[<dcs>]]
Where "m" is the type of action required, "str_urc" is the response string, and "dcs" is the response string encoding.
This quote is from a Siemens Cinterion MC55i manual but applies generally to other modem manufacturers:
If dcs indicates that GSM 03.38 default alphabet is used TA converts GSM alphabet into current TE character
set according to rules of GSM 07.05 Annex A. Otherwise in case of invalid or omitted dcs conversion of
str_urc is not possible.
USSD's can be sent in 7-Bit encoded format or UC2 hence when looking at your two example responses you can see either a DCS of 15 or 72.
GSM 03.38 Cell Broadcast Data Coding Scheme in integer format (default 15). In case of an invalid or omitted
dcs from the network side (MT) will not be given out.
So if you get a DCS of 15 then it is 7-Bit encoded. And if it's 72 then it will be UC2. So from this you can easily select either your first decoding routine or second.

How to parse this information from a string?

country residents area capital
Andorra 71201 468 Andorra la Vella
Italien 58133509 301230 Rom
San Marino 29251 61 San Marino
I need to store the information (capital, residents, area, capital) in different variables. How would I go about parsing this? Notice that sometimes there are spaces in the names.
I have tried reading each token ( scanner.next() ) this fails when there are spaces in the capital or country name.
I have tried reading each line and then parsing it but I can't figure out a way to parse everything correctly since there are sometime spaces in the names. (I used indexOf() and substring() )
This is part of a bigger file but there are no spaces in the residents or area field in the entire field.
My try:
while(scanner.hasNext()){
String info = scanner.nextLine();
//parse string
int nameindex = info.indexOf(" ");
System.out.println(info.substring(0,nameindex));
int resindex = info.indexOf(" ", nameindex);
}
I hope you have a multiline string as per your question title. So why don't you simply use a regex for the whole content. Given the string is stored in the variable data
data.split("[ ]{2,}")
This would give the array of data as a whole. So when you have to parse it you can simply do a loop 4 elements at a time
(edit)
or else you can simply use this function... hope this will be easier for you.
List<Map<String, String>> parse(String data){
List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
String[] lines = data.split("\n");
String[] keys = lines[0].split("[ ]{2,}");
for (int i = 1; i < lines.length; i++) {
String row[] = lines[i].split("[ ]{2,}");
Map<String, String> rowMap = new HashMap<String, String>();
for (int j = 0; j < row.length; j++) {
rowMap.put(keys[j], row[j]);
}
dataList.add(rowMap);
}
return dataList;
}

How to perform reverse of string in jsp?

This is my demo.jsp code ,here i'm writing code as follow,result showing as No line found exception where as i executed the code in java class working fine.How can i execute the revers e of string in jsp page?
Scanner sc = new Scanner(System.in);
System.out.println("Enter String :");
String n = sc.nextLine();
String rev = "";
int len = n.length();
for (int i = len - 1; i >= 0; i--) {
rev = rev + n.charAt(i);
}
out.println("Reverse of Given String is :");
out.println("" + rev);
Your current code works without errors . i guess the problem is you are passing the empty string as the input to nextLine method,
Try passing the values like this ,
Scanner sc = new Scanner(System.in);
System.out.println("Enter String :");
String n = sc.nextLine();
String rev = "value";
int len = n.length();
for (int i = len - 1; i >= 0; i--) {
rev = rev + n.charAt(i);
}
out.println("Reverse of Given String is :");
out.println("" + rev)
Hope this helps !!
The above code is pure Java code, not a valid JSP code. If we split what the above code does, it
Prompt for user input.
Perform the reverse on user entered string.
Print the result string.
System.in and Scanner cannot be used to read user input from a JSP page. Fundamentally the user interaction results in communication between the client (browser) and the server (like Tomcat).
In simple the below code is what makes your code a JSP.
Note : The jsp file name and the value of form action must be same.
<body>
<form action="rev.jsp">
<!-- 1. Prompt for user input -->
Enter Text : <input type="text" name="myText"/>
<input type="submit" value="Submit Text"/>
<br/>
<%
// 2. Perform the reverse on user entered string.
String rev = "";
String n = request.getParameter("myText"); // read user entered value in text box.
if(n != null) { // if myText is entered
int len = n.length();
for (int i = len - 1; i >= 0; i--) {
rev = rev + n.charAt(i);
}
// 3. Print the result string.
out.println("Reverse of Given String is :");
out.println("" + rev);
}
%>
</form>
</body>
If you want to open already existing function, You can use following,
String reverse = new StringBuffer(words).reverse().toString();
Refer THIS

Convert Table to Text with Spaces

I've come across this several times in a couple years of programming so I decided to do some research to see if it was possible. Often I create data structures in code that are initialized in a table like manner, with rows and columns, and I would have liked to have this table-to-text feature for code readability. How can you create a table in word, or excel, or some other program, and output the cells of the table to text, with spaces (not tabs)? Word can do it with tabs, and excel can do it with misaligned spaces. Is there any program out there that automates this?
Have you tried using a monospace font, such as courier, when you export from excel? Most fonts will adjust spacing based on the specific width, height and kerning of each character but a monospace font will allow you to use spaces for alignment.
As for converting tabs to spaces automagically, there must be 100s if not 1000s of methods, apps, commands available out there.
I spent an hour or 2 researching this. I experimented with excel and word and they both came so close to exact solution that it made me crazy. I tried other programs online but with no luck. Here's my solution, Microsoft's Word's Table-To-Text feature and custom C# program that converts the Word-tabified text to column aligned text with spaces and not tabs.
1) Put your columns and rows in an MS Word Table
2) Convert table to text with tabs (look up how to do this)
3) Save the converted table to a plain text file
4) Use my program to open and convert the file
5) Copy the text in the output file to your code
Below is the C# Windows Form Application I wrote. I apologize for lack of optimization. I was at work and wanted it done as quickly as possible:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenFileDialog of = new OpenFileDialog();
of.Title = "Select Tabbed Text File To Convert";
if (of.ShowDialog() != DialogResult.OK)
return;
StreamReader s = new StreamReader(of.OpenFile());
List<string> lines = new List<string>();
string line;
// Get each line into an array of lines.
while ((line = s .ReadLine()) != null)
lines.Add(line);
int numTabs = 0;
// count the number of tabs in each line, assume good input, i.e.
// all lines have equal number of tabs.
foreach (char c in lines[0])
if (c == '\t')
numTabs++;
for (int i = 0; i < numTabs; i++)
{
int tabIndex = 0;
// Loop through each line and find the "deepest" location of
// the first tab.
foreach (string l in lines)
{
int index = 0;
foreach (char c in l)
{
if (c == '\t')
{
if (index > tabIndex)
tabIndex = index;
break;
}
index++;
}
}
// We know where the deepest tab is, now we go through and
// add enough spaces to take the first tab of each line out
// to the deepest.
//foreach (string l in lines)
for (int l = 0; l < lines.Count; l++)
{
int index = 0;
foreach (char c in lines[l])
{
if (c == '\t')
{
int numSpaces = (tabIndex - index) + 1;
string spaces = "";
for (int j = 0; j < numSpaces; j++)
spaces = spaces + " ";
lines[l] = lines[l].Remove(index, 1);
lines[l] = lines[l].Insert(index, spaces);
break;
}
index++;
}
}
}
FileInfo f = new FileInfo(of.FileName);
string outputFile = f.FullName.Insert(f.FullName.IndexOf(f.Extension), " (Aligned)");
StreamWriter w = new StreamWriter(outputFile);
foreach (string l in lines)
w.Write(l + "\r\n");
w.Close();
s.Close();
MessageBox.Show("Created the file: " + outputFile);
}
}
}

Resources