I added that parenthesis and still had the same problem, no worries I just went ahead and used the method you showed me. Thanks againfor the help
KirkPatrick 28 Junior Poster
Oh wow, I was thinking of it in a completely different way for the array and for the switch statement. I retract my previous statement :) as you have proven me wrong. Thanks for showing me that, could come in handy.
Any suggestions as to why its saying I can't set it up with my .getSelectedIndex() ?
KirkPatrick 28 Junior Poster
If your code is an ActionL:istener, you're probably listening for the wrong event to see if the selection has changed. You probably need an ItemListener.
I went ahead and tried it for my itemListener with the same luck. This may be a solution to a question later in the situation, but for right now its some sort of syntax error. (its supposed to be .getSelectedIndex(int) but that didn't work which is why I tried .getSelectedIndex == 0)
I apologize as I thought I mentioned that it is a syntax error, and that it doesn't actually compile. I thought I had typed that, but looking back on my post, I didn't.
There is no condition on an "else" clause, so this isn't valid syntax
Indeed, was typing on a whim. That part has been fixed now.
The bigger question is why not use a switch or even better yet, an array of text fields so you don't have so much manually repetitive code.
I was typing it quickly to have an example as it seemed like a logical way to do it. Also if I'm not mistaken a switch statement would be nearly as many lines of code. As for an array of text fields, all in all it would still be just about as many lines of code but it would look cleaner to do it that way.
I'm not sure why I'm off on this one. I think I'm aiming a bit too much toward …
KirkPatrick 28 Junior Poster
Just had a quick question about my code. Seems like a very simple solution, perhaps I'm just too sleepy to think straight.
What I am wanting to do is if a certain index is selected in my combobox i want certain textfields to display which is apparent by the code:
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jComboBox1.getSelectedIndex == 0) {
aTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 1) {
aTextField.setVisible(true);
bTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 2) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 3) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
dTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 4) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
dTextField.setVisible(true);
eTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 5) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
dTextField.setVisible(true);
eTextField.setVisible(true);
fTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 6) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
dTextField.setVisible(true);
eTextField.setVisible(true);
fTextField.setVisible(true);
gTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 7) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
dTextField.setVisible(true);
eTextField.setVisible(true);
fTextField.setVisible(true);
gTextField.setVisible(true);
hTextField.setVisible(true);
}
else if (jComboBox1.getSelectedIndex == 8) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
dTextField.setVisible(true);
eTextField.setVisible(true);
fTextField.setVisible(true);
gTextField.setVisible(true);
hTextField.setVisible(true);
iTextField.setVisible(true);
}
else (jComboBox1.getSelectedIndex == 9) {
aTextField.setVisible(true);
bTextField.setVisible(true);
cTextField.setVisible(true);
dTextField.setVisible(true);
eTextField.setVisible(true);
fTextField.setVisible(true);
gTextField.setVisible(true);
hTextField.setVisible(true);
iTextField.setVisible(true);
jTextField.setVisible(true);
}
}
Perhaps I'm just not paying attention close enough, but shouldn't that code work?
KirkPatrick 28 Junior Poster
Hey guys, I was having a bit of trouble with making my GUI look presentable (not too busy or crammed up) and I have a few questions that shouldn't be too hard to answer.
I'll briefly explain what I have at the moment.
I have a total of 15 text fields that have information in them and when I click a submit button it adds it to the table. I then can click save once all my data is entered into the table and it will save my table data into a csv file. The problem with this is my table gets all crammed and messy with 15 columns. So I was wondering two things:
1) Is it possible to add 6 columns while the last column is a drop down menu that takes care of the last 10 textfields data which would be entered upon the submit button?
2) Or would it be possible to create a second table that would display the other half the information but also save to the csv file correctly. Ex: first table would have textfield data 1-8 and second table would have textfield data 9-15 but it would all save on one line in the csv file?
I believe both are possible, but I'm not quite sure about how to go about it.
A side note is that I'm using CSVReader/writer to save my table data to csv.
KirkPatrick 28 Junior Poster
Well one problem is that I won't know the overall size of the array.
Also if I'm not mistaken wouldn't ints = list.toArray(); also throw a cast exception for incompatible types?
Sorry I am still a bit 'new' to java and don't have much experience under my belt.
KirkPatrick 28 Junior Poster
I have been having a little trouble getting my program to output the results I am wanting.
At first I was having trouble with a cast exception at this line:
out = (ParseResult[]) ipHostsFound.toArray();
The cast exception was:
[Ljava.lang.Object; cannot be cast to [Lfilelocation.struct.ParseResult;
So I ended up changing it to this:
out = (ParseResult[])ipHostsFound.toArray(new ParseResult[ipHostsFound.size()]);
However, I don't want the size of it, i just changed it to that to see if I could get it to work correctly with the output as I was briefly reading some information here:
Here is the source (if you need the ParseResult source I can post it also):
import filelocation.struct.ParseResult;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
/**
*
* @author ******
* Scans the network for computers connected
*/
public class AngryIPParser {
private String ip = "xx.xx.xx.xx";
private String hostname = "host";
private String ping = "m/s";
public ParseResult[] parseResult(BufferedReader reader) throws FileNotFoundException, IOException {
ParseResult[] out;
ArrayList<ParseResult> ipHostsFound = new ArrayList();
try {
boolean bHeadersDone = false;
while (reader.ready()) {
String beanInfo = reader.readLine();
if (!bHeadersDone) {
if (beanInfo.contains("Ping")) {
bHeadersDone = true;
}
}
else {
String[] values = beanInfo.split("," , 3);
ip = values[0];
hostname = values[1];
ping = values[2];
}
}
} catch (Exception e) {
e.printStackTrace();
}
out = (ParseResult[])ipHostsFound.toArray(new ParseResult[ipHostsFound.size()]);
// compiles without errors/exceptions but doesn't function as it should
// out = (ParseResult[]) ipHostsFound.toArray(); <-- comes up with cast exception
System.out.println(out);
return out; …
KirkPatrick 28 Junior Poster
Aw I see what I did, when I had removed the "for (String str : values) like you mentioned the first time, I had placed my print statement outside of the loop.
Thank you for clearing that up bud, I appreciate your help.
As for the compatible pierce of code, I apologize for not posting the full part in the beginning. Many forums ask that you not post the full thing as they don't want 300+ lines filling the page. While my code wasn't quite that long I figured it was along the same concept. But I'll keep that in mind when posting in the future.
Thanks again to both of you two for replying (~s.o.s~ and JamesCherrill)
(im going to remove the source in my post above as its not necessary to keep up there and its only taking up space)
*Edit*
Seems it won't allow me to edit that previous post for some reason :s
~s.o.s~ commented: BTW, welcome to Daniweb. Feel free to introduce yourself in the community introductions in case you haven't done so. :-) +28
KirkPatrick 28 Junior Poster
Alright here is the code at the most basic (i've removed some information but its not necessary for the code to function)
Also note that I have my BufferedReader set up elsewhere and it looks like this:
BufferedReader reader = new BufferedReader(new FileReader("C:\\results.csv"));
Here is the code I have as of now:
import location.ParseResult;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class MyParser {
private String ip = "xx.xx.xx.xx";
private String hostname = "host";
private String ping = "m/s";
public ParseResult[] parseResult(BufferedReader reader) throws FileNotFoundException, IOException {
ParseResult[] out;
ArrayList<ParseResult> ipHostsFound = new ArrayList();
try {
boolean bHeadersDone = false;
while (reader.ready()) {
String beanInfo = reader.readLine();
if (!bHeadersDone) {
if (beanInfo.contains("Ping")) {
bHeadersDone = true;
}
}
else {
String[] values = beanInfo.split("," , 3);
for (String str : values) {
ip = values[0];
hostname = values[1];
ping = values[2];
if (ping.equals("[n/a]") || hostname.equals("[n/a]") ) {
//ignore this as I haven't implemented it yet
}
else {
//ignore this for now as I haven't implemented it
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
out = (ParseResult[])ipHostsFound.toArray(new ParseResult[ipHostsFound.size()]); // compiles without errors/exceptions but doesn't function as it should
return out;
}
/**
*
* @return hostname of the parsed machine
*/
public String getHostname() {
return hostname;
}
/**
*
* @return ipaddress of the parsed machine
*/
public String getIP() {
return ip;
}
}
And I can't post the results.csv file as it has information that I …
KirkPatrick 28 Junior Poster
where do you try to print ip etc. Is it inside the loop?
Inside the quote it produces all the information, but duplicated by 3.
Outside the loop it produces only the last instance of what I want printed out.
> it duplicates the information its printing out by 3
Because you print out the information in a loop. The solution here is pretty simple; split the string read from the file/stdin using the given delimiter. If the length of the resulting array is not equal to 3, it means that the input wasn't in the format required. If the array length is 3, print out the details using:
String[] arr = str.split(","); // assert length of the array is exactly 3 System.out.println(arr[0] + ", " + arr[1] + " and " + arr[2]);
The array length will always be 3 as I have set the program recording the information only to record 3 fields.
However, I went ahead and tried your example and it is still printing out duplicates of 3.
Also a side note, I want to keep my ip, hostname, and ping equal to each array column as I am not interested in printing these out (its just to test it to make sure it will properly do what I want it to do later) I have a viewobject that will be displaying each of those fields.
My ultimate assumption is that if it duplicates 3 times when I am just printing it …
KirkPatrick 28 Junior Poster
I am reading a .Csv file and am having problems trying to get the information I want, set up the way I want.
Here is where my problem comes in:
String[] info = beanInfo.split("," , 3);
for (String str : info) {
ip = info[0];
hostname = info[1];
ping = info[2];
}
Basically I am wanting to set column 1 to my ip information, column 2 to my hostname information, and column 3 to my ping information.
Whenever I attempt to print out any of the following strings ip, hostname, or ping; it duplicates the information its printing out by 3.
So basically if I do:
System.out.pringln(ip);
It will print out:
192.168.0.0
192.168.0.0
192.168.0.0
Instead of just:
192.168.0.0
If I print str, then it prints the information off exactly the way I want it just not by the columns I want it to be recognized with. This is what it would look like if I print out str:
192.168.0.0
myHostname
2 m/s
192.168.0.1
myHostname2
2 m/s
192.168.0.2
myHostname3
1 m/s
and so on...
So did I misunderstand the point of the 'number of fields' portion after my delimiter when splitting the string?
KirkPatrick 28 Junior Poster
Before going into the explanation I will list what I am wanting to do in order:
- Have main() read results.csv and create beans according to the lines of data in the file
- Have my beans set up correctly (getters, setters, and propertychanges)
To give you guys a quick brief on what I am doing: I am running a program called Angry IP Scanner through my java application. What it does is records the information I want and saves it to a csv file called results.csv.
I'm going to try and explain this as clear as possible so that you guys won't get confused. I'll be using illustrations to help out. (i noticed that image tags are disabled, if they are not allowed I'll remove them from my post)
Main() reading results.csv
What I am wanting to do is have my main() open up my results.csv (this will contain data that should be displayed on the bean.) and I want it to read the amount of lines (after the header of course, i'm just wanting the data angy IP picked up or will pick up) . Once it has read the amount of lines located in the results.csv I want it to create the same amount of beans as there are lines. Below I will illustrate what I am talking about.
Here is the program at full (an example of what im wanting it to be without the data):
[img]http://img80.imageshack.us/img80/8343/program.png[/img]
…