I have a java string, which has a variable length.
I need to put the piece "<br>" into the string, say each 10 characters.
For example this is my string:
`this is ...
|
I have two strings in a java program, which I want to mix in a certain way to form two new strings. To do this I have to pick up some ... |
Right now I'm using the following to minimize boxed object creation:
String myString = "" + myChar;
Is this the idiomatic way to do it? (IMHO it feels a little awkward.)
|
char c=7;
The above statement will execute in java without error even though we are assigning a number to character. where 7 is not a character .Why will it execute?
|
I'm probably missing out something, but why the two numeric values are equal to -1?
System.out.println(Character.getNumericValue(Character.MAX_VALUE));
System.out.println(Character.getNumericValue(Character.MIN_VALUE));
returns:
-1
-1
|
I am getting a string from the user and then doing some checking to make sure it is valid, here is the code I have been using;
char digit= userInput.charAt(0) - '0';
This ... |
Is there a way to turn a char into a String or a String with one letter into a char (like how you can turn an int into a double and ... |
|
I use java library Tidy to sanitize html-code. Some of the code contains links with Russian letters. For example
<a href="http://example.com/???????">link with Russian letters</a>
I understand that "???????" must be escaped, but I ... |
I got the same result from these, what is the difference? which is better?
temp is an int, read from reader.read()
System.out.print((char)temp);
System.out.print(Character.toChars(temp));
System.out.print(String.valueOf(Character.toChars(temp)));
|
I'm seeing both char and short are two bytes each,
also the following is valid:
char a = 'x';
int b = a;
long c = a;
However when I do short d = ... |
I'm making a method to read a whole class code and do some stuff with it.
What I want to do is get the name of the method, and make a String ... |
Given:
char x = '"';
char y = '\"';
Are x and y equal?
|
If I'm taking input with a scanner and I want to put a single character into a char type variable, what is the most efficient algorithm? I just noticed that both ... |
Apparently there is a method that takes a char and returns a char: http://download.oracle.com/javase/6/docs/api/java/lang/Character.html#toLowerCase(char)
But I can't seem to get it to work. My code:
import java.lang.Character;
public class Test {
...
|
I'm trying to use the char method isLetter(), which is supposed to return boolean value corresponding to whether the character is a letter. But when I call the method, I get ... |
I haven't found any information if it's even possible. I want to use method or some other way to return string and put it into for loop so that when loop ... |
I have java class which has a variable of type "char" now I have to conver that class on AS3 side . does any one know whats the java char equivalent ... |
import java.util.Scanner;
class Practice {
public static void main(String args[]) {
System.out.println("Please enter your test score: ");
Scanner enteredScore ...
|
A simple question that I don't know if Java has also a simple solution.
I have a string like: name.ext.ext.out.end
I need to get rid of .out, always the penultimate extension. ... |
Why are empty Strings valid and empty chars are not ?
I would have thought an empty String is not a string but just a placeholder. The same for a char, but ... |
I am trying to add the chars from a string in a textbox into my Stack,
here is my code so far:
String s = txtString.getText();
Stack myStack = new LinkedStack();
for (int ...
|
I'm trying to get the 5th, 6th and 7th digits from a list of digits.
E.g. I want to get the year out of the variable dateofbirth, and save it as a ... |
I'm trying to build a string in Java which will be at maximum 3 long and at minimum 1 long.
I'm building the string depending on the contents of a integer array ... |
Is it possible to reverse String in Java without using any of the temporary variables like String, Char[] or StringBuilder?
Only can use int, or int[].
|
My problem is that I cant make my string a valid one,
The valid way is this:
for example ---> "DP 3#4" this is a rule for a game, and i ... |
Is there any way to generate 8 characters long random and unique String in Java?
String id = getRandomString();
and then Id will be for example wf41Av5g
|
I am having trouble making a file, I think I have got the majority of it correct, but I'm having trouble getMorseCode() method. I am almost positive it begins with
String ...
|
In reference to the link: File IO Tuning, last section titled "Further Tuning" where the author suggests using char[] to avoid generating String objects for n lines in the ... |
|
|
|
In short: Because a char[] is not a subclass of a short[]. Casting primitives is different than casting references. When you're casting a primitive value, you're telling the compiler, "Yes, I know that I might be losing something by converting a value stored in one container type to another container type." When it comes to reference casting, it's not so much ... |
I am storing a flag in the database as type char, but it is set from a web page request object as a string. I need to convert it from type String to char in java. I tried String.charAt(0) which seems to convert to a char type untill I assign it to a variable of type char: char processed = processed_string.charAt(0); ... |
I am trying to build an array of hex integers as a "Large_Integer" class/object. I can't seem to move the values passed from the user to the array. How can I do this? package mathLib; public class Large_Integer implements Comparable { private static final long serialVersionUID = 1L; private static Large_Integer[] lrgInHex(String hexString) { Large_Integer[] lrgInt = new Large_Integer[hexString.length()]; // Using ... |
HI, Pls tell me how to remove or delete chars and include chars in a string and also i have to replace the spaces into some other thing.I know to use the "replace()" to replace chars. Consider i have a very big string with some 6/7 lines[450 chars].I have to include a text in the first three line.then remove all the ... |
Hi, a map is a good data structure, pairing each character to a count. You can do it like this using the Collections framework: import java.util.*; /*Count the occurences of characters in a string. **/ public class QuickTest { private static String data = "Hello"; public static void main(String[] args) { HashMap map = new HashMap(); char[] tempData = ... |
|
Hi can anybody help me? i have this piece of code and i get the error char cannot be dereferenced... im new at this.. this piece of code is in a method..and i have to count the total consonants in a word.. im trying to compare it to a string array of vowels. for (int outerCounter = (wordLength - 1); outerCounter ... |
Hi, You might want to change your design. Not many humans can input 13 numbers correctly without seeing them on the screen. If you hide the correct numbers and people make mistakes it will only take a few times and they will give up on your application. Most applications that require credit card numbers let the user see the result and ... |
hi abhinav, i am a new bie, i wrote this very simple program, when i compile this program on Dos then it is displaying the value of f and q as "a" why is this so...acc. to me it should have been displayed as NULL, when the same program i run in Kawa editor it is not showing anything ..why is ... |
Hi all, I want to change a char "firstnum" to a string. Here is my code. private static String firstNumber( String s ) { char firstnum = s.charAt( 0 ); String x = Character.toString( firstnum ); return x; } And I get the error message: cannot resolve symbol symbol: method toString (char) location: class java.lang.Character String x = Character.toString( firstnum ); ... |
|
public static char letter(boolean isUpper) { char[] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R' ,'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; double x = Math.random()*25; if (isUpper==true) return letters[(int)x]; else { char new_letter = letters[(int)x]; new_letter.toLowerCase('k'); return new_letter; } } The toLowerCse will not work. Char cannot ... |
|
|
Hi jacq, Take care not to confuse programming convenience with the process necessary to achieve a result. Unless a string is sequenced in some way, or has some special indexing mechanism designed to enable characters to be found faster, the ONLY way to find a particular character is to search. There may be special methods such as indexOf() in the String ... |
|
|
|
|
|
>> Welcome to JavaRanch Lara. Thanks! I hope I can give something in return soon. >> Umm, I don't see anything wrong with the code. I just compiled and ran this: Darn :-) >> And it works fine. Are you sure that you are using single quotes in the if statement Yuppers: char testSub = testId.charAt(2); if (testSub == 'R') { ... |
I'm trying to make a simple, 1-to-1 character conversion "Encryption" program. Here is the code: public class mono { public static void main (String args[]) { char[][] ct= { {'a','b'}, {'b','c'}, {'c','d'}, {'d','e'}, {'e','f'}, {'f','g'}, {'g','h'}, {'h','i'}, {'i','j'}, {'j','k'}, {'k','l'}, {'l','m'}, {'m','n'}, {'n','o'}, {'o','p'}, {'p','q'}, {'q','r'}, {'r','s'}, {'s','t'}, {'t','u'}, {'u','v'}, {'v','w'}, {'w','x'}, {'x','y'}, {'y','z'}, {'z','a'}, }; //ct[2][25] not ct[25][2] System.out.println("Type in ... |
|
Welcome to JavaRanch, liam! Which part of the problem you've described are you stuck on? Is it getting a character from a String? Is it just turning a char into an Object? Is it both? Note that for all of the Java primitive values, a wrapper type object can be created to hold the primitive value. In the case of a ... |
Originally posted by Billybob Marshall: No I think I have to disagree. Although they both have the exact same bit-wise value in memory, there are a few subtle differences. First, 0 has an int type and (char)0 has a char type. IIRC, there are some implicit conversions made between the two, they are still not the same type. This leads the ... |
I need to have the user enter A, B,C to decide their payment option and then I need to (in the same method) write code that says if the user selects a then the number of payments is 1 if b then number of payments 2 if c number of payments is 20 and pass that number to a method to ... |
PROBLEM: String s (of indeterminate length) is passed to the method searchString(), the user is prompted to enter a one character search argument name s1. Object of the exercise is to count the number of times s1 occurs within s, regardless of case h = H, H = h, as it were. CAN ANYONE FIGURE OUT WHERE I AM GOING WRONG. ... |
I have an array of strings that I need to query a specific char position of. Specifically, position 3 of an array of strings for a period. If the period shows up at a specific point in the string, its a type1 string and if the period is elsewhere its a type2 string. Is there anyway to grab the value of ... |
I suggest you try writing a tiny program that sets char c1='1' and char c2 = 1 and then does a Systen.out.println( "c1 = " + c1 + " c2 = " + c2 ) and let us know the results. [ July 22, 2004: Message edited by: Marilyn de Queiroz ] |
Java is a strongly-typed language. The charAt() method returns a char, which you are trying to assign to a String reference variable. You can do one of several things, including changing your variable type to that of char or using the String.valueOf(char c) method to convert the char from charAt() to a string. Hope this helps.... |
I have a number of char varable names i.e. char pos1; char pos2; char pos3; char pos4; char pos5; char pos6; char pos7; I thought the above was a little long winded so I decided to use a for loop and assign the varable names to char i.e. String pos [ ] = new String [7]; String charVar [ ] = ... |
Hi, Mannie - A String is a different thing in Java than it is in C. In C, a null terminated character array is a string; in Java, String is a full-fledged object. So, you can create a char array from the String object that you create when you put text between quotes, or you can change the method parameter to ... |
public static String valueOf(char[] data) Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string. Parameters: data - a char array. Returns: a newly allocated string representing the same sequence of characters contained in the character array argument public static String ... |
|
Strings are immutable. If you change one it really is creating a new one behind the scenes. String a = "123456789" ; int l = a.length(); int l2 = a.toCharArray().length ; System.out.println( "" + l + " " + l2 ) ; //9 9 a = "12345678" ; l = a.length(); l2 = a.toCharArray().length ; a = "12345678" ; System.out.println( "" ... |
|
|
|
Hi Trying to compare the first charater from SQL return with all the characters from the Alphabet. I have all the code apart from one problem. The alphabet characters are of type 'char' and the SQL returns of type 'String' (or .substring(0,1) to be exact) but when I try to compare the alphabet letter with the String ( if(char == string)... ... |
Hi, check out the code below. class chartest { public static void main(String[] args) { char c = '\u000d' ; // Line 5 char c = '\u000a'; // Line 6 char c = '\u0000'; // Line 7 char c = '\uface' ; // Line 8 char c = '\u00001' ; // Line 9 char c = '\101'; // Line 10 char ... |
Hi , Thanks for your answer. I agree that the char takes the last two bytes from the 2's compliment value of -10 . It could be some big value like 65535 in decimals9base 10).... Here I guess , it would do some mod operation on 65535 and then changes to some value less than 256 ,which can be printable. Am ... |
|
|
Originally posted by Henry Wong: Basically, when you are performing addition, and at least one of the two operands is a string. The compiler will convert the operation to ... create a stringbuffer, append the operands, and get the string result. It is the string buffer (or string builder class) that is overloaded to take a char, and does the conversions ... |
|
77. char coderanch.comHi Kunal, If you compile and run this, and it give you a compiler error, it GREATLY helps people figure out the problem if you post the EXACT text of that error message. It tells you all kinds of things, including where it thinks the error is (although it is sometimes wrong). Without that, people have to really study the code, ... |
|
|
80. Char coderanch.comThe result of any addition is never smaller than an int. Therefore, char + char == int. This means that x + y is not a char anymore but an int, and is therefore printed as an int. You will need to cast the result back to char first to see it as a char. |
Their was a requirment to validate the other language input (may be hindi or Tamil) in java. As we know localisation concepts which will take care of ONLY ocalizing the string based on the locale. However it has nothing to do with other language input. I was thing is there any started way to identifiy a unicode is char or a ... |
|
|
|
|
String pwd1 = "asdfgh"; // String literal String pwd2 = myObject.getPassword(); // Get it from elsewhere, e.g. user input or file // check password here pwd1 = null; pwd2 = null; // "asdfgh" still exists in String pool, but the former value of pwd2 is gone forever I guess if you were to do something dumb like hard-code a password into ... |
FileOutputStream f=new FileOutputStream("out.ser"); f.write(); Here, this write() method argument is of type int, so Unicode character value of '\n' i.e 10 is written to the file . but if i use ObjectOutputStream and use its writeChar(char) method instead of FileOutOutStream newline character is writene to the file . public void writeChar(){ char x = '\n'; FileOutputStream f=new FileOutputStream("out.txt"); f.write(x); //writes 10 ... |
Thanks for replys. But the point is that I'm receiving wrong values (I mean different than those provided by my teacher) My previous lesson was about "encryption", and after every lesson he is giving me different task, like write program in my language (which is going to be used during my school secondary school certificate - IT) For example for 'A' ... |
Need help with returning a character stored at a stated index position while input is converted to uppercase. Now I can easily convert string from dialog to uppercase , and return a character at an index position.. but not both together. Example: Input First Name: Bob Input Surname Name: Rudlo Output would be converted to BOB RUDLO Im trying to just ... |
I'm stuck, and having a bump in my program, here is a little snippet of the contructor just ot give you an idea. To Sum it up - I am running the letterCount to take in a string set by the client. It will take that string, and tally how many occurences of each letter occurs within that string inside an ... |
import javax.swing.JOptionPane; public class Question_1 { public static void main(String[] arg) { //Get the input String input = JOptionPane.showInputDialog("Enter a series of numbers: "); //Translate input and get rid of white space to the left and right of the string input = input.replaceAll("0"," -----"); input = input.replaceAll("1"," .----"); input = input.replaceAll("2"," ..---"); input = input.replaceAll("3"," ...--"); input = input.replaceAll("4"," ....-"); input ... |
By your statements above and elsewhere in the forum, you don't seem new to programming, just Java. Why not take a favorite program of yours written in whatever language you know, and re-write it from the ground up in Java. For e.g., any programmer worth their salt has attempted one of these: zooming the Mandelbrot set, displaying the game of life ... |
|
|
that helps a little bit but there are four chars that are the four card suits. im trying to put those in an array and then randomly pick one of them from that array. but i dont how to get those chars so i thought i would use a for loop to print out all of the chars then i could ... |
|
|
Originally Posted by sonny test code please, Here it is: Java Code: import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Random; public class CharacterCountTest { private static final int TRIALS_PER_RUN = 10; private Random rnd = new Random(); public void run(int samples, int charCount, double frequency) { // generate test data String[] testData = new String[samples]; for (int i = 0; i < samples; ... |
Say I have a String that says "this is a test". I need to write code to swap each set of 2 chars, so for example, in this case, the swapped String would be "htsii s aetts". The 't' and 'h' in 'this' swap, the 'i' and 's' swap, the space and 'i' in 'is' swap, so on so forth. Can ... |
public char charAt(int i) CharNode currentNode = _head; if(currentNode.getValue() == i) // if i equals value return currentNode.getData(); if(currentNode.getValue() > i) // if the first node's value is bigger than i, that just means that the same letter is used many times in a row... return currentNode.getData(); while(currentNode.getValue() < i){ // stops when the value is larger or equal to i ... |