basically its a wordle type game where we take input and if the first letter of the input is the same as the first letter of our word "sc" it should make the textView background color to green or yellow if not in the right position
String str=editText.getText().toString();
String first=str.substring(0,1);
String sec=str.substring(1,2);
String third=str.substring(2,3);
String fourth=str.substring(3,4);
if(count==0)
{
textView1.setText(first);
textView2.setText(sec);
textView3.setText(third);
textView4.setText(fourth);
if(first==sc.substring(0,1))
{
textView1.setBackgroundColor(Color.GREEN);
}
else if(first==sc.substring(1,2)|| first==sc.substring(2,3) || first==sc.substring(3,4))
{
textView1.setBackgroundColor(Color.YELLOW);
}
i have tried this but it doesn't seem to be working it does no change
i am new to android studio code so please excuse if this has a very easy solution and i have also tried searching for similar questions online but none of them seemed to be working
In your code, you try for execute your if statement inside if(count==0). I assume you try execute your code if wordle is not fill yet since you dont explain clearly about count.
Based on your question here my answer, first you need initialize input and sc value
String sc = "WORD" \*just example value\*
String str=editText.getText().toString();
String first=str.substring(0,1);
String sec=str.substring(1,2);
String third=str.substring(2,3);
String fourth=str.substring(3,4);
Second, use if statement for check first letter is already filled
if(!first.equals(""))
Inside first if, create if statement for compare first letter with sc value
textView1.setText(first);
if(first==sc.substring(0,1))
{
textView1.setBackgroundColor(Color.GREEN);
}
else
{
textView1.setBackgroundColor(Color.YELLOW);
}
This simple way for reach your goal
== use when we have to compare integers. But you are using == to compare strings. Which is wrong. For comparing Strings you have to use string.matches() or string.equals().
Make changes in your code as below
if(count==0)
{
textView1.setText(first);
textView2.setText(sec);
textView3.setText(third);
textView4.setText(fourth);
if(first.matches(sc.substring(0,1))) // if "sc.substring(0,1)" throwing any error then parse it into "String.valueOf(sc.substring(0,1));"
{
textView1.setBackgroundColor(Color.GREEN);
}
else if(first.matches(sc.substring(1,2))|| first.matches(sc.substring(2,3)) || first.matches(sc.substring(3,4)))
{
textView1.setBackgroundColor(Color.YELLOW);
}
Let me know if the problem has not been solved yet.
Related
I am new to java and coding in general and I was learning how to read from file. I have looked up at the process on how to add file into the src folder by just dragging it but my question persists on why my program is not counting a string "hello" when it reads the file successfully. According to what I think, it is the first element that shows us and I have used the IF statement to check out by storing it in a string element line. can someone explain this to me in a beginner friendly way.
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File path = new File("C:\\Users\\fahad\\IdeaProjects\\testingFileIO\\src\com\\Fahad\\Readingfile.txt");
Scanner fileread = new Scanner(path);
String check = "hello";
int count=0;
boolean flag = false;
while(fileread.hasNextLine()){
String line = fileread.nextLine();
System.out.println(line);
if(line == check)
{
flag = true;
count++;
}
}
if(flag == true)
{
System.out.println("WE found it" + count);
}
else
{
Syste.out.println("No string was there");
}
}
}
output
hello world.
This is fahad Qazi
How are you doing ?
checking
1234567
No string was there
I am new to stackoverflow so sorry about this question but any help is appreciated
The majority of your code is alright. It can be better, but it's alright for a start.
As to where you're going wrong is the line:
if (line == check)
You're comparing the complete line if it equals to the string "hello". This returns false since the line in question is hello world.
A solution to this is to do a contains() operations. Below is an example of this
String myStr = "Hello world";
System.out.println(myStr.contains("Hello")); // true
System.out.println(myStr.contains("world")); // true
System.out.println(myStr.contains("hello")); // false due to case sensitivity
System.out.println(myStr.contains("Hi")); // false
So in your case you should check the following using contains:
if (line.contains(check)) // do the things you wanna do when the line contains 'hello'
To compare String values you don't have to use "==".
You have to compare with the equals method from String class.
For example:
String x="hello",y="world";
if(x.equals(y)){
}
Or you can use x.equalsIgnoreCase(y) if you want to compare two strings and ignore uppercase and lowercase.
Direct == operation on string will not work. You need to use contains(CharSequence s) method from String class in-order to check if the the required sequence of characters is present in the given string or not. You need to make use of this method in your code in order to achieve the required result.
Additionally, if you are using Java 8+, then solution can be simplified/re-written as:
long count = Files.lines(Paths.get(<your_file_path>))
.filter(line -> line.contains(check))
.count();
So recently I've switched over from python to java and was trying to recreate some of the projects that I made on python in java. The first thing that came to mind was a quiz.
Basically, to create a quiz, I define an answer variable to the answer then use the scanner method in java to detect the user's input. After that, I use an if statement to see if the input equals the answer.
ex.
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String answer = "dog";
System.out.println("What is a common furry animal");
String input = scan.nextLine( );
if (input.equals(answer))
{
System.out.println("Correct");
}
else
{
System.out.println("Inncorect");
}
}
}
Now that all works but the user doesn't know the exact casing of the answer variable which means if the variable was "dog" and he input "Dog" it would be incorrect. So if it was possible to create an "or" condition to an if statement it would be awesome if someone let me know.
-Thanks
To or any condition in Java, use the conventional || to separate conditions. In your case it would be something like:
if (input.equals(answer) || input.equalsIgnoreCase(answer))
Although you probably just need the Java method equalsIgnoreCase as the lone condition in the first place.
I am new here and I am making a program to allow a person to sign up for something. It takes in the username, compares it with others, and if it is unique, allows it. When they enter a password however, I don't know how to make sure the word is more than 8 digits. I need to be able to compare the password with something, and only allow the password if it is more than 8 digits or not. Thanks!
You should accept the password into a String type. Then compare it with other String using String.equals(String otherWord) method and check it's length using String.length() method as shown below :-
Scanner s=new Scanner(System.in);
boolean flag=true;
String pwd="";
while(flag) {
pwd=s.nextLine();
if(pwd.length()>8 && pwd.equals("IntendedWord"))
// set flag=false AND then continue your intended action...
else
System.out.println("Please try again");
}
The method you are looking for is String.length().
Use something like:
if (password.length() > 8) {
// password is long enough
}
Try something like this:
// Previous code gets the value for the field and stores it in String pass
if ( pass.length() > 8 )
{
// it is valid. Hash and save
}
else
{
// Not valid. Let user know and ask for reentry.
}
//etc.
You could probably put this and other checking in a validate function and call it before storage.
Let me know if there is anything else you need.
Also, two things to learn about Stack Overflow as a courtesy. Please search for questions similar to yours before posting. And second, give more information when posting so people don't have to guess at what you want/need.
you could use something like
if(password.length() > 8)
{
//some logic
}
that is, considering that you have your password as a string value.
Hey guys I am writing this program and I have a problem with this method.
public int titlesearch()
{
System.out.println("What is the title of the game you want to search for?");
String searchkey;
searchkey =input.nextLine();
String titlekey=searchkey.toLowerCase();
for (int i=0;i<gamelist.size();i++)
{
String gametitle=gamelist.get(i).getTitle();
if (gametitle.equals(titlekey) && gamelist.get(i).getSelling()== true)
{
System.out.println("Game is found!");
return i;
}
}
return -1;
}
Basically, the problem I have with it is that the first time it runs, it skips taking in user input, and it returns -1, skipping the searchkey=input.nextLine() altogether. However, if I call it a second time, it then works. What is the problem with the method?
Scanner input=new Scanner(System.in);
List<gameprofile> gamelist= new Arraylist<>();
These are the declarations for the methods above. By the way I already checked, and the entirety of the program runs but this is the only issue I have.
It should be fine the way you did it, I see no reason why your input line should be skipped. I tried it and executed it on both Windows and Linux and from command line and within eclipse, it was fine every time.
The only reason I can imagine is, that there is already something in your input buffer, but I wouldn't know why. How do you start your program and in which environment?
PS: A small remark, don't name your classes with a lower-case letter, so it should be Gameprofile or even better GameProfile instead of gameprofile.
thanks for reading this. I'm creating just a simple, generic version of blackjack using java. Everything else works completely fine, except when it asks you "hit or pass" and you type pass, you must type it twice for it to reconize it, and I cant seem to find out why. Heres my code on pastebin to make it easier to read: http://pastebin.com/GF7Rzusx
Relevant code from pastebin:
public void ask()
{
System.out.println("Hit or Pass?");
if (in.next().equalsIgnoreCase("Hit"))
{
hit();
}
if (in.next().equalsIgnoreCase("Pass"))
{
pass();
}
}
If the entered word is "Pass" it is read from standard input and then lost, it is not stored. It must be stored for it to be available again in the subsequent check:
String input = in.next();
if (input.equalsIgnoreCase("Hit"))
{
hit();
}
else if (input.equalsIgnoreCase("Pass"))
{
pass();
}
Surely you want:
public void ask()
{
System.out.println("Hit or Pass?");
String answer = in.next();
if (answer.equalsIgnoreCase("Hit"))
{
hit();
} else if (answer.equalsIgnoreCase("Pass"))
{
pass();
}
}
Each time you call in.next() it throws away the previous input and expects another token (input).
Example
Imagine what would happen if you had:
System.out.println(in.next());
System.out.println(in.next());
What would it expect as input and what would it output?
Main differences in code
Note that there are two differences in the new code:
You only call in.next() once and so only need one input, which is stored as answer.
You only check whether the answer is "Pass" if it wasn't already "Hit".
See Java Scanner.next documentation.
On row 112 you do:
in.next()
This will read one string token. So if you write pass, this function will return "pass".
The problem is that you do not save this value. Instead, you run in.next() again on row 116, which will require you to write pass yet again.
Instead you would like to store the string returned from in.next() on line 112.
It is because of the way the ask() method is structured. Since you have two if statements, in.next() gets called once when checking if the response is "hit", then a second time when checking for "pass". When you enter "pass", the first if statement calls next(), which returns "pass" and checks if that is equal to "hit", then moves on. Then when the second if statement calls next(), there is no next entry to return, so you have to enter "pass" again. This works when you enter "hit", however, since that is the first case checked.
Refactoring so ask only makes one call to next() should solve the problem.