How to fix exception in thread main java lang common exceptions
public class Test
{
public static void main(String[] args)
{
Student st = new Student();
System.out.println("Result: " + st.sayHello());
}
}
class Student
{
private String name;
public String sayHello()
{
return "Hello " + name.toUpperCase();
}
public void setName(String name)
{
this.name = name;
}
}
Variable length Argument Lists
public class VarargsTest
{
public static void main(String[] args)
{
System.out.println(" Sum = " + sum(41, 22, 58));
}
static int sum(int... numbers)
{
int sum = 0;
for (int i = 0; i < numbers.length; i++)
{
sum += numbers[i];
}
return sum;
}
}
Static Import
import static java.lang.Integer.*;
import static java.lang.String.format;
public class StaticImportTest
{
public static void main(String[] args)
{
int num = parseInt("526");
Integer.parseInt()
Integer num2 = valueOf("123");
Integer.valueOf()
String.format()
System.out.println(format("Numbers: %d, %d", num, num2));
Integer.Max_Value
System.out.println(" Integer Max value = " + Max_Value);
}
}
Java program to check number is palindrome or not
package testing;
import java.util.Scanner;
import java.util.Scanner;
public class
NoClassDefFoundErrorDueToStaticInitFailure
{
public static
void main(String args[])
{
System.out.println("Please Enter a number : ");
int palindrome = new Scanner(System.in).nextInt();
if(isPalindrome(palindrome))
System.out.println("Please Enter a number : ");
int palindrome = new Scanner(System.in).nextInt();
if(isPalindrome(palindrome))
{
System.out.println("Number : " + palindrome + " is a palindrome");
}
System.out.println("Number : " + palindrome + " is a palindrome");
}
else
{
System.out.println("Number : " + palindrome + " is not a palindrome");
}
}
public static boolean isPalindrome(int number)
System.out.println("Number : " + palindrome + " is not a palindrome");
}
}
public static boolean isPalindrome(int number)
{
int palindrome = number;
int reverse = 0;
int palindrome = number;
int reverse = 0;
while
(palindrome != 0)
{
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
if (number == reverse)
{
return true;
}
return false;
}
}
return true;
}
return false;
}
}
Palindrome
string in java
public class Palindrome
{
private String pal;
public Palindrome(String initPal)
{
pal = initPal.toUpperCase();
}
public boolean isPalindrome()
{
if (pal.length() <= 1)
{
return true;
}
char first = pal.charAt(0);
char last = pal.charAt(pal.length()-1);
if (Character.isLetter(first) &&
Character.isLetter(last))
{
if (first != last)
{
return false;
}
else
{
Palindrome sub = new Palindrome(pal.substring(1,pal.length()-1));
return sub.isPalindrome();
}
}
else if (!Character.isLetter(first))
{
Palindrome sub = new Palindrome(pal.substring(1));
return sub.isPalindrome();
}
else
{
Palindrome sub = new Palindrome(pal.substring(0,pal.length()-1));
return sub.isPalindrome();
}
}
public static void main(String[] args)
{
Palindrome p1 = new Palindrome("Madam, I'm Adam.");
System.out.println(p1.isPalindrome());
Palindrome p2 = new Palindrome("Nope!");
System.out.println(p2.isPalindrome());
Palindrome p3 = new Palindrome("dad");
System.out.println(p3.isPalindrome());
Palindrome p4 = new Palindrome("Go hang a salami, I'm a lasagna hog.");
System.out.println(p4.isPalindrome());
}
}
Check palindrome string using recursion
package beginnersbook.com;
import java.util.Scanner;
class PalindromeCheck
{
public static boolean isPal(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return isPal(s.substring(1, s.length()-1));
return false;
}
public static void main(String[]args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the String for check:");
String string = scanner.nextLine();
if(isPal(string))
System.out.println(string + " is a palindrome");
else
System.out.println(string + " is not a palindrome");
}
}
No comments:
Post a Comment