overwriting an old String input - java

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
}

Related

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 do I put a list in if statement in Java [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 1 year ago.
Improve this question
import java.util.*
public class text_baes_adventure_game {
public static void main(String[] args){
ArrayList<String> list1 = new ArrayList<String>()
list1.add("Yes")
Scanner input = new Scanner(System.in)
String name = ""
String y_N1 = ""
System.out.println("Hi what is your name: ")
name = input.nextLine()
System.out.println("Hi "+name+" whould you like to play a dungeon game?: ")
y_N1 = input.nextLine()
if (y_N1 == list1){
System.out.println("Ok lets start!")
}
else{
System.out.println("Ok bye!")
}
}
}
1.How do I add a list to the if statement
2.I'm a beginner in java so can someone help me
if (y_N1 == list1){
This doesn't check whether or not the list contains the String y_N1, it checks whether both objects point to the same Object in memory, which isn't possible.
If you mean to check whether or not the list contains the String, use this:
if ( list1.contains(y_N1))
instead.

Java How to display string like this [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 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);
}
}
}

how to take input in Java from user? [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 new to Java but quite good at C. I want to take the input from user in Java. Could anyone please suggest the basic concept with the exact code? Thanks
import java.util.Scanner;
public class Main {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String inputText = sc.nextLine();
}
}
import java.util.Scanner;
This imports the java library that will allow you to read data from the specified input source.
Scanner sc = new Scanner(System.in);
Gets a working instance of the Scanner library in sc which will go get the input for you from the standard input of the system, System.in, in our case, which is the keyboard.
String inputText = sc.nextLine();
This code asks the sc instance to get the line of text typed by the user and set it into the inputText variable.
Note that when sc.nextLine() is called, it halts any execution and waits for user input and proceeds only when the user presses the Enter on the keyboard.
Here is a very simple idea using a Scanner
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter something searched.. ");
String input = scan.next();
System.out.println("your input is " + input);
}

Java Program find string length from user input [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 years ago.
Improve this question
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}
So i want to make a very simple program that takes in a user string and returns the length, but the program does not even run. I am using eclipse. Any tips?
Your program works fine.
Are you forgetting to type something into the console? It might appear to not be running but it actually is.
ALSO: If you are new to java/programming I would suggest using Netbeans over Eclipse. Netbeans offers a bit more support, although it is less flexible (something you shouldn't need to worry about right now).
Make sure that your are providing an input to calculate the length of word.
This is working fine if you providing an input, it is better to change your code as follows:
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your word: \n");
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}

Categories