I dont know how to make variable's content as a condition in Java | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

I dont know how to make variable's content as a condition in Java

I need help on how to make variable's content as a condition (true/false) in Java. as an example, this is my code: Scanner scanner = new Scanner(System.in); System.out.println("Do you agree? (true/false) = "); String dataPuas = scanner.next(); if (dataPuas = "true") { System.out.println(" Thank you :) "); } else if (dataPuas = "false") { System.out.println(" Can you tell me what's wrong? "); String complains = scanner.next(); } i tried to use this code but it shows an error, " incompatible types. Required: Boolean. Found: String"

2nd Nov 2019, 8:20 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
14 Antworten
+ 2
I found this post for ways of comparation of strings: https://www.geeksforgeeks.org/compare-two-strings-in-java/
2nd Nov 2019, 8:58 AM
ńŻáščąÚüôŔŽĆňëç´╝Ĺń╣芜»
ńŻáščąÚüôŔŽĆňëç´╝Ĺń╣芜» - avatar
+ 1
Okeey i get it right now, thanks for your help, very nice to talk with people here :)
2nd Nov 2019, 11:39 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
0
no it doesnt works well with " == " but the IDE suggest me to replace == with .equals() anyone has an idea what equals() is? so now my code look like if(dataPuas.equals(" true ") {} it works well now! Thank You so Much!
2nd Nov 2019, 8:54 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
0
equals() is used to compare the contents. For eg if two strings are same then it would return true or else false. public class Program { public static void main(String[] args) { String str1 = "abc"; String str2 = "abc"; System.out.println(str1.equals(str2)); } } This gives output true.
2nd Nov 2019, 9:02 AM
Avinesh
Avinesh - avatar
0
i had a new problem now, variable inside else if ( complains ) can't be accessed outside the statement, how? it shows "cannot resolve Symbol complains"
2nd Nov 2019, 9:02 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
0
It would be nice if you can clearly state what are you trying to achieve. Then post your code and tell us where you are going wrong.
2nd Nov 2019, 9:09 AM
Avinesh
Avinesh - avatar
0
from my codes above : else if (dataPuas = "false") { System.out.println(" Can you tell me what's wrong? "); String complains = scanner.next(); } there is variable named complains, but when i printed this variable outside if-else statement, it shows error : Error:(152, 55) java: cannot find symbol symbol: variable complains location: class com.main.app maybe it because of non-accessible variable inside if-else statement? idk
2nd Nov 2019, 9:19 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
0
Try writing that line just below your scanner class outside or above the loop. Also if you want to store true or false in complains variable then why don't you write- boolean complains = scanner.nextBoolean();
2nd Nov 2019, 9:22 AM
Avinesh
Avinesh - avatar
0
which line? printing variable's line? when i wrote //System.out.println(complains); just below the //String complains = scanner.next(); //it works. but when i wrote it outside, it doesnt work Also i need user's opinion, so it stores in String
2nd Nov 2019, 9:31 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
0
import java.util.*; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean dataPuas = scanner.nextBoolean(); //boolean complains = scanner.nextBoolean(); System.out.println("Do you agree? (true/false) = "); if (dataPuas == true) { System.out.println(" Thank you :) "); } else if (dataPuas == false) { System.out.println(" Can you tell me what's wrong? "); } } } complains is not created, that line is commented. Give input as either true or false.
2nd Nov 2019, 9:40 AM
Avinesh
Avinesh - avatar
0
Noo i dont mean to use boolean there, i mean why the variable cant used outside the statement, i need user opinion after the user choose false in the statement. So i use scanner in this case, handle it to variable called "complains". So after it all, i want to print user's opinion, in this case the variable, but it shows error. Is it not the problem of global variable or something?
2nd Nov 2019, 10:36 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
0
A variable defined inside a block has limited scope and can only be accessed within that block.
2nd Nov 2019, 10:55 AM
Avinesh
Avinesh - avatar
0
So it means it is impossible to print it outside?
2nd Nov 2019, 11:11 AM
Resqi Ageng Rahmatullah
Resqi Ageng Rahmatullah - avatar
0
Pretty much yes. Your way of coding is not right, there is no need to assign value to variable inside a if else statement. Why can't you write the string complains just outside the else if statement. Is there a problem? You are complicating it too much.
2nd Nov 2019, 11:25 AM
Avinesh
Avinesh - avatar