I'm listing all MIDI devices in a combobox, but for the loopback drivers I get duplicate entries.
The first entries don't work and contain no description (
getDescription() returns "No details available" )
The others, which are the working ones, return "External MIDI Port"
description.
Now, I would like to know why rtpMidi, LoopBE, LoopMidi and all loopback drivers cause these duplicate entries, so that I can exclude them from the list in a secure way.
I could simply avoid the entries without a valid description, but I don't like acting without knowing the reason why those entries are reported in the first place.
Another thing that puzzles me is that if I try filtering the results via "if(dev instanceof Receiver)" I get a blank list, the same with instanceof Transmitter, Synthesizer and Sequencer. So it seems like none of the midi devices are instance of one of those classes, but only instance of MidiDevice class, which doesn't help me with filtering the list...
Could someone please suggest a different solution?
// Get MIDI device list
info = MidiSystem.getMidiDeviceInfo();
device = new ArrayList<MidiDevice>();
deviceDetails = new ArrayList<String>();
int j=0;
for (int i = 0; i < info.length; i++) {
MidiDevice dev = MidiSystem.getMidiDevice(info[i]);
//if ( dev instanceof Receiver ) { // tried Receiver, Transmitter, Synthesizer and Sequencer
// Detailed List
deviceDetails.add("Device ID: " + j);
deviceDetails.set(j, deviceDetails.get(j) + "\nName: " + info[i].getName());
deviceDetails.set(j, deviceDetails.get(j) + "\nDescription: " + info[i].getDescription());
device.add(dev);
deviceDetails.set(j, deviceDetails.get(j) + "\nDevice: " + device.get(j));
//Combo Box (Dev names only)
MidiOutCombo.add(info[i].getName());
j++;
//}
}
Here's the temporary solution I found, it works but it's string-based and I don't know if it works crossplatform...
if (info[i].getDescription().compareTo("External MIDI Port") == 0 ) {
Loopback drivers typically have two ports, one for receiving, and one for transmitting.
To determine if a MIDIDevice has any receivers or transmitters, you must call its getMaxReceivers/getMaxTransmitters methods.
Related
I've decided to programm a search system for finding students and teachers in a school via GUI. It is an OOP and need some tweaking here and there, but there is one issue which doesn't seem logical to me. When I'm searching for a teacher, I have to type there name or surname into a JTextField and press the Search button which runs a method that loops through an ArrayList of teacher-objects and checks if their names match with the one in the Textfield. Then it checks if these teachers have multiple subjects and grades and it goes through nested if-statements. After the teacher is found, their information is displayed on a GUI with several Texfields. Theoretically if the name I typed into the TextField doesn't match one from the teacher objects, a Error Message should pop-up saying the teacher I'm looking for isn't in the system. But even though I type in the correct name and get all the information displayed, it sends me to the Error Message everytime. I tried to fix it with a break statement but that didn't work either. Can someone please help me with this.
Here is the code I'm talking about:
public void lehrerSuche()
{
String lehrername = tfSuchfeldLehrer.getText();
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++)
{
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername))
{
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
break;
}
else
{
switchPanels_3(panelErrorLehrer);
}
}
}
I've uploaded my project to GitHub. Methods and variables are written in German, so I'm really sorry if you can't understand what I have written. If u have questions please hit me up. I use Eclipse to code.
This link should direct you to my GitHub:
https://github.com/Gonzo-CR/Home-Projects.git
If the link doesn't work, look for Gonzo-CR on GitHub and check out my Home-projects repository where I uploaded all the files.
For better undestanding these are the Object oriented classes:
Person(Abstract)
Schueler
Lehrer
Fach
Schulklasse
Spezial
Sprecher
GUI classes:
Suchsystem
Testdaten(A class which generates all my objects)
The problem is likely that if td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) is not true the very first time the loop runs, switchPanels_3(panelErrorLehrer); will be triggered - regardless of whether the condition is met on a later iteration of the loop.
What you need is to check a sentinel value after the loop finishes - e.g.:
bool lehrerGefunden = false;
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++){
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername)){
//etc.
lehrerGefunden = true;
break;
}
}
if(!lehrerGefunden){
switchPanels_3(panelErrorLehrer);
}
That way, you check every entry in the list before deciding whether to show the error.
i am facing the problem how to retrieve the value from ArrayList. I pass in the value with this code to remove duplicate:
Set<List<String>> allTCP=new LinkedHashSet<List<String>>();
for(int x=0; x < TCPSourceIP.size();x++){
allTCP.add(Arrays.asList(new String[]{TCPSourceIP.get(x),TCPSourcePort.get(x),TCPDestIP.get(x),TCPDestPort.get(x)}));
}
But now the problem i facing is i do not know how to retrieve the value. The value i pass in is 10.1.1.1, 123, 10.1.1.2, 234 If i use System.out.println(allTCP), then i cannot use each of the value inside the List.I need to get the single value from the array to do proper output such as, The TCP Source IP is 10.1.1.1 with port 123 to Destination IP 10.1.1.2 with port 234. Previously i use this code to do output but it did not remove duplicate.
for(int x=0; x < TCPSourceIP.size(); x++){
jTextArea1.append(x+1+")IP " + TCPSourceIP.get(x) +" is sending packet using TCP Port "+
TCPSrcPort.get(x) + " to IP " + TCPDestIP.get(x) + "(" + TCPDestPort.get(x) +")" +
" which is non-standard port\n");
Can any one give me suggestion on how to retrieve the value from the ArrayList in order for me to do a proper output. Help would be appreciated.
In cases like that I prefer to have the values encapsulated in to an object with getters/setters and put instances of those to the lists / hashes:
class Route {
private String sourceIp;
private Long sourcePort;
private String destIp;
private Long destPort;
public Route(Sting src, Long sport, String dest, Long dport) {
setSourceIp(src);
...
... etc.
And then
ArrayList<Route> allTcp = new ArrayList<Route>();
...
allTcp.add(new Route(src, sport, dst, dport) );
This concept would then enable an easy way to find any source, destination, port etc. by putting the complex comparison methods inside class Route.
Any changes to the inner set of Route doesn't interfere with the caller(s).
What i am shooting for is to be able to take the input from my text field in my GUI and put it into an ArrayList. Then I need to check the array and see if I already have the Integer in the array. If so I need to remove both Integers so the Integer is no longer inside the array. I also need to be able to add the Integer if it is not a duplicate to the ArrayList.
The purpose of this is to be able to have users sign in with a number. The vision I have is for them to put their unique number in when they sign in or sign out (Like a time clock). If their number is not in the array, they are signing in. If their number is in the arraylist then they are signing out.
This is what i have for code so far, i am getting a problem with signing out. It keeps sending the second value in the array back as the only one able to sign out. I have tried fixing it and can't seem to figure out what is wrong. Let me know if it would be more helpful if i posted my whole program or if this code snippet is enough to figure it out.
Thanks,
private void btnSignInActionPerformed(java.awt.event.ActionEvent evt) {
// Get data from form and put it into an Array List
Integer txtUserSignInName = Integer.valueOf(txtUserSignIn.getText());
ArrayList<Integer> userSignInNumber = new ArrayList();
userSignInNumber.add(12345678); //sample data
userSignInNumber.add(55489563); //sample data
userSignInNumber.add(26489564); //sample data
userSignInNumber.add(78654865); //sample data
userSignInNumber.add(txtUserSignInName);
// Setting up HashSet so no duplicate data
Set<Integer> hashSet = new HashSet<>();
hashSet.addAll(userSignInNumber);
userSignInNumber.clear();
userSignInNumber.addAll(hashSet);
// Other settings needed
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss a");
String time = df.format(new Date());
if ((txtUserSignIn.getText() != null && txtUserSignIn.getText().isEmpty())) {
String userSignInErrorMessage = "Sorry, Please Try Again";
JOptionPane.showMessageDialog(new JFrame(), userSignInErrorMessage, "Incorrect Sign In",JOptionPane.ERROR_MESSAGE);
//setting focus
txtUserSignIn.setText("");
txtUserSignIn.requestFocus();
} else {
for(int i = 1; i < userSignInNumber.size(); i++) {
// If number is already in array, remove it
if(txtUserSignInName.equals(userSignInNumber.get(i))) {
userSignInNumber.remove((Integer)txtUserSignInName);
System.out.println(txtUserSignInName + " has signed out");
txtLoggedInUsers.append(txtUserSignInName + " has signed out at " + time + "\n");
break;
} else { // If number is not in the array, add it to the array
System.out.println(txtUserSignInName + " has signed in");
txtLoggedInUsers.append(txtUserSignInName + " has signed in at " + time + "\n");
break;
}
}
System.out.println(userSignInNumber);
}
}
Lists are not really suitable for what you are trying to do. You would be better off using one of the Set interface implementations. Sets provide fast contains()/add()/remove() methods that you can use, without generally having to iterate over all elements manually in a loop. And if you do need to quickly iterate over all elements for some reason, then a LinkedHashSet would work just fine. For example:
Set<Integer> signedIn = new LinkedHashSet<>();
...
if (signedIn.add(loginNumber)) {
// The set was modified, therefore this is a new login
} else {
// The number was already present, log-off the user.
signedIn.remove(loginNumber);
}
...
if (signedIn.contains(loginNumber)) {
// Allow the user to...
} else {
// Error, user not signed in
}
There are a number of problems with your implementation.
The loop starts at 1 when array indices start at 0, so you are skipping the first entry.
Then you always break out of the loop straight away after checking the 2nd item and so don't check any others.
A better approach would be to use a Set, not just to remove duplicates, but as a primary way to store who is signed in. Then you can check if a number is in the set easily with the contains method so you don't need to loop through manually (which means you don't have to deal with the problems associated with removing items from a list while you are looping through it).
I'm working on j2me project that involves getting a list of users from an online database, I then intend to populate a list with the names of the users and the number can be very large. my question is - are there limits to the number of items you can append to a list?
HttpConnection hc = (HttpConnection);
String reply;
Connector.open("http://www.xxxxxxxxxxxx.com/......?xx=xx");
InputStream is = new hc.openInputStream();
int ch;
// Check the Content-Length first
long len = hc.getLength();
if(len!=-1) {
for(int i = 0;i<len;i++)
if((ch = is.read())!= -1)
reply += (char) ch;
} else {
// if the content-length is not available
while ((ch = is.read()) != -1)
reply += (char) ch;
}
is.close();
hc.close();
DataParser parser = new DataParser(reply); // This is a custom class I created to process the XML data returned from the server to split it into groups and put in an array.
List user list = new List("Users");
if (parser.moveToNext()) {
do {
list.append(parser.get(), null);
}
}
This code seems to be working fine but my problem is, if a keep calling list.append("", null), will it get to a point when some exception is thrown, maybe in the case of 50,000 names (list items)?
Their is no limitation to number of items in a list. You can as well use stringItems appended to form, then add item commands to them... I hope this helps.
J2ME tutorial at http://www.tutorialmasterng.blogspot.com
Some implementations may have limit. Older Sony Ericsson phones have limit of 256 items in the list. Anyway, as Meier pointed out, lists with really many items can be slow or difficult to use. And 50k of strings may easily cause OOM on low heap devices (1 - 2 MB).
I am doing a project which i have to load a list of flights details from a text file. I read the text file and have load the 3 values into a hashmap. The 3 values are in this format (Airport ID, To, From). The To and From are being put into a list before putting into hashmap together with the ID.
I am having trouble with finding all possible routes from a selected To and From. I have read up on Dijkstra's algorithm but i did not know how to apply this due to lack of knowledge.
Below is an example of my code which i am able to find the direct flight and flight with 1 transfer point.
for (int i = 0; i < route.get("all").size(); i++) {
String boardAir = route.get("all").get(i).from;
String alightAir = route.get("all").get(i).to;
if (boardAir.equals(ar.boardAirport) && alightAir.equals(ar.alightAirport)) {
airline = route.get("all").get(i).id;
System.out.println("Direct Airlines = " + alr.airline1.get(airline));
System.out.println("From = " + ar.airport1.get(boardAir) + "\tDestination = " + ar.airport1.get(alightAir));
System.out.println();
} else {
System.out.println("No direct flight found.");
}
if (boardAir.equals(ar.boardAirport)) {
for (int j = 0; j < route.get(route.get("all").get(i).id).size(); j++) {
String transfer = route.get(route.get("all").get(i).id).get(j).from;
String finalDest = route.get(route.get("all").get(i).id).get(j).to;
}
}
}
Dijkstra's algorithm would be a good algorithm to study. If you read up on it, and are still having trouble, there are some additional resources I would suggest. First, there is a pretty good Algorithms book from Princeton University that is completely online. You can find that at http://algs4.cs.princeton.edu/home/ and the chapter you should reference is Chapter 4. It comes complete with sample code and I think it will provide enough information for you. Otherwise, check out a YouTube search for "Dijkstra's Algorithm" if you are more of a visual learner. There are actually some good vids up there.