Why won't my code work? (Search: Capital Cities) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why won't my code work? (Search: Capital Cities)

error: package system does not exist https://code.sololearn.com/c2c5ca06O7Kf/#java

16th Feb 2018, 5:08 PM
Aliensuper3
Aliensuper3 - avatar
19 Answers
- 2
Search to see my code
16th Feb 2018, 4:44 PM
Aliensuper3
Aliensuper3 - avatar
+ 5
lol The irony that I specialize in Java and you treated me like crap when trying to help you. Don't worry, I'm actually a nice guy. ;) https://code.sololearn.com/cCAPTOFoRfY9/#java import java.util.Scanner; class MyClass { public static void main(String[ ] args) { // Store our cities/countrys into an array String[] cities = {"London", "Paris", "Washington"}; String[] countrys = {"England", "France", "America"}; // Store scanner object in scnr Scanner scnr = new Scanner(System.in); // This is where we'll store the input from the scanner object String country = ""; /* !() means NOT and isEmpty() checks if it's empty / So !().isEmpty() is making sure we have input before looping / If there is input, lets check for its city and print the city/country */ while(!(country = scnr.nextLine()).isEmpty()){ // We have user input, so lets loop through countrys to see if we have match for(int x = 0; x < countrys.length; ++x){ // Since it's string comparison, use .equals() instead of == if(country.equals(countrys[x])){ // If our input equalled one of the countries, match it with the city and print System.out.println(cities[x] + ", " + country); } } } } } :::::: INPUT :::::::: England France America ::::: OUTPUT ::::::: London, England Paris, France Washington, America
16th Feb 2018, 6:16 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
Haha. :D You down vote me because I let you know what's required for us to help you, and then you tell us to "Go search for your code" when you're the one asking/creating threads for help? I can't wait to help you out in the future. Best of luck to you.
16th Feb 2018, 4:51 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
@jakob you're every where !! lol you're cod is very good. but. you made it complex. (considering that probebly the person asking about it. is a learner)
16th Feb 2018, 5:42 PM
Farshaad Heydari
Farshaad Heydari - avatar
+ 2
@Farshaad lol :D Sad thing is I'm at work. It's better to learn how to do it properly when learning than learning how to do it the wrong way and then having bad habits later that you have to try to fix, especially if you end up working on a team with other people that all use particular standards. With that being said, I should have properly commented/documented what I was doing to help you all learn. I'll go back and do that right now for y'all.
16th Feb 2018, 5:45 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
@Far / Alien: Updated code/post with comments.
16th Feb 2018, 6:16 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
No clue. You didn't post your code, your error, or any other vital information.
16th Feb 2018, 4:43 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
You should post you code with the link here.
16th Feb 2018, 4:47 PM
🅿️®️⭕️_e✖️e
🅿️®️⭕️_e✖️e - avatar
+ 1
You should also tell us what errors you get
16th Feb 2018, 4:47 PM
🅿️®️⭕️_e✖️e
🅿️®️⭕️_e✖️e - avatar
+ 1
first letter of "system " must be capitalized. but after changing it. there is another error. with "country.next()" witch I don't know (to be unest!! I was lazy ....)
16th Feb 2018, 5:17 PM
Farshaad Heydari
Farshaad Heydari - avatar
+ 1
allrigh. I fixed it . (after doing what I said in previous post ) first , you are trying to compare two objects using "==" ... witch is wrong . secondly you should save the input from scanner to a string and then call the "equal ()" method for comparison.... sorry I don't know more than that....
16th Feb 2018, 5:33 PM
Farshaad Heydari
Farshaad Heydari - avatar
+ 1
Aliensuper3 : here is your code with my modifications .... https://code.sololearn.com/cNp80QeRPE5f/?ref=app
16th Feb 2018, 5:45 PM
Farshaad Heydari
Farshaad Heydari - avatar
0
sorry
16th Feb 2018, 4:53 PM
Aliensuper3
Aliensuper3 - avatar
0
I would say that sololearn does not support java.util.Scanner but I’m not absolutely sure.
16th Feb 2018, 4:59 PM
Jax
Jax - avatar
0
it is in the tutorial, basics, input so it should support it
16th Feb 2018, 5:06 PM
Aliensuper3
Aliensuper3 - avatar
0
Ok, I don’t know for sure because I never learned java but that was just my guess.
16th Feb 2018, 5:07 PM
Jax
Jax - avatar
- 1
kk
16th Feb 2018, 4:47 PM
Aliensuper3
Aliensuper3 - avatar
- 1
1 sec
16th Feb 2018, 4:47 PM
Aliensuper3
Aliensuper3 - avatar
- 2
(capital cities)
16th Feb 2018, 4:44 PM
Aliensuper3
Aliensuper3 - avatar