String was not recognized as a valid DateTime in Android - microsoft-dynamics

I am developing an Android client for Microsoft Dynamics NAV. I am using ksoap2 for making communication with NAV webservices. I used MarshalDate class to serialize date. When I pass Date to webservice I am getting an error like "String was not recognized as a valid DateTime."
MarshalDate.java
public class MarshalDate implements Marshal {
public static Class DATE_CLASS = new Date().getClass();
public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException {
return IsoDate.stringToDate(parser.nextText(), IsoDate.DATE_TIME);
}
public void register(SoapSerializationEnvelope cm) {
cm.addMapping(cm.xsd, "dateTime", Date.class, this); }
public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME)); } }
My object for salesordercard,
SalesOrderCard.java
public class SalesOrderCard implements KvmSerializable
{
String Key;
String No;
String Sell_to_Customer_No;
Sales_Order_Line_List Salesorderlinelist;
String ShopNo;
Date OrderDate;
double Received_Amount;
boolean Cheque;
public SalesOrderCard(){}
public SalesOrderCard(String key,String no,String selltocustno,Sales_Order_Line_List salesorderline,String shopno,Date orderdate ,double received_Amount,boolean cheque) {
Key = key;
No = no;
Sell_to_Customer_No = selltocustno;
Salesorderlinelist=salesorderline;
ShopNo=shopno;
OrderDate=orderdate;
received_Amount=Received_Amount;
Cheque=cheque;
}
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Key;
case 1:
return No;
case 2:
return Sell_to_Customer_No;
case 3:
return Salesorderlinelist;
case 4:
return ShopNo;
case 5:
return Received_Amount;
case 6:
return Cheque;
case 7:
return OrderDate;
}
return null;
}
public int getPropertyCount() {
return 8;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Key";
break;
case 1:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "No";
break;
case 2:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Sell_to_Customer_No";
break;
case 3:
info.type = Sales_Order_Line_List.class;
info.name = "SalesLines";
break;
case 4:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Shop_No";
break;
case 5:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Received_Amount";
break;
case 6:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Cheque";
break;
case 7:
info.type = MarshalDate2.DATE_CLASS;
info.name = "Order_Date";
break;
default:break;
}
}
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Key = value.toString();
break;
case 1:
No = value.toString();
break;
case 2:
Sell_to_Customer_No = value.toString();
break;
case 3:
Salesorderlinelist = (Sales_Order_Line_List)value;
break;
case 4:
ShopNo = value.toString();
break;
case 5:
Received_Amount = Double.parseDouble(value.toString());
break;
case 6:
Cheque = Boolean.parseBoolean(value.toString());
break;
case 7:
OrderDate=(Date)value;
break;
default:
break;
}
}
}
And I am passing date like,
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
SalesOrderCard cr=new SalesOrderCard();
cr.OrderDate=d;
PropertyInfo custProp = new PropertyInfo();
custProp.setName("SalesOrderCard");
custProp.setValue(cr);
custProp.setType(SalesOrderCard.class);
request.addProperty(custProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
MarshalDouble md = new MarshalDouble();
md.register(envelope);
MarshalDate mdate=new MarshalDate();
mdate.register(envelope);

Assuming d is a Date this code looks correct
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
I suspect this line may be the problem
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME));
If you know the format writer.text expects then I would try using SimpleDateFormat to create the string for you instead:
String dateString = format.format((Date)obj);

Related

I need to group multiple functions into one function

