[JAVA] Loop double printing, can’t find the problem - HELP PLS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[JAVA] Loop double printing, can’t find the problem - HELP PLS

Long story short, I have made an application to order movie tickets. In some places the console is double printing things, and I cannot find the error. There’s two problems but to make this less cluttered I’ll focus on the first. ‘films’ refers to an array containing different movie titles. ‘numberSelection’ is for the user to input which movie they’d like to select based on the array. /////////////////////////////////////////////////////////// private static Film filmSelection() { int numberSelection: System.out.println(“Which film would you like to watch:”); for(int i=1;i<films.length;i++) { System.out.println(i + “ “ + films[i]); } numberSelection = scan.nextInt(); if(numberSelection >= 1 && numberSelection <= 4){ return films[numberSelection]; } else { return null } } /////////////////////////////////////////////////////////// The current console output is the print statement is: 1 MovieTitleHere 2 MovieTitleHere 3 MovieTitleHere 4 MovieTitleHere (Scanner input here) 1 MovieTitleHere 2 MovieTitleHere 3 MovieTitleHere 4 MovieTitleHere (Scanner input here *notice how it duplicates *the duplication doesn’t show until the first scanner input has been entered.

15th Aug 2019, 2:26 AM
Raj Grewal
Raj Grewal - avatar
7 Answers
+ 8
Ok Raj Grewal Probably calling of method multiple times for same input is cause for such output //good luck
15th Aug 2019, 4:29 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
Are you calling method filmSelection() multiple times for the same input ?
15th Aug 2019, 4:26 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
Can't really tell based on this snippet alone, but I do see a problem. Array index starts with zero, so the appropriate index value should range from 0 to 3 for an array of size 4. What is happening in your code though, is you accessing and returning arrays starting from index 1. I would suggest making the following amendments: for(int i=0;i<films.length;i++) { System.out.println((i+1) + “ “ + films[i]); } //... if(numberSelection >= 1 && numberSelection <= 4) { return films[numberSelection-1]; } else { return null }
15th Aug 2019, 2:42 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Maybe you should look at how filmSelection() is being invoked.
15th Aug 2019, 3:28 AM
ODLNT
ODLNT - avatar
+ 1
The reason it is starting from 1 is because I am trying to replicate an application using the console output provided to me. In the console output I am replicating from, the array starts at 1. I tried to play around with it and start from 0 but it doesn’t fix the problem.
15th Aug 2019, 2:53 AM
Raj Grewal
Raj Grewal - avatar
0
i have a for loop for whole application. it’s a bot hard to show because I can’t post images. i’ve got to submit my project in 20 minutes so I’ll just have to leave it. I’m new to Java so this is probably something small tbh
15th Aug 2019, 4:28 AM
Raj Grewal
Raj Grewal - avatar
0
filmSelection() is correct post how you do main loop and part after Scanner input from user for small programs you can create a code on sololearn save it and post link https://code.sololearn.com/#java or for longer code use https://pastebin.com/ or for whole project https://github.com/
15th Aug 2019, 7:35 AM
zemiak