1

I'm trying to make a program that asks the user to type in 3 cities and the program is supposed to take the 3 cities , and put them in a String Array , the first city in [0],second in [1] and third in [2] , I got it to ask for them , collect the answers but it's only Printing out the first answer, not all 3. Any ideas how I can fix that?

My code looks like this atm

public static void main(String[] args) {

    String ans;   
    String[] favoritStad = new String [3];
    Scanner scanner1 = new Scanner (System.in);

    System.out.println("skriv in 3 favoritstäder");
    String Användarinlägg1 = scanner1.nextLine();
    String Användarinlägg2 = scanner1.nextLine();
    String Användarinlägg3 = scanner1.nextLine();

    favoritStad[0] = Användarinlägg1;
    favoritStad[1] = Användarinlägg1;
    favoritStad[2] = Användarinlägg1;


    System.out.print(Användarinlägg1);  

}

Användarinlägg is userinputt , favorit stad is favcity the string "ans" was just an idea I tried to make to collect all 3 answers and print it out but never figured it out

Solved it ! Just needed to add

System.out.print(Användarinlägg2);
System.out.print(Användarinlägg3);
3
  • try using a for loop Commented Sep 11, 2013 at 13:25
  • A hint: I wouldn't use anything but ASCII for any code. Just causes pain in the --- you know where... Commented Sep 11, 2013 at 13:26
  • hahahaha well im trying to learn java eclipse thanks for advice tho ! Commented Sep 11, 2013 at 13:28

5 Answers 5

2

As I suggested in my comment below your question - use a for loop. Also always check twice if you are not using the same variable (for example Användarinlägg1) over and over.

favoritStad[0] = Användarinlägg1;
favoritStad[1] = Användarinlägg2;
favoritStad[2] = Användarinlägg3;

for(int i=0; i<favoritStad.length; i++) {
    System.out.println(favoritStad[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

In your code you've added the same element 3 times.

You need to use:

favoritStad[0] = Användarinlägg1;
favoritStad[1] = Användarinlägg2;
favoritStad[2] = Användarinlägg3;

And in order to print you can use for loop or just:

System.out.print(favoritStad[0]);
System.out.print(favoritStad[1]);
System.out.print(favoritStad[2]);

Comments

0

You can use for loop to print all the values of an array. You can use something like below:

for (int i=0; i<favoritStad.length();i++){
    System.out.println(favoritStad[i])
}

1 Comment

thats to advanced for me i dont understand Everything in there :D solved it anywa with simply spaming same option as system,outprint first userinput
0

Try this way

favoritStad[1] = Användarinlägg2;
favoritStad[2] = Användarinlägg3;

Now Print Array:

1st way :

for(int i=0; i<favoritStad.length; i++) {
    System.out.println(favoritStad[i]);
}

2nd way:

 for(String s :favoritStad) {
      System.out.println(s);
  }

Comments

0

Also you need to print...

System.out.print(favoritStad); 

instead of

System.out.print(Användarinlägg1); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.