I need to group multiple if functions into one big function with a custom name.
if (NPC == NPC1)
{
Attack = NPCAttack1;
}
if (NPC == NPC2)
{
Attack = NPCAttack2;
}
if (NPC == NPC3)
{
Attack = NPCAttack3;
}
if (NPC == NPC4)
{
Attack = NPCAttack4;
}
if (NPC == NPC5)
{
Attack = NPCAttack5;
}
if (NPC == NPC6)
{
Attack = NPCAttack6;
}
if (NPC == NPC7)
{
Attack = NPCAttack7;
}
if (NPC == NPC8)
{
Attack = NPCAttack8;
}
if (NPC == NPC9)
{
Attack = NPCAttack9;
}
if (NPC == NPC10)
{
Attack = NPCAttack10;
}
I want all of that to be inside a function called AttackFunction. How do I do that?
First of all, let's replace all of those if statements with a single switch statement:
switch (NPC) {
case NPC1:
Attack = NPCAttack1;
break;
case NPC2:
Attack = NPCAttack2;
break;
case NPC3:
Attack = NPCAttack3;
break;
case NPC4:
Attack = NPCAttack4;
break;
case NPC5:
Attack = NPCAttack5;
break;
case NPC6:
Attack = NPCAttack6;
break;
case NPC7:
Attack = NPCAttack7;
break;
case NPC8:
Attack = NPCAttack8;
break;
case NPC9:
Attack = NPCAttack9;
break;
case NPC10:
Attack = NPCAttack10;
break;
}
Now we can inject this code into a function, which receive the NPC, and return the desired type of attack.
<attack_type> get_attack_type_from_npc(<npc_type> NPC) {
switch (NPC) {
case NPC1:
return NPCAttack1;
case NPC2:
return NPCAttack2;
case NPC3:
return NPCAttack3;
case NPC4:
return NPCAttack4;
case NPC5:
return NPCAttack5;
case NPC6:
return NPCAttack6;
case NPC7:
return NPCAttack7;
case NPC8:
return NPCAttack8;
case NPC9:
return NPCAttack9;
case NPC10:
return NPCAttack10;
}
throw std::runtime_error("No attack type found");
}
in your main:
int main() {
// ... Declare Attack & NPC ... Set NPC ...
Attack = get_attack_type_from_npc(NPC);
}

'Pointer to member' error C++

I need some help with this error, I don't understand what has to be done to get rid of it. I am trying to build this code - the last exercise, Graduation. Here is my code:
// Bunnies.cpp : Defines the entry point for the console application.
// Male = 0; Female = 1
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include<stdio.h>
#include<time.h>
using namespace std;
int num;
class bunny {
int val;
int gender;
int colour;
int age;
int type;
bool radioBun;
string name;
public:
void create() {
val = num;
age = 1;
gender = rand() % 2;
switch (rand() % 100) {
case 1: radioBun = true;
break;
case 2: radioBun = true;
break;
default: radioBun = false;
break;
}
if (gender = 0) {
switch (rand() % 20) {
case 0: name = "Bob";
break;
case 1: name = "Alexis";
break;
case 2: name = "William";
break;
case 3: name = "Cleo";
break;
case 4: name = "Mark";
break;
case 5: name = "Jarod";
break;
case 6: name = "Billie";
break;
case 7: name = "Nathan";
break;
case 8: name = "Richard";
break;
case 9: name = "Thomas";
break;
case 10: name = "Rudolf";
break;
case 11: name = "Troy";
break;
case 12: name = "Wesley";
break;
case 13: name = "Jacob";
break;
case 14: name = "Cody";
break;
case 15: name = "Gavin";
break;
case 16: name = "Norris";
break;
case 17: name = "Matt";
break;
case 18: name = "Colton";
break;
case 19: name = "Daniel";
break;
}
}
else {
switch (rand() % 20) {
case 0: name = "Cecila";
break;
case 1: name = "Scarlet";
break;
case 2: name = "Abby";
break;
case 3: name = "Sandra";
break;
case 4: name = "Melissa";
break;
case 5: name = "Lizabeth";
break;
case 6: name = "Susie";
break;
case 7: name = "Cherly";
break;
case 8: name = "Kaitlin";
break;
case 9: name = "Debbie";
break;
case 10: name = "Evalyn";
break;
case 11: name = "Amalia";
break;
case 12: name = "Mendy";
break;
case 13: name = "Nora";
break;
case 14: name = "Brigitte";
break;
case 15: name = "Ebony";
break;
case 16: name = "Beatrice";
break;
case 17: name = "Tiffany";
break;
case 18: name = "Ying";
break;
case 19: name = "Kesha";
break;
}
}
}
void printCreate() {
cout << "Bunny ";
cout << val;
cout << " was zapped into existence!" << endl;
}
};
int main()
{
srand(time(0));
for (int x = 0; x == 10; x++) {
num = x;
bunny num;
num.create();
num.printCreate;
}
system("pause");
return 0;
}
I am working out a way to create 10 bunnies at the start of the code, by creating a variable num. The for loop runs 10 times, everytime increasing num by +1. Each time creating a new instance of the class bunnies and assigning the value of num to the name of that instance.
The void create() is not giving me any problems, its when I try to do void printCreate() that I get this error. It just comes up with "Severity Code Description Project File Line Suppression State
Error C3867 'bunny::printCreate': non-standard syntax; use '&' to create a pointer to member Bunnies c:\users\bob\source\repos\bunnies\bunnies\bunnies.cpp 145
". I have looked this up, but I can't understand how to fix this error. Could someone please tell me how to fix this in simple terms?
Replace num.printCreate; in your loop by num.printCreate();.

