Error: cannot find symbol -- EC=Character.parseChar(JOptionPane.showInputDialog("enter employee code")); - symbols

My professor said that we need to input a letter to the dialog box in order for regular, probation etc. to show. I don't know how to resolve this problem. Is there something wrong? I'm new to java.
Here's my code:
import javax.swing.*;
public class EmployeeCode_input
{
public static void main (String[]args)
{
char EC;
EC=Character.parseChar(JOptionPane.showInputDialog("enter employee code"));
if ((EC=="R")&&(EC=="r"))
System.out.println("Regular");
else if((EC=="P")&&(EC=="p"))
System.out.println("Probationing");
else if((EC=="T")&&(EC=="t"))
System.out.println("Trainee");
else if ((EC=="C")&&(EC=="c"))
System.out.println("Contractual");
else
System.out.println("INVALID");
}
}

Character.parseChar is not available in Java.

As chinmay ghag pointed out there is no Character.parseChar in Java. You may use String's charAt() method instead.
Change :
EC=Character.parseChar(JOptionPane.showInputDialog("enter employee code"));
To :
EC=JOptionPane.showInputDialog("enter employee code").charAt(0);
See this for more information: How to convert/parse from String to char in java?
Also if EC is of type char then you shouldn't be comparing char's with strings. ie: EC = 'p' not EC = "p"

Related

How to represent a C char array in Java with GraalVm

I'm consuming a C library in Java with GraalVm and I have this field (if_name) I don't know how to implement:
# define IFNAMSIZ 16
struct port_t
{
char if_name[IFNAMSIZ];
}
This is my current code:
#CStruct("port_t")
interface Port extends PointerBase {
#CField("if_name")
WordPointer getIfName();
#CField("if_name")
void setIfName(WordPointer ifName);
}
Using WordPointer I get the following error compiling:
Error: Type WordPointer has a size of 8 bytes, but accessed C value has a size of 16 bytes; to suppress this error, use the annotation #AllowNarrowingCast
I already tried with CCharPointer and CCharPointerPointer but those have fixed lengths to 8 bytes and I got a similar error when compiling.
Anyone can help?
Thanks in advance
As you already guessed, you'll need to get the address of this field to manipulate it.
You can use
#CStruct("port_t")
interface Port extends PointerBase {
#CFieldAddress("if_name")
CCharPointer addressOfIfName();
}
In this case it doesn't make sense to have a setter since you cannot change the address of this field. Instead you can use the read/write methods of CCharPointer.
You'll probably need
#CConstant
static native int IFNAMSIZ();
somewhere as well.
Then you can do things like
String foo(Port p, String newIfName) {
UnsignedWord size = WordFactory.unsigned(IFNAMSIZ());
String oldIfName = CTypeConversion.toJavaString(p.addressOfIfName(), size);
CTypeConversion.toCString(newIfName, p.addressOfIfName(), size);
return oldIfName;
}

Solidity - ParserError: Expected primary expression when using var

