r/javahelp • • Jun 17 '18

Java

Hi guys, two quick questions 1) how do I create a text UI in Java ? 2) how do I create a collection ? I cannot find anything relevant to this topic on the internet (found for Java, but not accurate, or not compatible w/ Netbeans). Thanks so much for help ! :)

0 Upvotes

7 comments sorted by

View all comments

1

u/96-62 Jun 17 '18

Collection is an abstract class for lists, sets and so on. So:

Collection<String> mySet = new HashSet<String>();

is as valid as

Collection<String> myList = new ArrayList<String>();

Collection itself cannot be instantiated, but that makes sense, it's just means "sets, lists and whatever else".

In terms of a text UI, what do you have in mind? You can use a Scanner and System.out.println to send data to the terminal, but that only works if you run the java program from the command line.

1

u/Irocidar Jun 17 '18

Thanks for replying. Your version of the collection really helped me, I couldnt find it anywhere. In the terms of a text UI, I have a System.out.println which should send the data to the terminal, but it doesnt. I have for example, if you press 1, s.o.p "xxx", but if I press it, nothing happens. I know that the problem lies in connection of our world to the virtual i.e - when I press the keyboard it should print out what I want but I have it wrongly computed. Any ideas of how to compute this ? Really sorry, I know that my knowledge about java is really basic, but thats because I am studying it only for the first year - still on highschool..

1

u/96-62 Jun 17 '18

What code did you use to read the input in? Does it need you to enter a line before it responds - 1 followed by enter maybe?

1

u/Irocidar Jun 18 '18

you dont need to enter anything. I have a sorted data system about a certain collection of cars and trucks in my netbeans project. these cars and trucks are a subclass of class vehicles - therefore vehicle is the superclass. Now i just need a text UI (via s.o.p) which would show me something like this : Press a number according to your choice

  1. List of all vehicles

  2. List of all Cars

  3. List of all Trucks

The problem is that when I put in the number, it doesnt do anything

1

u/96-62 Jun 18 '18

Try using a Scanner for input.

Scanner myScanner = new Scanner(System.in);

then myScanner.nextInt();