So I have a set of different strings made if a certain key is pressed using keyPressed(), but some of those strings i want to convert to characters and tried doing so like this:
char keyChar = keyChanged.charAt(0);
except now i get a nullpointerexpression. if it matters, keyChanged would be a 1 letter string like "r".
You can avoid a NullPointerException like this:
if (keyChanged != null){
char keyChar = keyChanged.charAt(0);
}
Solved it, It was pretty stupid on my part, I just set the original keyChanged variable as a character instead.
Related
I have a menu driven program where the user is prompted to enter as may integers as they would like in order to build a binary search tree--I have just started and am stuck getting out of reading their integers once they hit "Q"
switch(inputOption){
case 1:
System.out.println("You've selected to create a new binary tree." + "\n");
Scanner scan = new Scanner(System.in);
String again;
String tempInput;
Boolean repeat = true;
try{
System.out.println("Please enter as many integers as you'd like, hit 'Q' when you are finished." + "\n");
do{
tempInput = scan.next();
if(tempInput != "Q"){
integerInput = Integer.parseInt(tempInput);
repeat = true;
}
else
repeat = false;
}while(repeat);
}catch(InputMismatchException e){}
Any ideas on how I can get it to recognize the 'Q'?
Use
if(!tempInput.equals("Q"))
rather than
if(tempInput != "Q")
Java strings don't work with the comparison operators.
Try adding (|| "q") could possibly help. You don't give much information though. For example have you dropped in with debugger and analyzed the actual value of tempInput to be sure it's actually "Q" ? If so then maybe try casting to character, or trimming any extra white spaces or special characters that it might contain.
More info would be better :-D
I need to convert source files from an old CAN-8 system to/from standard ASCII.
The CAN-8 files have each byte with the high bit on (0x80).
So I need to do something like:
f=File.new
can8=f.read
... do something with variable can8
When I display the can8 variable it looks like "\xC1\xC2\xC3", I need to convert that to "ABC" ("\x41\x42\x43")
Mike
Here is one way to do it:
original_string = "\xC1\xC2\xC3"
converted_string = original_string.bytes.collect { |b| (b & 0x7f).chr }.join
You didn't specify a Ruby version, so I will assume you are using 1.9 or later.
The following little script seems to do the job:
File.open("/etc/passwd").each_byte { |char|
print (char & 0x7F).chr()
}
The char & 0x7F turns off the high-bit, and chr() turns the number back into a character.
I have the following code which is supposed to be removing a particular email address from a string if it exists. The problem is i get the error "invalid range "y-d" in string transliteration (ArgumentError)" which I assume is because it's treating my input as a regex. I will need to do this delete by a variable in the actual code, not a string literal but this is a simplified version of the problem.
So how do I properly perform this operation?
myvar = "test1#my-domain.com test2#my-domain.com"
myvar = myvar.delete("test1#my-domain.com")
Try
myvar = "test1#my-domain.com test2#my-domain.com"
myvar = myvar.gsub("test1#my-domain.com", '').strip
String#delete(str) does not delete the literal string str but builds a set out of individual characters of str and deletes all occurrences of these characters. try this:
"sets".delete("test")
=> ""
"sets".delete("est")
=> ""
The hyphen has a special meaning, it defines a range of characters. String#delete("a-d") will delete all occurrences of a,b,c and d characters. Range boundary characters should be given in ascending order: you should write "a-d" but not "d-a".
In your original example, ruby tries to build a character range from y-d substring and fails.
Use String#gsub method instead.
You can do it like this
myvar = "test1#my-domain.com test2#my-domain.com"
remove = "test1#my-domain.com"
myvar.gsub!(remove, "")
I'm using windows 7 and Visual C++. I have a console program and I am trying to trim a string at the begining and the end. TrimLeft() and TrimRight() don't seem to work without MFC. Here is what I have so far.
pBrowser->get_LocationURL(&bstr);
wprintf(L" URL: %s\n\n", bstr);
SysFreeString(bstr);
std::wstring s;
s = bstr;
s.TrimStart("http://");
s.TrimEnd("/*");
wprintf(L" URL: %s\n\n", s);
I'm trying to go from this:
"http://www.stackoverflow.com/questions/ask"
to this:
"www.stackoverflow.com"
TrimStart/End usually return a value, so you would have to set 's' to equal the value of s.TrimStart() and s.TrimEnd() respectively.
try,
s = s.TrimStart("http://");
s = s.TrimEnd("/*");
You should use find/rfind(right find - find from right) and substr(sub string) in sequence to do what you need to do.
1) Find the index of the first pattern (such as http://) with find - you already know its length, add this to the start index as the origo of your trimmed string
2) Find the last index of the ending pattern with find
3) Create a substring from the origo to the end using substr
These methods are all in std::string
Problem:
If String ends with \r, remove \r
I started with something like this
if (masterValue.endsWith(CARRIAGE_RETURN_STR)) {
masterValue = masterValue.replace(CARRIAGE_RETURN_STR, "");
}
where
public static final String CARRIAGE_RETURN_STR = (Character.toString(Constants.CARRIAGE_RETURN));
public static final char CARRIAGE_RETURN = '\r';
This seems awkward to me.
Is there an easy way to just remove \r character?
I then moved on to this:
if (value.contains(CARRIAGE_RETURN_STR)) {
value = value.substring(0, value.length()-3);
//-3 because we start with 0 (1), line ends with \n (2) and we need to remove 1 char (3)
But this too seems awkward .
Can you suggest a easier, more elegant solution?
Regexes can support end-of-string anchoring, you know. (See this Javadoc page for more information)
myString.replaceAll("\\r$", "");
This also takes care of fixing \r\n --> \n, I believe.
I'd write it like this:
if (masterValue.endsWith("\r")) {
masterValue = masterValue.substring(0, masterValue.length() - 1);
}
I see no point in creating a named constant for the String "\r".
By the way, your second attempt is incorrect because:
String.contains("\r") tells you if the String contains a carriage return, not if it ends with a carriage return,
the second argument of String.substring(int, int) is the index of the end character; i.e. the position first character that should NOT be in the substring, and
the length of "\r" is one.