1

Is it possible to modify a file path so that it incorporates a String variable?

example code

String var = "Picture.jpg";
   ImageIcon img = new ImageIcon("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\var");
2
  • 3
    ImageIcon img = new ImageIcon("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\" + var); Commented Nov 7, 2015 at 22:02
  • omg I feel dumb, such an obvious answer here. Commented Nov 7, 2015 at 22:09

1 Answer 1

2

Use String.format

String var = "Picture.jpg";
ImageIcon img = new ImageIcon(String.format("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\%s", var));
Sign up to request clarification or add additional context in comments.

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.