How do I call a parseInt method in Main? - parseint

I'm currently trying to call 2 parseInt methods from main. One converts characters in a char[] to ints and the other just converts String to int.
But I cant figure out the syntax to have main call them.
I tried parseInt(String conString2); and parseInt(char[] conArray);
I couldnt find anything on google about this either.
public class parseInt
{
public static void main(String[] args)
{
parseInt(String);
parseInt(char[] conArray);
}
public static void parseInt(char[] conArray)
{
System.out.println("Original Charaters 1, 2, 3");
char[] conArrray = {1, 2, 3};
int conInt = Integer.parseInt(new String(conArray));
System.out.println("New ints " + conInt);
}
public static void parseInt(String conString2)
{
String conString = "10";
System.out.println("this number will be converted to int from string " + conString );
int conInt2 = Integer.parseInt(conString);
System.out.println(conInt2);
}
}
Im expecting for it to run those methods but instead it gives the errors "cannot be resolved to variable" on conArray and String. I get "Syntax error, insert ". class" to complete " on the char[].

Related

invoking a public function that gives access to a private member(list of strings)

I set the access modifier of a list containing strings to private and introduced a public function that gives access to it(return type void). I got an error message while trying to invoke the method in the main function.
code is as:
class carCompany{
string name;
string owner;
int carsinStock;
list<string> bestsellingCars;
list<string> expensiveCars;
public:
carCompany(string Name,string Owner){//constructor to avoid redundancy when creating objects
Name = name;
Owner = owner;
int carsinStock =0 ;
};
void info(){
cout<<"Autocompany name: "<<name<<endl;
cout<<"Company Owner: "<<owner <<endl;
cout<<"Total cars available: "<<carsinStock<<endl;
cout<<"best selling cars: " <<endl;
for(string bestsellingCars: bestsellingCars){
cout<<bestsellingCars<<endl;
};
cout<<"Expensive cars : "<<endl;
for(string expensiveCars: expensiveCars){
cout<<expensiveCars <<endl;
};}
void importCars() {
carsinStock++;
};
void exportCars()
{carsinStock--;};
void bestsellingcars(string cars){
bestsellingCars.push_back(cars);}
void expensivecars(string cars){
expensiveCars.push_back(cars);}
};
Invoking the function in the main function:
int main(){
carCompany autocomp("Tesla", "Elon Musk");
autocomp.bestsellingcars.push_back("Tesla i");
autocomp.expensivecars.push_back("Tesla ins");
autocomp.info();
carCompany rashidMotors("Rashid motors","Rashid Abu Bakar");
rashidMotors.bestsellingcars.push_back("Porche");
rashidMotors.expensivecars.push_back("Porche carrera");
rashidMotors.info();
return 0;
}
The Error message:
invalid use of member'void carCompany::bestsellingcars(std::string)' (did you forget the'&'?)
invalid use of member'void carCompany::expensivecars(std::string)' (did you forget the'&'?)
invalid use of member'void carCompany::bestsellingcars(std::string)' (did you forget the'&'?)
invalid use of member'void carCompany::expensivecars(std::string)' (did you forget the'&'?)
I tried to implement the suggestion provided by the error method but the error was still persistent.
Call it like this:
rashidMotors.bestsellingcars("Porche");

Java SE 11 String Final Variable with Ternary Operator Does Not Count as a Constant Variable in Switch Case Expression

I encountered a problem that the following code doesn't work. I ran the code in Java SE 11 (11.0.8), Eclipse 2020-06, Windows 10.
Use String Final Variable with Ternary Operator: Doesn't work
public class Tester {
public static void main(String[] args) {
String switchVar = "abc";
final String caseStr = true ? "abc" : "def";
switch (switchVar) {
case caseStr: System.out.println("Doesn't work");
}
}
}
It has a compile time error: java.lang.Error: Unresolved compilation problem: case expressions must be constant expressions.
However, according to JLS §4.12.4 and JLS §15.28, the String type can be a final variable and ternary operator can also be counted as constant expression.
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression.
A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
...
The ternary conditional operator ? :
Simple names that refer to constant variables
I did some more tests which showed either one of these points works if not combined together.
Directly use constant expression as case constant: No Problem
public class Tester {
public static void main(String[] args) {
String switchVar = "abc";
switch (switchVar) {
case true ? "abc" : "def": System.out.println("works");
}
}
}
Use String constant variable without ternary operator: No Problem
public class Tester {
public static void main(String[] args) {
String switchVar = "abc";
final String VAR_A = "a";
final String VAR_BC = "bc";
final String CASE = VAR_A + VAR_BC;
switch (switchVar) {
case CASE : System.out.println("works");
}
}
}
Use int with ternary operator instead of String: No Problem
public class Tester {
public static void main(String[] args) {
int switchVar = 10;
final int CASE = 3 > 2 ? 10 : 0;
switch (switchVar) {
case CASE : System.out.println("works");
}
}
}
Could anyone help me please?
With kindly help of others, it is sure now this is a bug of eclipse.
I have reported the bug to eclipse. (Bugzilla – Bug 566332)

"Int should be Void -> Int" when comparing two integers

