Scanner java maby easy but cant find anything | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Scanner java maby easy but cant find anything

hey can anyone help me to get system.in to use my string? for example String x; Scanner input = new Scanner(System.in); input = x.nextline(); system.out.print(input); if frog (print rabbit if dog (print woof if cat print meow ect. ect. lol so if i input dog the output would be woof! thanks

10th Jun 2017, 12:51 AM
D_Stark
D_Stark - avatar
9 Answers
+ 4
nextLine isn't a method for a String its a method for the scanner. You wrote x.nextLine(), x is your String. Do x = input.nextLine(); System also has a caps on the s. I'll just assume the if statements aren't finished intentionally.
10th Jun 2017, 1:00 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
import java.util.Scanner; Scanner input = new Scanner(System.in); String x = input.nextLine(); if(x=="dog"){ System.out.print("Woof"); }
10th Jun 2017, 1:08 AM
LordHill
LordHill - avatar
+ 2
why equals function over ==? seems like == would be faster than accessing a function *Nevermind. I just read up on equals() .. Sami is correct
10th Jun 2017, 1:58 AM
LordHill
LordHill - avatar
+ 2
@Justin Hill When dealing with primitive data-types == will compare by values, and is safe to use. However, when comparing == with Objects (which is what a String is) it will compare the reference in memory instead since there's no pre-set way to compare equality for Objects. == can work for strings under some circumstances, however, not reliably. It may work only because the compiler may set a String that has the same value as the String you initially created, rather than creating a new string object. It's more of an optimization by JVM. (Note* I don't know much about how this works, so I could be off on something) So writing: String a = "apple"; String b = "apple"; b would likely just be set to the reference of a, so b isn't really a new String. So == would work here. Since this may not be reliable as you can create your own String objects unintentionally, it's best to use the equals() method. equals() will essentially itterate through each character in the String, comparing them one by one to check for equality. So it is fairly slower but you are garunteed the results you want. If you know when == would work, I suppose you could just use ==. According to stackOverFlow how java works with String references is called String Interning. It might be worth the read: https://en.m.wikipedia.org/wiki/String_interning
10th Jun 2017, 4:23 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Should variables of ints/floats/doubles/etc be compared with equals() as Well? Or is this mainly for Strings? and If it is just a string thing, will it hurt anything to use equals for all comparisons anyway?
10th Jun 2017, 2:11 AM
LordHill
LordHill - avatar
+ 1
you can only use the equals for the wrapper object types.. like Integer Double String not for primitives data type
10th Jun 2017, 2:15 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
instead of == operator use equals() function import java.util.Scanner; public class Program { public static void main(String[] args){ Scanner input = new Scanner(System.in); String x = input.nextLine(); if(x.equals("dog")){ System.out.print("Woof"); } }
10th Jun 2017, 1:28 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
it doesn't works for comparing variable to the object types like string... u will get an error of NullPointerException
10th Jun 2017, 2:01 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
Thanks everyone. Just woke up, going to give that link a read while I eat breakfast
10th Jun 2017, 1:38 PM
LordHill
LordHill - avatar