+ 1
Why won't my code work? (Search: Capital Cities)
error: package system does not exist https://code.sololearn.com/c2c5ca06O7Kf/#java
19 Respuestas
- 2
Search to see my code
+ 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
+ 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.
+ 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)
+ 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.
+ 2
@Far / Alien:
Updated code/post with comments.
+ 1
No clue. You didn't post your code, your error, or any other vital information.
+ 1
You should post you code with the link here.
+ 1
You should also tell us what errors you get
+ 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 ....)
+ 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....
+ 1
Aliensuper3 : here is your code with my modifications ....
https://code.sololearn.com/cNp80QeRPE5f/?ref=app
0
sorry
0
I would say that sololearn does not support java.util.Scanner but I’m not absolutely sure.
0
it is in the tutorial, basics, input so it should support it
0
Ok, I don’t know for sure because I never learned java but that was just my guess.
- 1
kk
- 1
1 sec
- 2
(capital cities)