How to Write multiline function in pug? - arrays

I want use the following function in pug, I tried so many thing but it won't run.
var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
function inWords (num) {
if ((num = num.toString()).length > 9) return 'overflow';
n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!n) return; var str = '';
str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
return str;
}

Related

How to add data in a test class Apex

I already created a test class, simple test class, but I cant give data to it, Somebody can explain me a little please here is my code, when i Run the test the cover is 14 % and I dont know how to give data, for exceptions and made it at leas the 75 %
public void cargaPacientes () {
lMSD = new List<Enrollee__c>();
bLista = true;
String strCredencial = '';
String strPoliza = '';
String strCertificado = '';
String strRfc = '';
String fNacimiento = '';
String nombres = '';
String Aseguradora = '';
String Contratante = '';
String query = '';
Boolean correcto = true;
integer cuentacriterios = 0;
string producto = '';
if (proveedor.Tipo_de_Proveedor__c == 'Dentista' || proveedor.Tipo_de_Proveedor__c == 'Clínica' || proveedor.Tipo_de_Proveedor__c == 'Hospital'){
producto = 'Dental';
}else if (proveedor.Tipo_de_Proveedor__c == 'Óptica'){
producto = 'Visión';
}
if((msd.DENTEGRA_ID_Number__c != null) && (msd.DENTEGRA_ID_Number__c != '')) {
strCredencial = ' and DENTEGRA_ID_Number__c like \'' + msd.DENTEGRA_ID_Number__c + '\'';
System.debug(':::: strCredencial = ' + strCredencial);
cuentacriterios = cuentacriterios + 1;
}
if((msd.Family_Number__c != null) && (msd.Family_Number__c != '')){
strCertificado = ' and Family_Number__c like \'' + msd.Family_Number__c + '\'';
System.debug(':::: strCertificado = ' + strCertificado);
cuentacriterios = cuentacriterios + 1;
}
if((msd.RFC__c != null) && (msd.RFC__c != '')){
strRfc = ' and RFC__c like \'%' + msd.RFC__c + '%\'';
System.debug(':::: strRfc = ' + strRfc);
cuentacriterios = cuentacriterios + 1;
}
if((msd.Policy_Number__c != null) && (msd.Policy_Number__c != '')){
strPoliza = ' and Policy_Number__c like \'%' + msd.Policy_Number__c + '%\' ';
System.debug(':::: strPoliza = ' + strPoliza);
cuentacriterios = cuentacriterios + 1;
}
if(msd.Birth_Date__c != null) {
fNacimiento = ' and Birth_Date__c = ' + String.valueOf(msd.Birth_Date__c);
System.debug(':::: fNacimiento = ' + fNacimiento);
cuentacriterios = cuentacriterios + 1;
}
if(msd.Name != null) {
nombres = ' and Name like \'%' + msd.Name + '%\' ';
System.debug(':::: nombres = ' + nombres);
cuentacriterios = cuentacriterios + 1;
}
if(msd.Contratante__c != null) {
Contratante = ' and Contratante__c like \'%' + msd.Contratante__c + '%\' ';
System.debug(':::: Contratante = ' + Contratante);
cuentacriterios = cuentacriterios + 1;
}
if(msd.Aseguradora__c != null) {
Aseguradora = ' and Aseguradora__c like \'%' + msd.Aseguradora__c + '%\' ';
System.debug(':::: Aseguradora = ' + Aseguradora);
cuentacriterios = cuentacriterios + 1;
}
//if(strCredencial != '' || strCertificado != '' || strRfc != '' || strPoliza != '' || fNacimiento != '' || nombres != '') {
if(cuentacriterios>1){
//query = 'Select e.Contact__c, e.Id, e.Name, e.Policy_Number__c, e.Family_Number__c, e.RFC__c, e.Birth_Date__c from Enrollee__c e where Cve_tipo_registro__c = \'Elegibilidad\' and OwnerId = \'' + userID + '\'' + strCredencial + strCertificado + strRfc + strPoliza + fNacimiento;
query = 'Select e.Contact__c, e.Id, e.Name, e.Policy_Number__c, e.Family_Number__c, e.RFC__c, e.Birth_Date__c, e.Plan__c,Product__c from Enrollee__c e where Cve_tipo_registro__c = \'Elegibilidad\' and Enrollee_Status__c = \'Elegible\' and Product__c = \'' + producto + '\'' + strCredencial + strCertificado + strRfc + strPoliza + fNacimiento + nombres + Contratante + Aseguradora;
System.debug(':::: query concatenado = ' + query);
lMSD = Database.query(query);
cuentacriterios = 0;
} else {
System.debug(':::: Sin criterios');
bLista = false;
correcto = false;
cuentacriterios = 0;
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Debe filtrar al menos por dos criterios.'));
}
if(lMSD.size() == 0 && correcto == true) {
bLista = false;
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'No se obtuvieron resultados basado en sus criterios de búsqueda. Utilice un criterio diferente o comuníquese al Centro de Contacto para confirmar elegibilidad.'));
msd = new Enrollee__c();
}
if(lMSD.size() > 0) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Se encontraron ' + lMSD.size() + ' registros. Favor de seleccionar el registro que corresponda con el nombre de su paciente.'));
msd = new Enrollee__c();
}
here is My test Class
private static void cargaPacientesTesting(){
Test.startTest();
PortalProveedoresController obj = new PortalProveedoresController();
obj.cargaPacientes();
Test.stopTest();
}
Thank you Im stuck with this, thanks

