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);
}
}
Related
// I'm looking for any errors or mistakes I made in the code or another way to do it with if-then statements or usage of array. (very new to java)
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string");
String letter = input.nextLine();
int n = letter.length();
int numberOfLetters = 0;
for (int i = 0; i < letter.length(); i++) {
numberOfLetters++;
}
if(letter.charAt(0) != letter.charAt(n-1)){
String letter2 = letter.toLowerCase();
if (letter2.charAt(0) == letter2.charAt(n - 1)) {
System.out.println("This is a palindrome " + letter);
}
else {
System.out.println("This is not a palindrome " + letter);
}
}
}
}
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));
}
}
I need help making it so that the entire script will loop from the beginning if the user types y at the end and the script ends if the user types n at the end. Thanks for any help.
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
}while (!playAgain.equals("y"));
}
}
}
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
} // end else
} // end do
while (playAgain.equals("y"));
}// end main
} // end class
Change
while (!playAgain.equals("y"));
to
while (!playAgain.equals("n"));
package task;
import java.util.*;
public class Task {
public static void main(String[] args) {
System.out.println("Enter \"t\" to terminate.");
for(;;){
Scanner input = new Scanner(System.in);
int i;
double I = Double.POSITIVE_INFINITY;
for(i = 0; i <= I; i++){
System.out.println("Integer " + i);
String a = input.next();
if(a.equals("c")){
break;
}
}
}
}
}
I am having trouble in prompting the user to enter "t" to end the for loop. I basically want the for loop to print out every single positive integer, and when I decide to end, I enter "t".
If I could get some help, I'd appreciate it. Thanks!
You could simply use a do while instead of a for+break
Do while :
System.out.println("Enter \"t\" to terminate.");
Scanner input = new Scanner(System.in);
String pressed;
int i = 0;
while (true) {
i=0;
do {
System.out.println("Integer " + i);
pressed = input.next();
i++;
} while (!pressed.equals("t"));
}
Note that you are not testing if the input is an integer.
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");
}