Capnproto D language Print all data in a file

I downloaded the capnproto for dlanguage and started tinkering the sample-addressbook.. My problem is, everytime I add a person to the addressbook, it still prints only one of the data in the file, instead of the whole file..
void writeAddressBook()
{
MessageBuilder message = new MessageBuilder();
auto addressbook = message.initRoot!AddressBook;
auto people = addressbook.initPeople(1);
auto person = people[0];
person.id = 123;
person.setName("Tomas");
person.email = "tomas#example.com";
auto personPhones = person.initPhones(1);
personPhones[0].number = "0917xxxxxxx";
personPhones[0].type = Person.PhoneNumber.Type.mobile;
person.employment.school = "School of Thoughts";
File f = File("1.dat","a+b");
SerializePacked.writeToUnbuffered(new FileDescriptor(f), message);
}
If I call the writeAddressBook() 4 times, then I have 4 People in the addressbook with the same name, The Problem is, everytime I print all of the data, it only prints the first one..
void printAddressBook()
{
File f = File("1.dat","r");
auto message = SerializePacked.readFromUnbuffered(new FileDescriptor(f));
auto addressbook = message.getRoot!AddressBook;
foreach(person; addressbook.people)
{
writefln("%s: %s", person.name, person.email);
foreach(phone; person.phones)
{
string typeName = "UNKNOWN";
switch(phone.type) with(Person.PhoneNumber.Type)
{
case mobile:
typeName = "mobile";
break;
case home:
typeName = "home";
break;
case work:
typeName = "work";
break;
default:
break;
}
writefln(" %s phone: %s", typeName, phone.number);
}
auto employment = person.employment;
switch(employment.which()) with(Person.Employment.Which)
{
case unemployed:
writefln(" unemployed");
break;
case employer:
writefln(" employer: %s", employment.employer);
break;
case school:
writefln(" student at: %s", employment.school);
break;
case selfEmployed:
writefln(" self-employed");
break;
default:
break;
}
}
}

Helps with TGrid

