String method not giving the right output [duplicate] - java

This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
String replace method is not replacing characters
(5 answers)
Closed 5 years ago.
So I was solving a question from a coding website that required me to replace ? with an a or b provided there are no two consecutive a... I have tried a lot and this is the code that i have written. although the code seems correct to my knowledge, whenever I run this code the output is same as the input itself. No changes are made to it.
Example case:
input: ?ababa?b?
output: babababba
input: ababb?b
output:ababbab
The input has to be wither a,b or ?
The output must be such that it has the highest precedence in dictionary.
However whatever input i give i get the same output. If I give ?ab as input I get the same as output please help me
package Beginner;
import java.util.Scanner;
public class ExplRuin {
public static void main(String args[]){
String s;
Scanner in = new Scanner(System.in);
s = in.nextLine();
if(s.length()==1){
if(s.equals("?"))
s.replace("?", "a");
} else
{
if(s.toString().startsWith("?")){
if(s.contains("?b"))
s.replace("?b","ab");
else
if(s.contains("?a"))
- s.replace("?a", "ba");
}
if(s.endsWith("?")){
if(s.contains("a?"))
s.replace("a?", "ab");
else
if(s.contains("b?"))
s.replace("b?","ba");
}
if(s.contains("?a")||s.contains("a?")){
s.replace("?", "a");
}
else{
s.replace("?", "a");
}
}
System.out.print(s);
}
}

You have to assign the result to your variable, because String in java is immutable :
s = s.replace("?", "a");
^^^---------------------------assign the result to your variable

Related

I'm trying to get a toString to print out in a group together after a loop is cancelled [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
This is what I have so far and I tried to use the while (yorn=="yes") to cancel but it keeps going regardless and I am also trying to find a way to make it so the outputs go in one group together rather than being seperated.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String no = "no";
String yes = "yes";
String yorn="yes";
CollegeStudent Student1 = new CollegeStudent();
while (yorn=="yes") {
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
}
}
You need to use the equals method for string comparing in the java:
while (yorn.equals("yes"))
{
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
Since after you answer exists the logic in the cicle, after yes typing code has been executing anyway (one time). Simply solution for you will be:
System.out.println("Do you need to add a student? yes/no:");
yorn=scan.next();
while (yorn.equals("yes"))
{
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
System.out.println("continue? yes/no:");
yorn=scan.next();
}

Java. First loop doesnt work [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i got a little problem with my code. When i write "exit" still shows statement "Unknown command" and i want to show "Bye". Can you help me?
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner odczyt = new Scanner(System.in);
String word;
do{
word = odczyt.nextLine();
System.out.println("Unknown command");
}
while(word!="exit");
System.out.println("Bye");
}
}
"Unknown command" will always be printed. Besides that you shouldn't use = to compare Strings. You should use .equals() or .equalsIgnoreCase().

i want to repeat a statment in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i want to ask a question for a bedsize and while the answer is not what i choose it will be i want that it will ask the user to answer again
import java.util.Scanner;
public static void main(String[] args) {
String newBedType ;
Scanner sc1 = new Scanner(System.in) ;
System.out.println("you want a single bed or doublebed? ") ;
newBedType = sc1.next() ;
while (newBedType != "single" + "doublebed") {
System.out.println("please choose againe the bed size: ");
newBedType = sc1.next() ;
switch (newBedType) {
case "single" : System.out.println("i see you like sleeping alone");
break ;
case "doublebed" : System.out.println("got company ;) ");
break ;
}
}
}
}
the code kinda works it shows the cases if i write the correct string but it will continue to ask me forever.....
i just stared learning java so be easy on me i know its a stupid question but after hours of trying and searching here(though i did found in python but dont know how to "translate" it to java)
i cant figure it out... thanks to anyone willing to help :)
Your issue is with the line:
while (newBedType != "single" + "doublebed")
This doesn't do what you think it does. You are comparing the variable newBedType with the string "singledoublebed", the addition operator is concatenating those two strings. You want the line:
while (!newBedType.equals("single") && !newBedType.equals("doublebed"))
Note the use of the .equals() method, as string comparisons in Java do not act as expected with the == or != operators.

Never runs `if` , always `else` [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
For some reason, my (basic) program always prints the text I reserved for my else statement.
I am a newb when it comes to Java, so if I am making an obvious mistake I apologize. I also searched high and low for an answer, but couldn't find one. Could you take a look at this:
package test;
import java.util.Scanner;
public class tutorial_7 {
private static Scanner x;
public static void main(String args []) {
x = new Scanner(System.in);
System.out.print("Apples, or oranges: ");
String bog = x.next();
if (bog == "Apples") {
System.out.print(1);
}
if (bog == "Oranges") {
System.out.print(2);
}
else {
System.out.print(3);
}
}
}
}
Why is the text reserved for my if statements never being output? Everything seems to be fine.
Regards,
JavaNoob
Don't use == to compare strings, it's for object identity.
Comparing strings should be done with the equals() method, such as:
if (bog.equals ("Oranges")) {
How do I compare strings in Java?
if (bog.equals("Apples")){
System.out.print(1);
}
if (bog.equals("Oranges")){
System.out.print(2);
}
else{
System.out.print(3);
}

cannot find the symbol method console(); [duplicate]

This question already has answers here:
java.lang.System error in Console() [closed]
(3 answers)
Closed 4 years ago.
This is my code :---
import java.lang.*;
class Console
{
public static void main(String args[])
{
char i;
i=System.console().readLine("this is how we give he input to the string");
System.out.println("this is what we want to print:0)");
System.out.println(i);
}
}
and the output I am getting is this:-
Output:-
.java:7: cannot find symbol
symbol : method console()
location: class java.lang.System
i=System.console().readLine("this is how we give he input to the string");
^
1 error
Tool completed with exit code 1
If anyone can help me out...
Mistake with the jdk version, because it must be jdk1.6 or later, and when changed to a newer jdk,
there's a compilation problem, System.console().readLine() returns a String, but you assigned char
Also, some IDE's have trouble with the console class (possibly because they are using it themselves to redirect output to a window/dialog)
So a really good work around is using:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readline();
//or if you want a char
char i = str.charAt(0);
Hope that helps

Categories

Resources