I am trying to create a struct and add mapping in such a way that it can be retrieved later on using its address using the below code.
pragma solidity ^0.8.0;
contract Courses {
struct Instructor {
uint age;
string fName;
string lName;
}
mapping (address => Instructor) instructors;
address[] public instructorAccts;
function setInstructor(address _address, uint _age, string _fName, string _lName) public {
var instructor = instructors[_address]; //ERROR HERE
instructor.age = _age;
instructor.fName = _fName;
instructor.lName = _lName;
instructorAccts.push(_address) -1;
}
}
However, I am getting an error at the line var instructor = instructors[_address]
The error is ParserError: Expected primary expression
I am unable to understand what the issue is and how to resolve it. Could anyone help with this?
Solidity uses typed variables, doesn't have the generic var keyword (that is used in JavaScript for example).
When declaring reference types (in your case string and struct), you need to specify their data location - in your case memory for the string argument. And either memory or storage for the struct depending on whether you want to set the values just in the context of the setInstructor() function (location memory), or if you want to set its values in the contract storage (location storage).
function setInstructor(address _address, uint _age, string memory _fName, string memory _lName) public {
// either `memory` or `storage` depending on your use case
Instructor storage instructor = instructors[_address];
// ... rest of your code
There also is a syntax error on this line
instructorAccts.push(_address) -1;
you can fix it by removing the -1
instructorAccts.push(_address);

From a String to a Type usable as a type argument

I'm creating a Money class in Dart and came up with the idea of leveraging the type system to make sure you can't subtract Swiss Francs from Dollars (as opposed to this). This works swimmingly, significantly abbreviated what I have looks as follows:
abstract class Currency {
const Currency(this.precision, this.code);
final int precision;
final String code;
}
class Chf extends Currency {
const Chf() : super(2, 'CHF');
}
class Usd extends Currency {
const Usd() : super(2, 'USD');
}
class Money<T extends Currency> {
Money<T> subtract(Money<T> other) { ... }
}
It is impossible to subtract USDs from CHFs. Great. (More complete code here.)
But I'm receiving JSON payloads with currency representations therein. I now need to go from a String 'CHF' to a Money<Chf> instance. I can't figure out how to do that.
Somewhere I need to map a (currency code) string to a Type.
final t = convertStringToType('CHF');
final m1 = Money<t>(100); // <- I can't do this: 't' isn't a type.'
The only option forward I see is having a large (colossal) switch/case statement:
switch(code) {
case 'CHF':
return Money<Chf>(100);
...
}
Clearly I don't want that. Is there a better way?
Type arguments cannot be created from scratch, they have to be actual types or other type variables. So, if you need <Chf> as a type argument, you need to have <Chf> as an actual type argument somewhere in your program. Then you can piggy-back on that.
I would do something like:
T callWithCurrency<T>(String name, T action<X extends Currency>()) {
if (name == "CHF") return action<Chf>();
if (name == "USD") return action<Usd>();
...
throw ArgumentError.value(name, "name", "Not a recognized currency");
}
and then you can use it as:
final Money m1 = callWithCurrency("CHF", <X extends Currency>() => Money<X>(100));

`nameof` operator in flutter

There is nameof operator in C#, it allows to get property name at compile time:
var name = nameof(User.email);
Console.WriteLine(name);
//Prints: email
It is not possible to use reflection in flutter and I do not want to hardcode names of properties i.e. to be used for querying SQLite tables. Is there any workaround?
***Currently I'm using built_value library.
For the archives, I guess, this isn't possible as Dart doesn't store the names of variables after compiling, and as you mentioned, Flutter doesn't support reflection.
But you can still hardcode responsibly by grouping your properties as part of the object that they belong to, like with JSON:
class User {
final String email;
final String name;
const User({required this.email, required this.name});
Map toJson() => {
"email": email,
"name": name,
};
}
Instead of remembering to type out "email" and "name" whenever you use User, just call User.toJson(). Then, when you want to rename your variables, you can use your IDE's "rename all", or just skim over your User class to quickly change all of the names without missing any.
I'm currently monitoring the progress on the dart:mirrors package, which offers some neat reflective properties and methods, though, I hadn't found a simple way to just get the name of a symbol like nameof() does.
Example:
import 'dart:mirrors';
class User {
final String email;
final String name;
const User({required this.email, required this.name});
}
void main() {
reflectClass(User).declarations.forEach((key, value) {
print(value.simpleName);
});
}
Output:
Symbol("email")
Symbol("name")
Symbol("User")
These are of type Symbol.
More here: https://api.dart.dev/stable/2.4.0/dart-mirrors/dart-mirrors-library.html
So, until they develop a nameof, I've created an extension on symbol:
extension SymbolExtensions on Symbol {
String get nameof =>
RegExp(r'"(.*?)"').firstMatch(toString())!.group(1).toString();
}
So, you could do:
print(reflectClass(User)
.declarations[#email)]!
.simpleName
.nameof);
Output:
email
It's a start. Dart is still growing.
You can use code generation.
The basic idea is to create a nameof annotation class and mark parts of your code with it. You also need to create a code generator that handles your annotated code. Look at the json_serializable package for an example and create your own code generator.
If you do not want to create your own generator, use a ready-made package nameof: https://pub.dev/packages/nameof
Short how-to with this package.
Mark your class with nameof annotation.
#nameof
class Car {
final double price;
final double weigth;
final int year;
final String model;
Car(this.price, this.weigth, this.year, this.model);
Car.sedan(double price, double weigth, int year)
: this(price, weigth, year, 'Sedan');
}
Run the code generator.
flutter pub run build_runner build
Then use the generated class, which will look something like this.
/// Container for names of elements belonging to the [Car] class
abstract class NameofCar {
static const String className = 'Car';
static const String constructor = '';
static const String constructorSedan = 'sedan';
static const String fieldPrice = 'price';
static const String fieldWeigth = 'weigth';
static const String fieldYear = 'year';
static const String fieldModel = 'model';
}
You can implement your own nameOf function:
String? nameOf(dynamic o) {
if (o == null) return "null";
try {
if (o is List) {
var first = o.length > 0 ? o[0] : null;
if (first != null) {
var elementType = nameOf(first)!;
Log.debug("nameOf: List<$elementType>");
if (!isMinified(elementType))
return "List<$elementType>";
}
} else {
Function? getTypeName = o.getTypeName;
if (getTypeName != null) return getTypeName();
}
} catch (e) {
Log.debug("ignored nameOf error: $e, falling back to o.runtimeType: ${o.runtimeType}");
}
return o.runtimeType.toString();
}
bool isMinified(String type) => type.startsWith("minified:");

What is the term for the '#' in Razor syntax?

The material I've read generally uses phrases like 'use the # character here to denote the start...' but I want to know what to call such a character. A coworker suggested the word 'token' but I don't think that's correct.
Brifely looking at the source code, the Razor team seem to refer to it as Transition Symbol.
In SyntaxConstants:
namespace System.Web.Razor.Parser
{
public static class SyntaxConstants
{
public static readonly char TransitionCharacter = '#';
public static readonly string TransitionString = "#";
}
}
Also in HtmlSymbolType.Transition:
namespace System.Web.Razor.Tokenizer.Symbols
{
public enum HtmlSymbolType
{
...
Transition, // #
}
}
Still, I doubt you can formally name it "Transition", it's seems more like an internal term of the parser to denote contexts switches (for example from HTML to C# and vice versa).

Resources