+ 2

New to Java - can you tell me what´s wrong on my program?

Hello everyone. I started to learn Java from the course here on Sololearn. I have completed it all in a couple of hours, but I haven´t understood a lot of things. So I am taking the course again and trying to make a short program after each lesson. I just started module about OOP and made a program, where you can mix up to 3 different liquids to make yourself a custom cocktail. It works fine, but I hit a tiny problem. I want to ask user 3 times for 3 different things. The outpus should look like this: You can have up to 3 diferent liquids in your cocktail. Type "1","2" or "3". The given slot will get overwritten. <USER INPUT> Type liquid - for example "soda". <USER INPUT> Type volume in mililitres - for example "150" <USER INPUT> But it firstly prints all the instructions and THEN it acceptsuser input 3 times. Can you tell me where did I make a mistake?

12th Mar 2018, 1:04 PM
Jan Štěch
Jan Štěch - avatar
6 odpowiedzi
0
Problematic part of my program: System.out.println("You can have up to 3 diferent liquids in your cocktail. Type \"1\",\"2\" or \"3\". The given slot will get overwritten."); Scanner n = new Scanner(System.in); System.out.println("Type liquid - for example \"soda\""); Scanner lq = new Scanner(System.in); System.out.println("Type volume in mililitres - for example \"150\""); Scanner vl = new Scanner(System.in);
12th Mar 2018, 1:04 PM
Jan Štěch
Jan Štěch - avatar
0
Thank you very much. This solved another my problem too.
12th Mar 2018, 1:23 PM
Jan Štěch
Jan Štěch - avatar
0
I have one more problem. When I write String input = new Scanner(System.in).next(); it throws me warning: Resource leak '<unassigned Closeable value>' is never closed! What can I do about this? I have the same problem alway, when I use Scanner.
12th Mar 2018, 2:00 PM
Jan Štěch
Jan Štěch - avatar
0
String input = new Scanner(System.in).next();
12th Mar 2018, 2:43 PM
Jan Štěch
Jan Štěch - avatar
0
import liquidMixer.*; import java.util.Scanner; public class program { public static void main(String[] args) { int lqdPrint; Glass glss = new Glass(); while(true) { System.out.println("What do you want to do?"); String input = new Scanner(System.in).next(); switch(input) { case "addLiquid": System.out.println("You can have up to 3 diferent liquids in your cocktail. Type \"1\",\"2\" or \"3\". The given slot will get overwritten."); int n = new Scanner(System.in).nextInt(); System.out.println("Type liquid - for example \"soda\""); String lq = new Scanner(System.in).next(); System.out.println("Type volume in mililitres - for example \"150\""); int vl = new Scanner(System.in).nextInt(); glss.setLiquid(n, lq); glss.setVolume(n, vl); System.out.println("Liquid number "+n+" has been changed to "+vl+" of "+lq); continue; case "printLiquid": System.out.println("Which liquid do you want informations about?"); Scanner lqPrint = new Scanner(System.in); lqdPrint=lqPrint.nextInt(); glss.printLiquid(lqdPrint); continue; case "printContent": glss.printContent(); continue; case "help": System.out.println("Possible options: addLiquid, printLiquid, printContent, help, exit"); continue; case "exit": break; default: System.out.println("Invalid option. Possible options: addLiquid, printLiquid, printContent, help, exit"); continue; } break; } } }
12th Mar 2018, 2:47 PM
Jan Štěch
Jan Štěch - avatar
0
OK, thank you.
12th Mar 2018, 3:00 PM
Jan Štěch
Jan Štěch - avatar