I'm getting the error:-
error: cannot find symbol
tofile.println("Dancing" + " " + danceminutes + " " + bpm);
^
symbol: variable tofile
location: class CreateFile
I'm not sure why.
// writing to a file
PrintWriter toFile = new PrintWriter(new FileWriter("exLog.txt"));
Random randy = new Random();
int count = randy.nextInt(12) + 19;
for (int i = count; i > 0; i--) {
int exercise = randy.nextInt(3);
if (exercise == 0) {
int minutesOne = randy.nextInt(47) + 14;
int minutesTwo = randy.nextInt(62) + 59;
int minutesThree = randy.nextInt(122) + 119;
int roulette = randy.nextInt(100);
if (roulette <= 74) {
int runoneminutes = minutesOne;
double speed = 4.9 + (16.1 - 4.9) * randy.nextDouble();
double distance = speed * runoneminutes/60;
tofile.println("Running" + " " + runoneminutes + " " + distance);
}
else if (roulette > 74 && roulette < 96) {
int runtwominutes = minutesTwo;
double twospeed = 4.9 + (16.1 - 4.9) * randy.nextDouble();
double twodistance = twospeed * runtwominutes/60;
tofile.println("Running" + " " + runtwominutes + " " + twodistance);
}
else if (roulette >= 96) {
int runthreeminutes = minutesThree;
double threespeed = 4.9 + (16.1 - 4.9) * randy.nextDouble();
double threedistance = threespeed * runthreeminutes/60;
tofile.println("Running" + " " + runthreeminutes + " " + threedistance);
}
}
if (exercise == 1) {
int bikeminutesOne = randy.nextInt(62) + 29;
int bikeminutesTwo = randy.nextInt(212) + 89;
int bikeroulette = randy.nextInt(100);
if (bikeroulette <= 49) {
int bikeoneminutes = bikeminutesOne;
double bikeonespeed = 7.9 + (25.1 - 7.9) * randy.nextDouble();
double bikeonedistance = bikeonespeed * bikeoneminutes/60;
tofile.println("Biking" + " " + bikeoneminutes + " " + bikeonedistance);
}
else if (bikeroulette > 49) {
int biketwominutes = bikeminutesTwo;
double biketwospeed = 7.9 + (25.1 - 7.9) * randy.nextDouble();
double biketwodistance = biketwospeed * biketwominutes/60;
tofile.println("Biking" + " " + biketwominutes + " " + biketwodistance);
}
}
if (exercise == 2) {
int exercises = randy.nextInt(15) + 1;
int reps = randy.nextInt(37) + 9;
double liftminutes;
int liftminutes = 5*reps*exercises/60 + 2*(exercises-1);
tofile.println("Lifting" + " " + liftminutes + " " + reps);
}
if (exercise == 3) {
int danceminutes = randy.nextInt(90) + 29;
double bpm = 79.9 + (220 - 79.9) * randy.nextDouble();
tofile.println("Dancing" + " " + danceminutes + " " + bpm);
}
}
toFile.close(); //saving output file
} // end main
} // end CreateFile class
The variable name is toFile. The compiler is complaining about the use of 'tofile'.
Related
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
I've been working on the edge filter function for cs50 for a few hours now. It looks fine to me but I'm receiving a pretty scary looking error which I don't understand here is my code:
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
copy[h][w] = image[h][w];
}
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float gxred = 0;
float gxgreen = 0;
float gxblue = 0;
float gyred = 0;
float gygreen = 0;
float gyblue = 0;
for (int r = -1; r < 2; r++)
{
for (int s = -1; s < 2; s++)
{
if ((y + r < height && y + r >= 0) && (x + s < width && x + s >= 0))
{
if (r == -1)
{
gxred = copy[y + r][x + s].rgbtRed * s;
gxgreen = copy[y + r][x + s].rgbtGreen * s;
gxblue = copy[y + r][x + s].rgbtBlue * s;
gyred = copy[x + s][y + r].rgbtRed * s;
gygreen = copy[x + s][y + r].rgbtGreen * s;
gyblue = copy[x + s][y + r].rgbtBlue * s;
}
if (r == 0)
{
gxred = copy[y + r][x + s].rgbtRed * (s * 2);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * 2);
gxblue = copy[y + r][x + s].rgbtBlue * (s * 2);
gyred = copy[x + s][y + r].rgbtRed * (s * 2);
gygreen = copy[x + s][y + r].rgbtGreen * (s * 2);
gyblue = copy[x + s][y + r].rgbtBlue * (s * 2);
}
if (r == 1)
{
if (s * s == 1)
{
gxred = copy[y + r][x + s].rgbtRed * (s * s);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * s);
gxblue = copy[y + r][x + s].rgbtBlue * (s * s);
gyred = copy[x + s][y + r].rgbtRed * (s * s);
gygreen = copy[x + s][y + r].rgbtGreen * (s * s);
gyblue = copy[x + s][y + r].rgbtBlue * (s * s);
}
else
{
gxred = copy[y + r][x + s].rgbtRed * (s + 1);
gxgreen = copy[y + r][x + s].rgbtGreen * (s + 1);
gxblue = copy[y + r][x + s].rgbtBlue * (s + 1);
gyred = copy[x + s][y + r].rgbtRed * (s + 1);
gygreen = copy[x + s][y + r].rgbtGreen * (s + 1);
gyblue = copy[x + s][y + r].rgbtBlue * (s + 1);
}
}
}
}
}
image[y][x].rgbtRed = round(sqrt((gxred * gxred) + (gyred * gyred)));
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
image[y][x].rgbtBlue = round(sqrt((gxblue * gxblue) + (gyblue * gyblue)));
image[y][x].rgbtRed = image[y][x].rgbtRed % 256;
image[y][x].rgbtGreen = image[y][x].rgbtGreen % 256;
image[y][x].rgbtBlue = image[y][x].rgbtBlue % 256;
}
}
return;
}
And here is my error
helpers.c:169:37: runtime error: 261 is outside the range of representable values of type
'unsigned char'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==5405==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7ffea18b01fa (pc
0x000000429bb3 bp 0x7ffea18af1a0 sp 0x7ffea17fe250 T5405)
==5405==The signal is caused by a READ memory access.
#0 0x429bb2 (/home/ubuntu/pset4/filter/filter+0x429bb2)
#1 0x42330e (/home/ubuntu/pset4/filter/filter+0x42330e)
#2 0x7f7c0b22eb96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x402e19 (/home/ubuntu/pset4/filter/filter+0x402e19)
UndefinedBehaviorSanitizer can not provide additional info.
==5405==ABORTING
This directs me to this line at the end:
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
I'd be really grateful if someone could explain what this kind of error means. And also, any critique on my code would be much appreciated, I'm trying to get good at this.
RGB values can be any value between 0 and 255.
You are somehow assigning your RGB value in that line to 261 which is outside of the range.
I would print the value in the following code before assigning to rgb value.
round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)))
Then check why inside of round gives 261. Once you fix this you are good to go.
I'm creating a particles engine in C (with the CSFML library) where each particle has a position on {X,Y}, a life, and a movement vector in {X,Y}.
I'm actually creating X particles at the same position on my screen (ex: {100, 100}, and i wan't to make them moving to create a growing circle from the initial position.
What is the mathematical function that can help me to make this ?
#include <SFML/Graphics/Vertex.h>
#include <math.h>
#include "main.h"
int my_rand(int a, int b){
return rand()%(b-a) +a;
}
partBuffer *newPartBuffer(int size)
{
partBuffer *this;
const size_t size_m = (sizeof(partBuffer) + sizeof(sfVertex) * size * 4
+ sizeof(t_info) * size);
void *ptn = malloc(size_m);
if (ptn == NULL)
return (NULL);
memset(ptn, 0, size_m);
this = (partBuffer*)(ptn);
this->size = size;
this->vertex = (sfVertex*)(ptn + sizeof(partBuffer));
this->info = (t_info*)(this->vertex + (size * 4));
return (this);
}
void setPart(partBuffer *this, uint id, sfVector2f pos)
{
if (id >= this->size)
return ;
this->vertex[(id * 4) + 0].position = (sfVector2f){pos.x + 0, pos.y + 0};
this->vertex[(id * 4) + 1].position = (sfVector2f){pos.x + 5, pos.y + 0};
this->vertex[(id * 4) + 2].position = (sfVector2f){pos.x + 5, pos.y + 5};
this->vertex[(id * 4) + 3].position = (sfVector2f){pos.x + 0, pos.y + 5};
this->vertex[(id * 4) + 0].color = (sfColor){255, 255, 255};
this->vertex[(id * 4) + 1].color = (sfColor){255, 255, 255};
this->vertex[(id * 4) + 2].color = (sfColor){255, 255, 255};
this->vertex[(id * 4) + 3].color = (sfColor){255, 255, 255};
//this->info[id].speed = (sfVector2f){my_rand(-3, 3), my_rand(-3, 3)};
this->info[id].speed = (sfVector2f){0, 0};
this->info[id].life = 1.0;
}
static uint newPart(partBuffer *this)
{
for (uint id = this->size - 1; id != 0; id -= 1)
if (this->info[id].life <= 0)
return (id);
return ((uint)(-1));
}
void updatePartBuffer(partBuffer *this)
{
for (uint id = 0; id < this->size; id += 1) {
if (this->info[id].life > 0)
this->info[id].life -= 0.0;
if (this->info[id].life <= 0)
this->info[id].life = 0.0;
this->vertex[(id * 4) + 0].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 1].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 2].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 3].position.x += this->info[id].speed.x;
this->vertex[(id * 4) + 0].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 1].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 2].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 3].position.y += this->info[id].speed.y;
this->vertex[(id * 4) + 0].color.a = (sfUint8)(this->info[id].life * 255.);
this->vertex[(id * 4) + 1].color.a = (sfUint8)(this->info[id].life * 255.);
this->vertex[(id * 4) + 2].color.a = (sfUint8)(this->info[id].life * 255.);
this->vertex[(id * 4) + 3].color.a = (sfUint8)(this->info[id].life * 255.);
}
}
void drawPartBuffer(partBuffer *this, sfRenderWindow *window)
{
sfRenderWindow_drawPrimitives(window, this->vertex, this->size * 4,
sfQuads, NULL);
}
void set_circle(partBuffer *this, float points)
{
sfVector2f start = {0, -1};
sfVector2f adder = {1 / (points), 1 / (points)};
for (uint id = 0; id < points; id += 1) {
this->info[id].speed = (sfVector2f){start.x + adder.x * id, start.y + adder.y * id};
}
}
int game_loop(sfRenderWindow *window)
{
partBuffer *buffer = newPartBuffer(10000);
int points = 11;
sfClock *clock = sfClock_create();
sfVector2f position = {250, 250};
for (uint nb = 0; nb < points; nb += 1)
setPart(buffer, nb, position);
set_circle(buffer, points);
while (sfRenderWindow_isOpen(window)) {
if (sfTime_asMilliseconds(sfClock_getElapsedTime(clock)) >= 10) {
updatePartBuffer(buffer);
sfClock_restart(clock);
}
sfRenderWindow_clear(window, sfBlack);
drawPartBuffer(buffer, window);
sfRenderWindow_display(window);
}
}
int main(void)
{
sfRenderWindow *window = sfRenderWindow_create((sfVideoMode){500, 500, 32},
"Title", sfDefaultStyle, NULL);
sfRenderWindow_setFramerateLimit(window, 120);
game_loop(window);
return (0);
}
I have an assignment where I am supposed to take grades entered and output the amount of each grade and the percentage of that grade. The loop should stop when a negative number is entered. I think I have it now but I can't get it to compile.
import java.util.Scanner;
public class Homework6_Project1
{
public static void main (String[] args)
{
Scanner Keyboard = new Scanner(System.in);
double count = Keyboard.nextDouble();
gradecounter:
do
{
for (int i = 0; i < count; i++)
{
System.out.println("Enter the grade:");
int Grade = Keyboard.nextInt();
}
if (Grade >=90)
{
int CountA = 0;
int A = Grade;
double AverageA = A/count;
++CountA;
}
if (Grade<89 && Grade>=80)
{
int CountB = 0;
int B = Grade;
double AverageB = B/count;
++CountB;
}
if (Grade < 80 && Grade >=70)
{
int CountC = 0;
int C = Grade;
double AverageC = C/count;
++CountC;
}
if (Grade < 70 && Grade >= 60)
{
int CountD = 0;
int D = Grade;
double AverageD = D/count;
++CountD;
}
if (Grade < 60 && Grade >= 0)
{
int CountF = 0;
int F = Grade;
double AverageF = F/count;
++CountF;
}
while (Grade < 0)
{
System.out.println("Count of A grade's:" + " " + CountA + " Percentage of A's:" + " " + AverageA);
System.out.println("Count of B grade's:" + " " + CountB + " Percentage of B's:" + " " + AverageB);
System.out.println("Count of C grade's:" + " " + CountC + " Percentage of C's:" + " " + AverageC);
System.out.println("Count of D grade's:" + " " + CountD + " Percentage of D's:" + " " + AverageD);
System.out.println("Count of F grade's:" + " " + CountF + " Percentage of F's:" + " " + AverageF);
break gradecounter;
}
}
}
}
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