I want to write a code that deletes a character from a string(stringbuilder)and continue until the loop finishes. when I delete a character in a string the loop which include this string stop immediately.
for(i=1;i<n;i++){
for(j=0;j<x[i].length();j++){
if((x[i].contains(v)) || (x[i].contains(other))){
}
else{
for(m=0;m<u;m++){
for(l=0;l<x[i].length();l++){
System.out.format("d[i][l]%s charat(m)%s \n",d[i][l],v.charAt(m));
if(d[i][l] != v.charAt(m)){
count++;
System.out.println(m);
System.out.format("count:%d\n",count);
}
if(count == x[i].length()){
v.deleteCharAt(m);
others.deleteCharAt(v.length()-m-1);
System.out.format("count%d \n",count);
System.out.format("u%d ",u);
System.out.println(v);
count=0;
}
}
count=0;
}
}
}
}
Here is the code for delete no.of given chars from given String.
import java.util.*;
class Test{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter String : ");
String str=sc.nextLine();
System.out.print("\nEnter No.of Deletion : ");
int n=sc.nextInt();
//convert String char Array
char[] st = str.toCharArray();
if(st.length>n)
{
for(int i=0;i<n;i++)
{
st[i]='\0';
}
}
else{
for(int j=0;j<st.length;j++)
{
st[j]='\0';
}
}
System.out.println("Rest String : "+String.valueOf(st));
}
}
Related
In a given sentences, reverse each word and print the reversed words in the sentences. if there is a palindrome print those words .if there is no palindrome print "No Palindrome".
this is what i wrote
import java.util.Scanner;
public class main{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String s1=input.nextLine();
String arr[]=s1.split("\\s",s1.length());
int count=0;
String palindrome[]=new String[s1.length()];
for(int i=0;i<arr.length;i++){
String s2="";
for(int j=arr[i].length()-1;j>=0;j--){
s2=s2.concat(Character.toString(arr[i].charAt(j)));
System.out.println(arr[i].charAt(j));
}
System.out.print(" ");
if(arr[i].equals(s2)){
count++;
palindrome[i]=s2;
}
}
if(count>0){
for(String i:palindrome)
System.out.println(i);}
else
System.out.println("Not a palindrome");
}
}
But code is not giving proper output.
This should work:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s1 = input.nextLine();
String[] arr = s1.split(" ");
StringBuilder output = new StringBuilder();
for (String currentWord : arr) {
String reverseWord = new StringBuilder(currentWord).reverse().toString();
if (currentWord.equals(reverseWord)) {
output.append(reverseWord);
} else {
output.append("No Palindrome ");
}
}
System.out.println(output);
}
Here's a shorter way of doing this:
//prints each palindome word in a sentence
Arrays.asList(sentence.split(" ")).stream().filter(this::isPalindrome).forEach(System.out::println);
Here is a working code that you can use for checking the input string/number is palindrome or not palindrome
code:
import java.util.*;
class stackOverflow {
public static void main(String[] args) {
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number to check if it is a palindrome or not:");
// takes input string
original = in.nextLine();
int length = original.length(); // length of the string to do iteration on it
// check the string from the end to the start to reverse it
// read and append it with the reverse variable in backward
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
// Finally we check if the input string and reversed string
if (original.equals(reverse))
System.out.println("Is palindrome.");
else
System.out.println("Not palindrome.");
}
}
I've edited your code and now it's working fine:
import java.util.*;
class stackOverflow {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String s1=input.nextLine();
String arr[]=s1.split("\\s",s1.length());
for(int i=0;i<arr.length;i++){
String s2="";
for(int j=arr[i].length()-1;j>=0;j--){
s2=s2.concat(Character.toString(arr[i].charAt(j)));
//System.out.println(arr[i].charAt(j));
}
//System.out.print(" ");
if(arr[i].equals(s2)){
//palindrome[i]=s2; // you are inserting the s2 value into the first element of the array,
//so the rest of the positions remains empty/null that's not a problem to solve palindrome
System.out.println("Is a palindrome");
}
else
System.out.println("Not a palindrome");
}
}
}
What I am trying to do is very elementary. The user should enter a name and the program prints only the initials of the name except for the last word.
Eg:
input - "Mansha Mannan UL Haque"
output - "M.M.U.Haque"
The program is as follows but it does not compile.
class joke
{
public static void main(String str)
{
String alter=" "+str;
int n=alter.length();
for(int i=0;i<=n;i++)
{
char f=alter.charAt(i);
if (f.compareTo(" ")>0)
{
System.out.println(alter.charAt(i+1));
}
}
}
}
The error showed is char cannot be dereferenced.
Split your input string into string array then access each word and print its first char, for last word print it whole.
class joke
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String alter=sc.nextLine(); //take input from user
String arr[] = alter.split(" ");
int n=arr.length(); //total words in user string
for(int i=0;i<n;i++)
{
if(i==n-1){ //if last word
System.out.print(arr[i]);
}
else{
System.out.print(arr[i].charAt(0)+". ");
}
}
}
}
This should do the trick
in this case namewould be your input
String name = "Mansha Mannan UL Haque";
String[] nameArray = name.split(" ");
String newName = "";
for(int i = 0; i < nameArray.length; i++)
{
if(i != nameArray.length -1)
{
newName += nameArray[i].substring(0, 1) + ".";
}
else
{
newName += nameArray[i];
}
}
and you should also make your class joke public and change String str to String[] args
I have a program that is supposed to ask the user for a number and it will determine whether it is a palindrome or not. It's supposed to keep asking for numbers until EOF is input - So far it asks for the number twice and doesn't seem to be doing the while loop correctly.
Any insight is appreciated
import java.util.Scanner;
public class PalindromeEOF
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to check if it is a palindrome:");
String num = scanner.nextLine();
String reverse = "";
while (scanner.hasNextLine())
{
for ( int i = 0; i<num.length(); i++ )
{
reverse = num.charAt(i) + reverse;
}
if (num.equals(reverse))
{
System.out.println("\nEntered number IS a palindrome.");
}
else
{
System.out.println("\nEntered number is NOT a palindrome.");
}
System.out.println("\nEnter a number to check if it is a palindrome:");
num = scanner.nextLine();
reverse = "";
}
System.out.println("\nProgram ended on request");
}
}
This worked for me; unless you need num or reverse outside the while loop it should work.
import java.util.Scanner;
public class PalindromeEOF
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to check if it is a palindrome:");
while (scanner.hasNextLine())
{
String num = scanner.nextLine();
String reverse = "";
for ( int i = 0; i<num.length(); i++ )
{
reverse = num.charAt(i) + reverse;
}
if (num.equals(reverse))
{
System.out.println("\nEntered number IS a palindrome.");
}
else
{
System.out.println("\nEntered number is NOT a palindrome.");
}
System.out.println("\nEnter a number to check if it is a palindrome:");
}
System.out.println("\nProgram ended on request");
}
}
I would separate the palindrome test into its' own method. You could do that in a one line method like
public static boolean isPalindrome(String str) {
return new StringBuilder(str).reverse().toString().equals(str);
}
but I would prefer to iterate the first half of the characters and compare them to the second half in reverse like
public static boolean isPalindrome(String str) {
if (str == null) {
return false;
}
char[] chars = str.toCharArray();
for (int i = 0; i * 2 <= chars.length; i++) {
if (chars[i] != chars[chars.length - i - 1]) {
return false;
}
}
return true;
}
Then your main can invoke that in an infinite loop (terminating on the lack of input) like
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a number to check if it is a palindrome:");
if (!scanner.hasNextLine()) {
break;
}
String num = scanner.nextLine();
if (isPalindrome(num)) {
System.out.printf("%s is a palindrome%n", num);
} else {
System.out.printf("%s is NOT a palindrome%n", num);
}
}
System.out.println("Program ended on request");
}
I am trying to get user input and determine if the word is a palindrome or not. The main method should be where all of the print statements are placed.
package help;
import java.util.Scanner;
public class Help {
public static Scanner user_input;
public static void main(String[] args, Iterable<String> lines) {
System.out.print("Enter a word: ");
user_input=new Scanner(System.in);
}
public static boolean istPalindrom(char[] word) {
int i1 = 0;
int i2 = word.length - 1;
while (i2 > i1) {
if (word[i1] != word[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}
}
I want it to say
if true then System.out.println(The word is a palindrome.)
else System.out.println(The word is not a palindrome.)
I am not sure how to go about this.
Add following lines into main:
if (isPalindrome(user_input))
System.out.println ("The word is a palindrome.");
else
System.out.println("The word is not a palindrome.");
Note: Try to make your indentation better in order to make code easily understandable and for visual purposes :)
Your palindrome test looks good once you fix the indentation, but it's named oddly in English. You have a Scanner, so you can get lines of input. Then call toCharArray() on the String to get the char[] like
public static void main(String[] args) {
System.out.print("Enter a word: ");
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
String line = input.nextLine();
if (isPalindrome(line.toCharArray())) {
System.out.printf("The word %s is a palindrome.%n", line);
} else {
System.out.printf("The word %s is not a palindrome.%n", line);
}
}
}
public static boolean isPalindrome(char[] word) {
int i1 = 0;
int i2 = word.length - 1;
while (i2 > i1) {
if (word[i1] != word[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}
This solution might work:
public static void main(String[] args, Iterable<String> lines) {
user_input = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = user_input.nextLine();
if(istPalindrome(word.toCharArray()){
System.out.println("The word is a palindrome");
}else{
System.out.println("The word is not a palindrome");
}
}
The code for palindrome as simple as this if you want to use it !
public static boolean isPalindrom(String word) {
if(new StringBuilder(word).reverse().toString().equals(word)){
System.out.println("The word is a palindrome.");
} else {
System.out.println("The word is not a palindrome.");
}
}
I am making a hangman program for my practice and i am halting at how to print the correct character entered by user with the partial answer.Every thing is working optimally, just need help in concatenation.?????????? is to map the place for which I need the logic.
import java.io.*;
import java.util.Scanner;
class hangman{
public static void main(String args[]){
int counter=6;
String m="ashish";
char mj[] = m.toCharArray();
//for printing the puzzle
for(int j=0;j<m.length();j++)
{
if(mj[j]%3==0)
{
System.out.print(" "+mj[j]);
}
else System.out.print(" ___ ");
}
System.out.println();
//taking the input from user
Scanner scanner=new Scanner(System.in);
do{
char c=scanner.next().charAt(0);
System.out.println(c+"-----scanning complete");
for(int i=0;i<m.length();i++)
{
if(c==mj[i])
{
String n= ?????????????????????;
counter--; }
}while(counter != 0);
}}
You need to save some sort of array of characters the player has got correct.
Try something like this:
import java.io.*;
import java.util.Scanner;
class hangman {
public static void main(String args[]){
int counter = 6;
String m = "ashish";
char mj[] = m.toCharArray();
char correct[] = new char[mj.length];
//for printing the puzzle
for(int j=0;j<m.length();j++) {
if(mj[j]%3==0) {
System.out.print(" "+mj[j]);
correct[j] = mj[j];
}
else {
System.out.print(" ___ ");
}
}
System.out.println();
//taking the input from user
Scanner scanner=new Scanner(System.in);
do {
char c=scanner.next().charAt(0);
System.out.println(c+"-----scanning complete");
for(int i=0;i<m.length();i++) {
if(c==mj[i]) {
correct[i] = c;
counter--;
}
// This is the default value of a char in Java.
if (correct[i] == '\u0000') {
System.out.print(" ___ ");
} else {
System.out.print(correct[i]);
}
}
} while(counter != 0);
}
}