Search the Whole World Here.,.,

How do I convert string to an integer or number?

How do I convert string to an integer or number?

package org.kodejava.example.lang;
public class StringToInteger {
public static void main(String[] args) {

// Some random selected number, could representing a decimal,
// hexadecimal or octal number.

String myLuckyNumber = "13";

// We convert a string to an integer by invoking parseInt() method
// of the Integer class.

Integer number = Integer.parseInt(myLuckyNumber);
System.out.println("My lucky number is: " + number);

// We can also converting a string representation of a number other
// then the decimal base, for instance an hexadecimal by providing
// the radix to the method.

number = Integer.parseInt(myLuckyNumber, 16);
System.out.println("My lucky number is: " + number);
number = Integer.parseInt(myLuckyNumber, 8);
System.out.println("My lucky number is: " + number);
}
}

Our code results are:

My lucky number is: 13
My lucky number is: 19
My lucky number is: 11


Source : https://kodejava.org/how-do-i-convert-string-to-an-integer-or-number/
How do I reverse a string?

How do I reverse a string?

Below is an example code that reverse a string. In this example we useStringBuffer.reverse() method to reverse a string. In Java 1.5, a new class calledStringBuilder also has a reverse() method that do just the same, one of the difference is StringBuffer class is synchronized while StringBuilder class is not.
And here is the string reverse in the StringBuffer way.
package org.kodejava.example.lang;

public class StringReverseExample {
    public static void main(String[] args) {
        // The normal sentence that is going to be reversed.
        String words =
                "Morning of The World - The Last Paradise on Earth";

        // To reverse the string we can use the reverse() method in
        // the StringBuffer class. The reverse() method returns a
        // StringBuffer so we need to call the toString() method to
        // get a string object.
        String reverse = new StringBuffer(words).reverse().toString();

        // Print the normal string
        System.out.println("Normal : " + words);
        // Print the string in reversed order
        System.out.println("Reverse: " + reverse);
    }
}
Beside using this simple method you can try to reverse a string by converting it to character array and then reverse the array order.
And below is the result of the code snippet above.
Normal : Morning of The World - The Last Paradise on Earth
Reverse: htraE no esidaraP tsaL ehT - dlroW ehT fo gninroM

Source : https://kodejava.org/how-do-i-reverse-a-string/

How do I iterate each characters of a string?

How do I iterate each characters of a string?

package org.kodejava.example.text; import java.text.CharacterIterator; import java.text.StringCharacterIterator; public class StringCharacterIteratorExample { private static final String text = "The quick brown fox jumps over the lazy dog"; public static void main(String[] args) { CharacterIterator it = new StringCharacterIterator(text); int vowels = 0; int consonants = 0; // Iterates charater sets from the beginning to the last character for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels = vowels + 1; } else if (ch != ' ') { consonants = consonants + 1; } } System.out.println("Number of vowels: " + vowels); System.out.println("Number of consonants: " + consonants); } }




Source :https://kodejava.org/how-do-i-iterate-each-characters-of-a-string/
How do I add leading zeros to a number?

How do I add leading zeros to a number?

This example shows you how to use the String.format() method to add zero padding to a number. If you just want to print out the result you can use System.out.format(). This method is available since Java 1.5, so If you use a previous version you can use the NumberFormat class, see: How do I format a number with leading zeros?.


package org.kodejava.example.lang; public class LeadingZerosExample { public static void main(String[] args) { int number = 1500; // // String format below will add leading zeros (the %0 syntax) // to the number above. The length of the formatted string will // be 7 characters. // String formatted = String.format("%07d", number); System.out.println("Number with leading zeros: " + formatted); } }


Here is the result of the code snippet above:

Number with leading zeros: 0001500

Source : https://kodejava.org/how-do-i-add-leading-zeros-to-a-number/
How do I convert InputStream to String?

How do I convert InputStream to String?

This example will show you how to convert an InputStream into String. In the code snippet below we read a data.txt file, could be from common folder or from inside a jar file.


package org.kodejava.example.io; import java.io.*; public class StreamToString { public static void main(String[] args) throws Exception { StreamToString sts = new StreamToString(); // Get input stream of our data file. This file can be in // the root of you application folder or inside a jar file // if the program is packed as a jar. InputStream is = sts.getClass().getResourceAsStream("/data.txt"); // Call the method to convert the stream to string System.out.println(sts.convertStreamToString(is)); } public String convertStreamToString(InputStream is) throws IOException { // To convert the InputStream to String we use the // Reader.read(char[] buffer) method. We iterate until the // Reader return -1 which means there's no more data to // read. We use the StringWriter class to produce the string. if (is != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader( new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } return writer.toString(); } return ""; } }



Source : https://kodejava.org/how-do-i-convert-inputstream-to-string/

Ads by Google