C - Recursion instead of loops

I made a program that evaluates expressions, for example
Enter text: 97+74/51-98-11+68-34-2-22+73/40+81/15+100
output :
105.36
If any wrong data is given a user gets a response Incorrect input
I am wondering if I could replace all my for loops to recursion, but I don't know how could I achieve that. Could anybody help ?
Any assistance would be much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
int my_atoi(const char* tab, int from, int to)
{
int score = 0;
int minus = 0;
for (int i = from; i <= to; i++)
{
if (*(tab + i) == 10)
break;
if (*(tab + i) == 32)
break;
if (*(tab + i) == '-' && from == i)
{
minus = 1;
continue;
}
if (*(tab + i) == '+' && i == from)
{
continue;
}
if (*(tab + i) > '9' || *(tab + i) < '0')
break;
else if (*(tab + i) >= '0' && *(tab + i) <= '9')
{
score *= 10;
score += (*(tab + i) - '0');
}
}
if (minus == 1)
score *= -1;
return score;
}
int validate_expression(const char* expr)
{
if (!expr)return -1;
int lng = strlen(expr) - 1;
if (*(expr) > '9' || *(expr) < '0' || * (expr + lng) > '9' || *(expr + lng) < '0')return 0;
int flaga = 0; // flag
for (int i = 1; i < lng; i++) {
if (*(expr + i) <= '9' && *(expr + i) >= '0')
{
flaga = 0;
}
else if (*(expr + i) == '/' || *(expr + i) == '*' || *(expr + i) == '-' || *(expr + i) == '+')
{
if (flaga == 1)
return 0;
flaga = 1;
}
else
return 0;
}
return 1;
}
int calculate(const char* expr, float* result)
{
if (!expr || !result || !validate_expression(expr)) return 0;
if (strpbrk(expr, "i") != NULL) return 0;
int index = 0;
float score = 0;
char sign_memory = 'A';
int flaga = 0;
unsigned int i = 1;
for (; i <= strlen(expr); i++) {
if (*(expr + i) == '-' || *(expr + i) == '+' || *(expr + i) == '/' || *(expr + i) == '*' || *(expr + i) == '\0') {
score += (int)my_atoi(expr, index, i - 1);
index = i;
break;
}
}
for (; i <= strlen(expr); i++) {
if (*(expr + i) == '-' || *(expr + i) == '+' || *(expr + i) == '/' || *(expr + i) == '*' || i == strlen(expr)) {
flaga = 1;
if (sign_memory == '+')
score += (int)my_atoi(expr, index, i - 1);
else if (sign_memory == '-')
score -= (int)my_atoi(expr, index, i - 1);
else if (sign_memory == '/')
{
if (my_atoi(expr, index, i - 1) == 0)return 0;
score /= (int)my_atoi(expr, index, i - 1);
}
else if (sign_memory == '*')
score *= (int)my_atoi(expr, index, i - 1);
sign_memory = *(expr + i);
}
else if (*(expr + i) <= '9' && *(expr + i) >= '0' && flaga == 1) {
flaga = 0;
index = i;
}
}
*result = score;
return 1;
}
int main()
{
char tab[201];
float result, * pointer = &result;
printf("Enter text: ");
fgets(tab, sizeof(tab), stdin);
*(tab + strlen(tab) - 1) = '\0';
if (!validate_expression(tab))
{
printf("Incorrect input");
return 1;
}
else
{
if (calculate(tab, pointer))
printf("%.2f", *pointer);
else {
printf("Incorrect input");
return 1;
}
}
return 0;
}
I would make a function, which does the following:
Check if there is just one or more operators ("+", "-", "*", "/")
In case there is only one, perform the calculation.
In case there are two, perform a tail recursion, as proposed by Jonathan, something like:
calculate("97+74/51-98-11+68-34-2-22+73/40+81/15+100")
= calculate("97+74/51-98-11+68-34-2-22+73/40+81/15") + 100
= (calculate("97+74/51-98-11+68-34-2-22+73/40+81") / 15) + 100
= ((calculate("97+74/51-98-11+68-34-2-22+73/40") + 81) / 15) + 100
= (((calculate("97+74/51-98-11+68-34-2-22+73") / 40) + 81) / 15) + 100
= ((((calculate("97+74/51-98-11+68-34-2-22") + 73) / 40) + 81) / 15) + 100
= (((((calculate("97+74/51-98-11+68-34-2") - 22) + 73) / 40) + 81) / 15) + 100
= ((((((calculate("97+74/51-98-11+68-34") - 2) - 22) + 73) / 40) + 81) / 15) + 100
= (((((((calculate("97+74/51-98-11+68") - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= ((((((((calculate("97+74/51-98-11") + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= (((((((((calculate("97+74/51-98") - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= ((((((((((calculate("97+74/51") - 98) - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= (((((((((((calculate("97+74") / 51) - 98) - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= ((((((((((((97 + 74) / 51) - 98) - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100

convert amount in words in angularjs

I need to convert amount in words. For example, the amount I will get it from service is 9876, I need to display in a table "Nine Thousand Eight Hundred and Seventy Six" in a table.
I need to do this using angularjs. Please help me how can I do this.
JSFIDDLE
function convertNumberToWords(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
}
<input type="text" name="number" placeholder="Number OR Amount" onkeyup="word.innerHTML=convertNumberToWords(this.value)" />
<div id="word"></div>
I refered this javascript fiddle. But I want to it in a angularjs.
I used this for Angular 8.
Input : 123456789.09 Output : twelve crore thirty four lakh fifty six thousand seven eighty nine point zero nine
n: string;
a = ['zero ', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
ngOnInit(): void {
console.log(this.inWords(123456789.09));
}
inWords (num): string {
num = Math.floor(num * 100);
if ((num = num.toString()).length > 11) { return 'overflow'; }
let n;
n = ('00000000' + num).substr(-11).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})(\d{1})(\d{1})$/);
if (!n) { return; } let str = '';
// tslint:disable-next-line:triple-equals
str += (n[1] != 0) ? (this.a[Number(n[1])] || this.b[n[1][0]] + ' ' + this.a[n[1][1]]) + 'crore ' : '';
// tslint:disable-next-line:triple-equals
str += (n[2] != 0) ? (this.a[Number(n[2])] || this.b[n[2][0]] + ' ' + this.a[n[2][1]]) + 'lakh ' : '';
// tslint:disable-next-line:triple-equals
str += (n[3] != 0) ? (this.a[Number(n[3])] || this.b[n[3][0]] + ' ' + this.a[n[3][1]]) + 'thousand ' : '';
// tslint:disable-next-line:triple-equals
str += (n[4] != 0) ? (this.a[Number(n[4])] || this.b[n[4][0]] + ' ' + this.a[n[4][1]]) : 'hundred';
// tslint:disable-next-line:triple-equals
str += (n[5]) ? (this.a[Number(n[5])] || this.b[n[5][0]] + ' ' + this.a[n[5][1]]) : '';
// tslint:disable-next-line:triple-equals
str += (n[6]) ? ((str != '') ? 'point ' : '') + (this.a[Number(n[6])] || this.b[n[6][0]] + ' ' + this.a[n[6][1]]) : '';
// tslint:disable-next-line:triple-equals
str += (n[7] != 0) ? (this.a[Number(n[7])] || this.b[n[7][0]] + ' ' + this.a[n[7][1]]) : '';
return str;
}
Define a filter to convert number to word such as following code:
angular.module('myModuleName')
.filter('convertToWord', function() {
return function(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
};
});
Then in your templates (views) use this filter as follow:
{{amount | converToWord}}
For example to show inserted value in an input field:
<input type="text" name="number" placeholder="Number OR Amount" ng-model="myValue" />
<div id="word">{{myValue | convertToWord}}</div>

Trying to poll for a MSP430's Keypad Input

I'm working on a really simplified version of space invaders. I generate some aliens then as time goes on I lower them closer to the bottom of a screen which I'm just using arrays for. I've got it generating and lowering the invaders but when I tried to hit the keypad button to delete an alien it was ignored. I believe its because I'm not constantly looking for the Keypad's input so my checkInput() function isn't doing what it should do. I wanted to poll for it but I am not sure how or where I should do it. Any help would be appreciated.
#include <msp430.h>
#include "peripherals.h"
#include <stdlib.h>
// Function Prototypes
void swDelay(char numLoops);
void countDown();
//void speedCheck();
void generateAliens();
void inputCheck();
void displayAliens();
void initLeds(void);
// Declare globals here
int game;
//int win;
//int turn;
//int levelSpeed;
char arrAliens0[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens1[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens2[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens3[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens4[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens5[5] = {' ', ' ', ' ', ' ', ' '};
char arrTempAliens1[5];
char arrTempAliens2[5];
char arrTempAliens3[5];
char arrTempAliens4[5];
char arrTempAliens5[5];
void main(void)
{
//unsigned char ret_val = 0x0F;
unsigned char currKey=0;
// Define some local variables
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Useful code starts here
initLeds();
configDisplay();
configKeypad();
while (1) // Forever loop
{
GrClearDisplay(&g_sContext); // Clear the display
while (getKey() != '*')
{
// *** Intro Screen ***
GrStringDrawCentered(&g_sContext, "Space Invaders!", AUTO_STRING_LENGTH, 48, 30, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, "Press '*' to", AUTO_STRING_LENGTH, 48, 50, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, "Start the Game", AUTO_STRING_LENGTH, 48, 65, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
}
currKey = getKey();
if (currKey == '*')
{
game = 1;
//turn = 0;
countDown();
while (game == 1)
{
//speedCheck();
generateAliens();
inputCheck();
//displayAliens();
//turn++;
}
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, "You Lose!", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
}
} // end while (1)
} //end main
void countDown()
{
//3 Count
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, "3", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
//2 Count
GrStringDrawCentered(&g_sContext, "2", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
//1 Count
GrStringDrawCentered(&g_sContext, "1", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
}
/*void speedCheck()
{
if (turn <= 3)
{
levelSpeed = 10;
}
else if ((turn > 3) && (turn <= 6))
{
levelSpeed = 8;
}
else if ((turn > 6) && (turn <= 9))
{
levelSpeed = 4;
}
else if ((turn > 9) && (turn <= 12))
{
levelSpeed = 2;
}
else
{
levelSpeed = 1;
}
}*/
void generateAliens()
{
memcpy(arrTempAliens1, arrAliens1, 5);
memcpy(arrTempAliens2, arrAliens2, 5);
memcpy(arrTempAliens3, arrAliens3, 5);
memcpy(arrTempAliens4, arrAliens4, 5);
memcpy(arrTempAliens5, arrAliens5, 5);
memcpy(arrAliens1, arrAliens0, 5);
memcpy(arrAliens2, arrTempAliens1, 5);
memcpy(arrAliens3, arrTempAliens2, 5);
memcpy(arrAliens4, arrTempAliens3, 5);
memcpy(arrAliens5, arrTempAliens4, 5);
/*arrTempAliens1 = arrAliens1;
arrTempAliens2 = arrAliens2;
arrTempAliens3 = arrAliens3;
arrTempAliens4 = arrAliens4;
arrTempAliens5 = arrAliens5;
arrAliens1 = arrAliens0;
arrAliens2 = arrTempAliens1;
arrAliens3 = arrTempAliens2;
arrAliens4 = arrTempAliens3;
arrAliens5 = arrTempAliens4;*/
int a = rand() % 4;
if (a == 0)
{
arrAliens0[0] = '0';
arrAliens0[1] = ' ';
arrAliens0[2] = ' ';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 1)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = ' ';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 2)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = ' ';
arrAliens0[4] = ' ';
}
else if (a == 3)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = '3';
arrAliens0[4] = ' ';
}
else if (a == 4)
{
arrAliens0[0] = '0';
arrAliens0[1] = '1';
arrAliens0[2] = '2';
arrAliens0[3] = '3';
arrAliens0[4] = '4';
}
displayAliens();
}
void inputCheck()
{
unsigned char currKey = getKey();
if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')
{
if (currKey == '0')
{
if ((currKey == '0') && (arrAliens4[0] == '0'))
{
arrAliens4[0] = ' ';
setLeds(0x30);
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] == '0'))
{
arrAliens3[0]= ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] == '0'))
{
arrAliens2[0] = ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] == '0'))
{
arrAliens1[0] = ' ';
}
else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] != '0') && (arrAliens0[0] == '0'))
{
arrAliens0[0] = ' ';
}
}
if (currKey == '1')
{
if ((currKey == '1') && (arrAliens4[1] == '1'))
{
arrAliens4[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] == '1'))
{
arrAliens3[1]= ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] == '1'))
{
arrAliens2[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] == '1'))
{
arrAliens1[1] = ' ';
}
else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] != '1') && (arrAliens0[1] == '1'))
{
arrAliens0[1] = ' ';
}
}
if (currKey == '2')
{
if ((currKey == '2') && (arrAliens4[2] == '2'))
{
arrAliens4[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] == '2'))
{
arrAliens3[2]= ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] == '2'))
{
arrAliens2[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] == '2'))
{
arrAliens1[2] = ' ';
}
else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] != '2') && (arrAliens0[2] == '2'))
{
arrAliens0[2] = ' ';
}
}
if (currKey == '3')
{
if ((currKey == '3') && (arrAliens4[3] == '3'))
{
arrAliens4[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] == '3'))
{
arrAliens3[3]= ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] == '3'))
{
arrAliens2[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] == '3'))
{
arrAliens1[3] = ' ';
}
else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] != '3') && (arrAliens0[3] == '3'))
{
arrAliens0[3] = ' ';
}
}
if (currKey == '4')
{
if ((currKey == '4') && (arrAliens4[4] == '4'))
{
arrAliens4[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] == '4'))
{
arrAliens3[4]= ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] == '4'))
{
arrAliens2[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] == '4'))
{
arrAliens1[4] = ' ';
}
else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] != '4') && (arrAliens0[4] == '4'))
{
arrAliens0[4] = ' ';
}
}
}
else if ((arrAliens5[0] | arrAliens5[1] | arrAliens5[2] | arrAliens5[3] | arrAliens5[4]) != ' ')
{
game = 0;
}
}
void displayAliens()
{
GrClearDisplay(&g_sContext);
GrStringDrawCentered(&g_sContext, arrAliens0, 5, 48, 5, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens1, 5, 48, 25, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens2, 5, 48, 45, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens3, 5, 48, 65, TRANSPARENT_TEXT);
GrStringDrawCentered(&g_sContext, arrAliens4, 5, 48, 85, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
}
void swDelay(char numLoops)
{
volatile unsigned int i,j; // volatile to prevent optimization
// by compiler
for (j=0; j<numLoops; j++)
{
i = 50000 ; // SW Delay
while (i > 0) // could also have used while (i)
i--;
}
}
if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')
This condition is never true because && is a boolean operator and results in either 0 or 1, which can never be equal to the value of the space character.

SQLServerException Invalid column name

I have a problem that doesn't appear always, but it do it most of the times. In my huge java forecast class I have some ResultSets, and when I execute the routine I get:
com.microsoft.sqlserver.jdbc.SQLServerException: El nombre de columna DistanciaMision no es vßlido.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(SQLServerResultSet.java:626)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(SQLServerResultSet.java:2301)
at es.csic.iiia.udt.itim.iInformation.WebData.Forecast.etaMSR(Forecast.java:1109)
at es.csic.iiia.udt.itim.iInformation.WebData.Forecast.phase2(Forecast.java:662)
at es.csic.iiia.udt.itim.iInformation.WebData.Forecast.setData(Forecast.java:166)
at es.csic.iiia.udt.itim.iInformation.WebData.Forecast.main(Forecast.java:81)
at es.csic.iiia.udt.itim.iInformation.WebData.Forecast.execute(Forecast.java:71)
at org.quartz.core.JobRunShell.run(JobRunShell.java:199)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:546)
The column exists, so I don't know what is the problem...
The code line is this one:
Float distancia_restante = (float) ( Integer.parseInt(rs.getString("DistanciaMision")) - (myodometer - initialodometer));
The same problem appears on other columns but it usually crash here.
Thank you!
Edit: ok, this is the whole method:
private static void etaMSR() throws Exception, SQLException {
/**
*
* Calculem ETAN MSR - Mision - Seguimiento - Restricciones conductor
*
*
**/
System.out
.print("Get data from iTIM forecast&forecastAUX DDBB ....... ");
myStatement = MSSQLServerAccess.connection();
// Distancia mision, ruta, hora mision anterior, hora
rs = getTable("SELECT dbo.WebForecast.IdConductor, dbo.WebForecast.IdMision, dbo.WebForecast.IdMisionAnterior, dbo.WebForecast.DistanciaMision, "
+ " WebForecast_1.HoraIniMis AS himanterior, dbo.WebForecast.HoraFiMis AS hfmactual, WebForecast_1.Ciudad AS CiudadOrigen,"
+ " dbo.WebForecast.Ciudad AS CiudadDestino, dbo.Distancias.Ruta, dbo.WebForecast.HoraDistancia AS HoraDistancia"
+ " FROM dbo.WebForecast AS WebForecast_1 INNER JOIN"
+ " dbo.Distancias ON WebForecast_1.Ciudad = dbo.Distancias.Origen RIGHT OUTER JOIN"
+ " dbo.WebForecast ON WebForecast_1.IdMision = dbo.WebForecast.IdMisionAnterior AND dbo.Distancias.Destino = dbo.WebForecast.Ciudad"
+ " WHERE (dbo.WebForecast.IdConductor <> '') AND (CONVERT(datetime, '"
+ df.format(fechaDia)
+ "') <= dbo.WebForecast.HoraFiMis) "
+ " AND WebForecast_1.HoraIniMis <= CONVERT(datetime, '"
+ df.format(fechaDia) + "') ");
System.out.println("[ok]");
while (rs.next() && (rs.getString("IdConductor") != "") && org.apache.commons.lang.StringUtils.isNumeric(rs.getString("IdConductor"))) {
int initialodometer = 0;
String start = null;
if (rs.getString("HoraDistancia") != null) {
start = rs.getString("HoraDistancia");
}
if (rs.getString("himanterior") != null) {
start = rs.getString("himanterior");
}
if (start != null) {
ResultSet myrs = null;
Timestamp tobjetivo = rs.getTimestamp("himanterior");
long boundtime = 7200000; // 3600000 = 60m = 1h
Timestamp tini = (Timestamp) rs.getTimestamp("himanterior")
.clone();
Timestamp tfin = (Timestamp) rs.getTimestamp("himanterior")
.clone();
tini.setTime(tini.getTime() - boundtime);
tfin.setTime(tfin.getTime() + boundtime);
int contador = 0;
long bestdiff = 0;
myStatement = MSSQLServerAccess.connection();
myrs = getTable("SELECT DISTINCT Odometer, DT "
+ "FROM DriverEvents "
+ "WHERE (DT BETWEEN CONVERT(datetime, '"
+ df.format(tini) + "') " + "AND CONVERT(datetime, '"
+ df.format(tfin) + "')) " + "AND (CardId = '"
+ Integer.parseInt(rs.getString("IdConductor")) + "')");
int j = 0;
while (!myrs.next() && (j < 20)) {
// En caso de no encontrar en las 2h antes y despues nada:
tini.setTime(tini.getTime() - boundtime);
tfin.setTime(tfin.getTime() + boundtime);
myrs.close();
myStatement = MSSQLServerAccess.connection();
myrs = getTable("SELECT DISTINCT Odometer, DT "
+ "FROM DriverEvents "
+ "WHERE (DT BETWEEN CONVERT(datetime, '"
+ df.format(tini) + "') "
+ "AND CONVERT(datetime, '" + df.format(tfin)
+ "')) " + "AND (CardId = '"
+ Integer.parseInt(rs.getString("IdConductor"))
+ "')");
j++;
}
if (myrs.next()) {
initialodometer = myrs.getInt("Odometer");
bestdiff = Math.abs(tobjetivo.getTime()
- myrs.getTimestamp("DT").getTime());
contador++;
while (myrs.next()) {
long pretendiente = Math.abs(tobjetivo.getTime()
- myrs.getTimestamp("DT").getTime());
if (pretendiente <= bestdiff) {
bestdiff = pretendiente;
initialodometer = myrs.getInt("Odometer");
}
contador++;
}
}
myrs.close();
}
// Get Odometer distance at the moment
if (!rs.getString("IdConductor").isEmpty() && !rs.getString("IdConductor").equals("") ) {
ResultSet myrs = null;
int myodometer = 0;
myStatement = MSSQLServerAccess.connection();
myrs = getTable("SELECT MAX(DT) AS DT, MAX(Odometer) AS Odometer"
+ " FROM dbo.DriverEvents"
+ " WHERE (CardId = '"
+ Integer.parseInt(rs.getString("IdConductor"))
+ "') AND (DT > CONVERT(datetime, '"
+ df.format(fechaDatos) + "')) ");
if (myrs.next()) {
myodometer = myrs.getInt("Odometer");
if (initialodometer == 0)
initialodometer = myodometer;
Float distancia_restante = (float) ( Integer.parseInt(rs.getString("DistanciaMision")) - (myodometer - initialodometer));
if (distancia_restante < 0)
distancia_restante = (float) 0;
Timestamp ETAN = null;
Calendar cal = Calendar.getInstance();
if (rs.getTimestamp("himanterior") != null && rs.getTimestamp("himanterior").toString() != "") {
cal.setTimeInMillis(rs.getTimestamp("himanterior")
.getTime());
if (cal.after(Calendar.getInstance())) {
cal.setTimeInMillis(rs.getTimestamp("himanterior")
.getTime());
}
if (cal.before(Calendar.getInstance())) {
cal = Calendar.getInstance();
}
} else {
if (rs.getTimestamp("HoraDistancia") != null)
cal.setTimeInMillis(rs
.getTimestamp("HoraDistancia").getTime());
}
myStatement = MSSQLServerAccess.connection();
rs2 = getTable("SELECT TOP (100) PERCENT CardId, DT"
+ " FROM dbo.DriverEvents"
+ " GROUP BY CardId, DT"
+ " HAVING (CardId = '"
+ Integer.parseInt(rs.getString("IdConductor"))
+ "') AND (DT > CONVERT(datetime, '"
+ df.format(fechaDatos) + "'))");
if (rs2.next()) {
ETAN = getETAN(rs, distancia_restante, cal);
} else {
ETAN = getETA(rs, distancia_restante, cal, 1);
Statement myStatement2 = MSSQLServerAccess.connection();
myStatement2.executeUpdate("UPDATE WebForecast "
+ "SET ETAmsr = '" + df.format(ETAN)
+ "', KmsDiff = '" + distancia_restante.intValue()
+ "' " + "WHERE IdMision = '"
+ rs.getString("IdMision") + "'");
} else {
}
}
}
rs.close();
}
And the statement, maybe i'm doing something wrong?:
private static Statement conStatement(Properties properties){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // Load the JDBC driver
String dbURL = "jdbc:sqlserver://"
+ properties.getProperty("qualcomm.action.JDBC.MSSQLServerAccess.ddbbserverIP")
+ ";DatabaseName="
+ properties.getProperty("qualcomm.action.JDBC.MSSQLServerAccess.ddbbName")
+ ";SelectMethod=Cursor;"; // Connect to a server and database
String userName = properties.getProperty("qualcomm.action.JDBC.MSSQLServerAccess.userName");
String userPwd = properties.getProperty("qualcomm.action.JDBC.MSSQLServerAccess.userPwd");
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
//log.info("Connection Successful!");
Statement myst = dbConn.createStatement();
return myst;
} catch (Exception e) {
log.error(e);
System.out.println(e);
return null;
}
}
Thanks guys :) Could be something wrong with the statement?
remember that Java is case-sensitive and so can be your table on SQL depending on the way you created them, actually depending on the collation on your DB. If your database is created with a Case Sensitive collation then all object names will be Case Sensitive.
try to check the exact columns name of the column on SQL and access it using [] and the exact case

Resources