I'm a newbie in C++ Builder and really need some help. In my work, we have to use C++ Builder and I can't find very much documentation about it.
What I want to do is to insert a new row with some data in each column each time the user press the Insert key. There are 20 rows in total in my TGrid: 1 TCheckColumn and 19 TStringColumn.
I don't know how to do it exactly. First, here is the code I already wrote when someone enter the insert key:
void __fastcall TForm2::Grid1KeyDown(TObject *Sender, WORD &Key, System::WideChar &KeyChar,
TShiftState Shift)
{
switch(Key)
{
case VK_INSERT:
if(Grid1->RowCount>MAXTask){}else
{
Label1->Text = "number Task: "+IntToStr(++nmbertask);
Grid1->RowCount++;
}
break;
}
}
Here is the Grid1GetValue() and Grid1SetValue() code:
void __fastcall TForm2::Grid1GetValue(TObject *Sender, const int Col, const int Row,
TValue &Value)
{
switch(Col)
{
case 0:
Value = A[Col][Row]; break;
case 1:
Value = A[Col][Row]; break;
case 2:
Value = A[Col][Row]; break;
case 3:
Value = A[Col][Row]; break;
case 4:
Value = A[Col][Row]; break;
case 5:
Value = A[Col][Row]; break;
case 6:
Value = A[Col][Row]; break;
case 7:
Value = A[Col][Row]; break;
case 8:
Value = A[Col][Row]; break;
case 9:
Value = A[Col][Row];break;
case 10:
Value = A[Col][Row];break;
case 11:
Value = A[Col][Row];break;
case 12:
Value = A[Col][Row];break;
case 13:
Value = A[Col][Row];break;
case 14:
Value = A[Col][Row];break;
case 15:
Value = A[Col][Row];break;
case 16:
Value = A[Col][Row];break;
case 17:
Value = A[Col][Row];break;
case 18:
Value = A[Col][Row]; break;
case 19:
Value = A[Col][Row];break;
}
}
void __fastcall TForm2::Grid1SetValue(TObject *Sender, const int Col, const int Row,
const TValue &Value)
{
switch(Col)
{
case 0:
A[Col][Row] = Value; break;
case 1:
A[Col][Row] = Value; break;
case 2:
A[Col][Row] = Value; break;
case 3:
A[Col][Row] = Value; break;
case 4:
A[Col][Row] = Value; break;
case 5:
A[Col][Row] = Value; break;
case 6:
A[Col][Row] = Value; break;
case 7:
A[Col][Row] = Value; break;
case 8:
A[Col][Row] = Value; break;
case 9:
A[Col][Row] = Value; break;
case 10:
A[Col][Row] = Value; break;
case 11:
A[Col][Row] = Value; break;
case 12:
A[Col][Row] = Value; break;
case 13:
A[Col][Row] = Value; break;
case 14:
A[Col][Row] = Value; break;
case 15:
A[Col][Row] = Value; break;
case 16:
A[Col][Row] = Value; break;
case 17:
A[Col][Row] = Value; break;
case 18:
A[Col][Row] = Value; break;
case 19:
A[Col][Row] = Value; break;
}
}
This is what I've done so far. I've never developed in C++ Builder, so can you help me please? I'm not really looking for a written solution, I'm really looking for an indication of where to look for a solution.
If I'm not clear, please indicate it so I can give more information. If you also have a good site with good documentation about C++ Builder, please indicate it to me. English is not my native language but I will do my best.
You can find the TGrid documentation on Embarcadero's DocWiki. Look at the 'Methods' page to figure out how to insert and remove elements in your TGrid. There are two methods that can help you: InsertComponent() and `InsertObject().
Also, in your Grid1GetValue() and Grid1SetValue() methods, you are using a switch statement. In all those cases you are doing the same job :
Value = A[Col][Row];break;
So, instead of using a switch, you can simply call one time Value = A[Col][Row]; and it will do the same result.
I hope my answer can help you. At least I answered what I understood from your question.

I wanted to know how to properly use switch/case

