Java How to display string like this [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
My program will take a string from user via a text field
when he presses the button the string should be formatted as shown in the example
An Example will help you understand better
If String Entered is "hello"
Output should be
hello
elloh
llohe
lohel
ohell
hello
The first character must shift to last until the initial word is formed again.
This must work for any length of string
Displaystr =newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length - 1);
I tried this code but It didn't help
Edited - Please don't put question on hold now.

try this:
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
System.out.println(text);
for(int j=0;j<text.length();j++) {
char firstLetter = text.charAt(0); //get the first letter
text = text.substring(1); //remove the first letter from the input string
text = text + firstLetter;
System.out.println(text);
}
}
}

Related

overwriting an old String input

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 hours ago.
Improve this question
I'm trying to make my program work with a new input instead of the old one when the user clicks an option from the options menu
I don't what the problem that is not causing the program to continue with the new input instead of the old one. thanks in advance.
public static String optionSix (String input)
{
String newInput;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter List of Words Seperated by Spaces: ");
newInput = scnr.nextLine();
input = newInput;
return input;
}
Java is pass-by-value, so you can't override a string's value like that. You'll have to assign the new value to it:
public static void main(String args[]) {
String someOldString = "some old value";
someOldString = optionSix(); // No need to pass the original value either
}

why is my Input Validation loop not working properly? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
Improve this question
public static String reqnumber() {
Scanner keyboard = new Scanner(System.in);
String s;
boolean isMatch = true;
ArrayList<String>patientsNumbers = new ArrayList<String>();
int i = patientsNumbers.size();
do
{
System.out.println("enter number: ");
s = keyboard.next();
if(!(patientsNumbers.contains(s))){
patientsNumbers.add(s);
isMatch = false;
}else{
System.out.println("this is a number taken by another patient");
}
}while(isMatch);
return patientsNumbers.get(i);
}
here is a piece of code that includes an input validation loop, as every number of patient entered by user it should be saved in an arraylist called patientsNumbers.
and when the method reqnumber() is called in main, in the output when a user inputs a number already exist it should prompt the user that the number already taken by another patient and to enter another number but it doesn't, how to solve this!?

How to find words that are different between two Strings? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I hope you are fine.
I am searching for a simple way to compare two Strings and print out the words which are unique between the two, for example I have :
String one = edittext1.getText().toString();
String two = editText2.getText().toString();
Then the output should be what is the words that exists in edittext1 and not exist in edittext2.
if it's only words you can split the strings to string[] that each cell will contain 1 word only and then compare those words. Goes as follows:
String one = "this is first text example";
String two = "this is next text example";
String[] oneVals = one.split("\\ ");
String[] twoVals = two.split("\\ ");
int i = oneVals.length;
if(oneVals.length != twoVals.length)
{
// determine what to do
}
String wordsNotMatching = "";
for(int j=0; j<i; j++)
{
if((!oneVals[j].equals(twoVals[j])))
wordsNotMatching += oneVals[j] + " ";
}
// wordNotMatching will contain all different words.

Java Beginner logic error help needed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I apologize in advance for my post. I decided to register tonight and this is my first post.
A little background about the question.
I am doing a little java project, where I need to take a name from user input, which is saved in a variable. the variable value must then display the output.
my question is. How is it done in java, using the (if) keyword, to use my code to create an if statement that voids my simple i_PlayerName method of any logic error.
to be more specific, how can I make sure that my program will only accept a string value, and if another data type is entered, an output message will be displayed " user input not accepted ".
lastly, what is this procedure called?
Thank you all :)
keep on coding the code.
public void i_PlayerName() {
String playerName;
Scanner i_playerName = new Scanner(System.in);
System.out.println("ENTER YOUR SHIP NAME");
playerName = i_playerName.nextLine();
System.out.println("Your vessel has been named " + playerName);
}
Assuming you meant alphabets when you said only strings are allowed, this should do it.
public static void main(String[] Args) {
String playerName = "";
System.out.print("Enter ship name: ");
Scanner scanner = new Scanner(System.in);
String i_playerName = scanner.next();
System.out.println();
char[] chars = i_playerName.toCharArray();
boolean isChar = true;
for (char c : chars) {
if (!Character.isLetter(c)) {
isChar = false;
}
}
if(isChar == true) {
System.out.println("Your vessel has been named: " + i_playerName);
}
else {
System.out.println("User input is not accepted.");
}
}
when the scanner is reading if you assign it to a String type it will take the input as a String regardless if the user inputs numbers or Strings. If you want to handle the scenario when user inputs value other than String you can do it with regular expression and if the input is not a match then output the error. Use below method to check if input is a String.
public boolean isStringValue(String s){
String pattern= "[a-zA-Z]*";
return s.matches(pattern);
}
//use method above:
String input = scan.next();
if(isStringValue(input)){
//your code
} else {
//output some error
}

Replace Every 5th Character Of The Input (SPACES COUNT) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying write a program that accept an input and present the input with every 5th character replaced with X (count spaces also).
For example:
input: "hello my name is mario"
outpu: "hellX my Xame Xi maXio"
I only manage to replace specific letters, e.g every "m" letter.
Any help?
If you don't care about which character is at each fifth position you could use a regex.
String input = "hello my name is mario";
String output = input.replaceAll("(....).", "$1X");
System.out.printf("input : %s%noutput: %s%n", input, output);
output
input : hello my name is mario
output: hellX my Xame Xs maXio
Here is the code for you:
String test = "hello my name is mario";
String result = "";
int c = 1;
for (int i = 0; i < test.length(); i++) {
if (c++==5) {
result += "X";
c = 1;
} else {
result += test.charAt(i);
}
}
System.out.println("result = " + result);

Categories