If in java filed value is coming like 1123456#lopoa format.
So My question I am trying to take the value before #.
So according to java what to do to implement this logic?
You should use split(String) method of String class.
Following is working code:
public static void main (String[] args)
{
String val = "1123456#lopoa";
String[] vals = val.split("#");
System.out.println(vals[0]);
}
Output:
1123456
See it working here
Related
I was recently reading a java class where the person had created methods via IDE shortcut( extract method shortcut ). All methods had a return type of void and what was supposed to be return type was passed as input parameter for the method. Below is an example of what i'm referring to.
public void multiply(Integer first, Integer second, Integer result){
result = first * second;
}
I was wondering if the above is a good way of doing things. I personally do not like the above way of creating methods. I think the method should have been like below.
public Integer multiply(Integer first, Integer second, Integer result){
return first * second;
}
But in IntelliJ/Eclipse when we do extract method mostly creates method like above.
Can someone please let me know why IDE's usually create void method when we use the extract method shortcut and is the 1st style of method definition a good practice at all?
If the method being called isn't being assigned to a variable, Eclipse has no way to know what the return value is supposed to be.
Presumably, the original code looked something like this:
public static void main(String args[]){
Integer result = 0;
multiply(1,3,result);
}
There's no way for Eclipse to divine that multiply is supposed to return anything, so it defaults to void. If you want to infer return values, have it be assigned to a variable like so:
public static void main(String args[]){
Integer result = 0;
result = multiply(1,3,result);
}
I tried looking around for this before btw
When attempting to covert strings like "0.00000911" using any of the methods ive seen or used before keep giving me the value of 9.11-E
keep giving me the value of 9.11-E
No it doesn't. That's not a value. That's a representation. It is just what happens when you turn it back into a String via whatever code you're using to do that.
If you want to display it without scientific notation, see java.text.DecimalFormat.
If you just want to use double-precision arithmetic, you're already doing it.
Use BigDecimal instead of double.
Try this:
import java.math.*;
public class MyClass {
public static void main(String args[]) {
String a = "0.00000911";
BigDecimal b = new BigDecimal(a);
System.out.println("b = " + b);
}
}
I am trying to send my binary string to checkSequence, but when i display that string object, all I get is blank. Any ideas?
public class binaryGame
{
public static void main( String[] args )
{
String binaryString = new String(); // creating object I want to send
binaryString.equals(createString());
checkSequence(binaryString);
}
private static String checkSequence(String binaryString)
{
//things
}
right now, the program compiles, but the "String binaryString" is empty. I think it is just creating a new binaryString String, but not passing the object. thanks in advance.
I can't see where you initialized your string.
String.equals doesn't affect string object, it only compare them. So you need to do something like this:
binaryString = createString();
I have a question, i have a code like below:
controller.start(c.class, 1);
but i want to set "c" from console. I can get/set it from args on main method . but how can i put it on c.class ? I mean how can i do that?
String a = "c";
controller.start(a.class,1);
Of course it doesnt work , but i hope i can tell u about my problem
On php we can use $$string to set/get string to variable, but i dont know how can we do it on Java ?
More commonly used (and more secure) way of addressing this is using maps:
private static final Map<String, Class<?>> NAME_TO_CLASS = new Map<>();
static {
NAME_TO_CLASS.put("c", c.class);
...
}
static void main(String[] args) {
...
controller.start(NAME_TO_CLASS.get(args[0]), 1);
}
Of course in real life you'd want to check if argument is correct and is in the map NAME_TO_CLASS.contains(your_arg);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dynamic Variable Names in Java:
Let's say I have a string, as below.
String s = "Hello";
Now, I want to create a string, but the string's variable will be called "Hello". In order to make the string's name "Hello", I must access string s to get the name "Hello", so I can use it as a variable name. Below is what I want to see.
String Hello = "I want to do this, But from Accessing String s So I KNOW that String s = Hello";
Thank you for the effort, and please try to explain to me because I am a Java beginner. :D
What you are attempting to do is add a layer of indirection. You cannot access variables dynamically in a static language such as Java/C/C++/Pascal/etc
What you can do is emulate the dyamic context that dynamic languages use by, eg creating a Map to hold the variable names and values in this case you would have
Map<String,String> stringVars = new HashMap<String,String>();
// set a "variable"
stringVars.put("Hello", "value");
// get a "variable"
System.out.println(stringVars.get("Hello"));
Using Reflection (not recommended):
public class MainClass
{
public String Hello = "I want to do this, But from Accessing String s So I KNOW that String s = Hello";
public static void main(String[] args) throws Exception
{
MainClass m = new MainClass();
String s = "Hello";
String result = (String) MainClass.class.getField(s).get(m);
System.out.println(result);
}
}
OUTPUT:
I want to do this, But from Accessing String s So I KNOW that String s = Hello
Instead, use a map as others illustrated.
It is not possible in java.
The only thing you can do is to use A map interface' implementation, for example a HashMap. Using a put method you can 'assign' a value to a given 'name'. The name would be a key and has to be unique within a map, just like variable has a unique name within it's scope.
To retrieve the value, call get method passing appropriate key (ex. string 'Hello') as an argument.
In Java you cannot obtain the variable(reference) name which is pointing to an object since the object has no knowledge of the reference to it and there can be more than one variable refering to the same object.
On the other hand you could do somtething like this; but I do not know how would help:
String s = "Hello";
Reference<String> hello = new SoftReference<String>(s);
String myStringAccessedWithHello = hello.get();
Could you use a Map which takes a string (such as "Hello") and maps it to another String (such as "I want to do this, But from Accessing String s So I KNOW that String s = Hello"). Something like the following:
Map<String, String> stringMap = new HashMap<String, String>();
stringMap.put("Hello", "I want to do this, But from Accessing String s So I KNOW that String s = Hello");
Then you can find all of the key values in the map (such as "Hello") by calling:
Set<String> stringKeys = stringMap.keySet();
and you can lookup the long string belonging to the key "Hello" like this:
String longValue = stringMap.get("Hello");
This is how I would use a simple String value to find an unwieldy String value. Bear in mind you could also use a Map which maps String values to any other type of object.