So this is a new one for me. When I try to compare 2 integers, the error tells me that Int should be Void -> Int, which is something I have never even seen before.
The code:
public static function whenTouchEnds(event:TouchEvent){
for (item in currentTouches){
if (item.getId == event.touchPointID){
currentTouches.remove(item);
trace("removed touch");
break;
}
}
}
Following the Haxe documentation, I also tried:
public static function whenTouchEnds(event:TouchEvent){
for (item in currentTouches){
if (item.getId == event.touchPointID) break;
}
}
And for the sake of trail and error (hobby programmer here) even tried:
public static function whenTouchEnds(event:TouchEvent){
for (item in currentTouches){
var itemID:Int = item.getId;
var touchID:Int = event.touchPointID;
if (itemID == touchID){
currentTouches.remove(item);
trace("removed touch");
break;
}
}
}
They all gave me the same error message "Int should be Void -> Int". Here is the Touch class I created which returns an Integer with the getId function:
class Touch
{
public var id:Int = 0;
public var xPos:Int = 0;
public var yPos:Int = 0;
public function new(Id:Int, X:Int, Y:Int)
{
id = Id;
xPos = X;
yPos = Y;
}
public function getX() : Int
{
return (xPos);
}
public function getY() : Int
{
return (yPos);
}
public function getId() : Int
{
return (id);
}
}
I'm not looking for a simple solution, but rather an explanation of what I am missing here. The more I learn, the better!
Cheers
The culprit is this line:
if (item.getId == event.touchPointID)
Since there's no parentheses, you're not actually calling the getId() function here - you're comparing it to an integer (which doesn't make sense). Try this instead:
if (item.getId() == event.touchPointID)
Void -> Int is Haxe's notation for a function type, specifically a function that takes no parameters (Void) and returns an integer. You're comparing such a function to an Int, hence the error message "Int should be Void -> Int".
A small code style critique: the get* functions in your Touch class don't really seem to serve any purpose, the variables are public anyway. If you ever want to do something more complex than just returning the variable in a getter function, you might want to look into using properties instead.

Special character escaping

All groovy special character #{\'}${"}/', needs to be replaced by \ in front in a groovy string dynamically
input : anish$spe{cial
output : anish\$spe\{cial
input : anish}stack{overflow'
output : anish\}stack\{overflow\'
I have written a sample program in Java, that i want in groovier way
import java.util.regex.*;
import java.io.*;
/**
*
* #author anish
*
*/
public class EscapeSpecialChar {
public static void main(String[] args) throws IOException {
inputString();
}
private static void inputString() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter string to find special characters: ");
String string = in.readLine();
// Escape the pattern
string = escapeRE(string);
System.out.println("output: -- " + string);
}
// Returns a pattern where all punctuation characters are escaped.
static Pattern escaper = Pattern.compile("([^a-zA-z0-9])");
public static String escapeRE(String str) {
return escaper.matcher(str).replaceAll("\\\\$1");
}
}
Enter string to find special characters: $Anish(Stack%1231+#$124{}
output: -- \$Anish\(Stack\%1231\+\#\$124\{\}
This does what your Java code does:
System.console().with {
def inStr = readLine 'Enter string to find special characters: '
def outStr = inStr.replaceAll( /([^a-zA-Z0-9])/, '\\\\$1' )
println "Output: $outStr"
}
I am still dubious that what I think you are doing is a good idea though... ;-)

Can extension methods modify extended class values?

I was just trying to code the following extension method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4Testing
{
static class ExtensionMethods
{
public static void AssignMe(this int me, int value)
{
me = value;
}
}
}
But it is not working, i mean, can I use an extension method to alter values from extended classes? I don't want to change void return type to int, just changing extended class value. Thanks in advance
Your example uses int, which is a value type. Classes are reference types and behaves a bit differently in this case.
While you could make a method that takes another reference like AssignMe(this MyClass me, MyClass other), the method would work on a copy of the reference, so if you assign other to me it would only affect the local copy of the reference.
Also, keep in mind that extension methods are just static methods in disguise. I.e. they can only access public members of the extended types.
public sealed class Foo {
public int PublicValue;
private int PrivateValue;
}
public static class FooExtensions {
public static void Bar(this Foo f) {
f.PublicValue = 42;
// Doesn't compile as the extension method doesn't have access to Foo's internals
f.PrivateValue = 42;
}
}
// a work around for extension to a wrapping reference type is following ....
using System;
static class Program
{
static void Main(string[] args)
{
var me = new Integer { value = 5 };
int y = 2;
me.AssignMe(y);
Console.WriteLine(me); // prints 2
Console.ReadLine();
}
public static void AssignMe(this Integer me, int value)
{
me.value = value;
}
}
class Integer
{
public int value { get; set; }
public Integer()
{
value = 0;
}
public override string ToString()
{
return value.ToString();
}
}
Ramon what you really need is a ref modifier on the first (i.e. int me ) parameter of the extension method, but C# does not allow ref modifier on parameters having 'this' modifiers.
[Update]
No workaround should be possible for your particular case of an extension method for a value type. Here is the "reductio ad absurdum" that you are asking for if you are allowed to do what you want to do; consider the C# statement:
5.AssignMe(10);
... now what on earth do you think its suppose to do ? Are you trying to assign 10 to 5 ??
Operator overloading cannot help you either.
This is an old post but I ran into a similar problem trying to implement an extender for the String class.
My original code was this:
public static void Revert(this string s)
{
char[] xc = s.ToCharArray();
s = new string(xc.Reverse());
}
By using the new keyword I am creating a new object and since s is not passed by reference it will not be modified.
I changed it to the following which provides a solution to Ramon's problem:
public static string Reverse(this string s)
{
char[] xc = s.ToCharArray();
Array.Reverse(xc);
return new string(xc);
}
In which case the calling code will be:
s = s.Reverse();
To manipulate integers you can do something like:
public static int Increment(this int i)
{
return i++;
}
i = i.Increment();

Resources