I was just wondering if someone could just give me an example of how to use switch/case. I kinda get the just of it but am having trouble figuring out what and how i can use this. Thank you in advance.
There are couple of things to remember about switch case statements:
a) The condition should be integeral/enum/user defined type which supports conversion to int or enum
b) case lables are compile time constants
c) No two case label expressions can have the same value
d) $6.4.2/5- "When the switch statement is executed, its condition is evaluated and compared with each case constant. If one of the case constants is equal to the value of the condition, control is passed to the statement following the matched case label. If no case constant matches the condition, and if there is a default label,control passes to the statement labeled by the default label. If no case matches and if there is no default then none of the statements in the switch is executed."
e) $6.4.2/6- "case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels. To exit from a switch, see break"
enum direction {north, south, east, west};
char x;
class UD{
operator int(){return 0;}
};
direction f1(){
return north;
}
char f2(){
return 'A';
}
int main(){
direction d = f();
string country;
// switch condition of type enum
switch(d){
case north:
country = "P";
break;
case south:
country = "Q";
break;
case east:
country = "R";
break;
case west:
country = "S";
break;
default:
country = "";
break;
}
// switch condition of integral type
switch(c){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << "Vowel";
break;
default:
cout << "Not a Vowel";
break;
}
UD u;
// switch condition of user defined type (conversion to integral type)
switch(u){
case 0:
case 1:
cout << "Good";
break;
default:
cout << "Not so good";
break;
}
}
Here is a fairly typical use case. You have a list of values (the enum) and a switch which checks the input to determine which you are dealing with. This assumes of course that the action you will take depends on the underlying value of the enum.
enum ImageFormat
{
FormatRGB888,
FormatRGB8888,
FormatRGB101010,
FormatRGB161616,
FormatRGB16161616
};
void SomeFunc(ImageFormat format)
{
switch(format)
{
case FormatRGB888:
// do stuff
break;
case FormatRGB8888:
// do stuff
break;
case FormatRGB101010,
// do stuff
break;
case FormatRGB161616:
// do stuff
break;
case FormatRGB16161616:
// do stuff
break;
default:
// bad value for 'format'
}
}
Say you have an enum
enum expr_type {
EXPR_TYPE_ADD,
EXPR_TYPE_SUBTRACT,
EXPR_TYPE_GET_VALUE
};
We can do a switch on this:
enum expr_type t = /* get input somehow and find the type */;
switch(t) {
case EXPR_TYPE_ADD:
cout << "Operator Add";
/* fall through */
case EXPR_TYPE_SUBTRACT:
cout << "Operator (Add or Subtract)";
break;
case EXPR_TYPE_GET_VALUE;
cout << "Getting some value";
break;
}
You have to put in the break; so it doesn't fallthrough - Currently, EXPR_TYPE_ADD will exute all the code for EXPR_TYPE_SUBTRACT. Make sure to use break correctly!
Switch statements are a more efficient way of doing a lot of ifs and elses.
import java.util.Scanner;
class Date{
public static void main(String[] args) {
String dow;
String wowby;
String yowby;
Double n1,n2,res;
Scanner scan = new Scanner (System.in);
System.out.print("Enter Date (dd/mm/yy): ");
String date = scan.nextLine();
String dd = date.substring(0,2);
String mm = date.substring(3,5);
String yy = date.substring(6,8);
int d = Integer.valueOf(dd);
int m = Integer.valueOf(mm);
int y = Integer.valueOf(yy);
boolean valid = (d>=1) && (d<31)||(m>=1) && (m<12);//||((y>=00) && (y<99));
if(!valid)
System.out.print("Invalid date");
else {
switch (dd)
{
case "01":
System.out.print("First of ");
switch (mm) {
case "01":
System.out.print("January,2020");
break;
case "02":
System.out.print("February,2020");
break;
case "03":
System.out.print("March,2020");
break;
case "04":
System.out.print("April,2020");
break;
case "05":
System.out.print("May,2020");
break;
case "06":
System.out.print("June,2020");
break;
case "07":
System.out.print("July,2020");
break;
case "08":
System.out.print("August,2020");
break;
case "09":
System.out.print("September,2020");
break;
case "10":
System.out.print("October,2020");
break;
case "11":
System.out.print("November,2020");
break;
case "12":
System.out.print("December,2020");
break;
default:
System.out.print(" Invalid date ");
}
break;
case "02":
System.out.print("Second of ");
switch (mm)
{
case "01":
System.out.print("January,2020");
break;
case "02":
System.out.print("February,2020");
break;
case "03":
System.out.print("March,2020");
break;
case "04":
System.out.print("April,2020");
break;
case "05":
System.out.print("May,2020");
break;
case "06":
System.out.print("June,2020");
break;
case "07":
System.out.print("July,2020");
break;
case "08":
System.out.print("August,2020");
break;
case "09":
System.out.print("September,2020");
break;
case "10":
System.out.print("October,2020");
break;
case "11":
System.out.print("November,2020");
break;
case "12":
System.out.print("December,2020");
break;
default:
System.out.print(" Invalid month ");
}
break;
case "03":
System.out.print("Third of ");
switch (mm)
{
case "01":
System.out.print("January,2020");
break;
case "02":
System.out.print("February,2020");
break;
case "03":
System.out.print("March,2020");
break;
case "04":
System.out.print("April,2020");
break;
case "05":
System.out.print("May,2020");
break;
case "06":
System.out.print("June,2020");
break;
case "07":
System.out.print("July,2020");
break;
case "08":
System.out.print("August,2020");
break;
case "09":
System.out.print("September,2020");
break;
case "10":
System.out.print("October,2020");
break;
case "11":
System.out.print("November,2020");
break;
case "12":
System.out.print("December,2020");
break;
default:
System.out.print(" Invalid month ");
}
return;
}
}
}
}