Here is my case:
So far my group and I, managed to read information from an external file and place it in a JTable. But we need an update button. So we guess we should take all the information from JTable after editting something inside it, and replace it with the current information in the same file. So we kind of think we have to overwrite the old file.
So far we got this: (for int i... is a part of the code but can't get it inside the grey area :P)
for(int i = 0; i < model.getRowCount(); i++) {
p += model.getValueAt(i, 0) + " "
+ model.getValueAt(i, 1) + " "
+ (Integer) model.getValueAt(i, 2) + " "
+ model.getValueAt(i, 3) + " "
+ (Integer)model.getValueAt(i, 4) + " "
+ model.getValueAt(i, 5) + " "
+ model.getValueAt(i, 6) + " "
+ model.getValueAt(i, 7) + " "
+ (Integer)model.getValueAt(i, 8) + "\n";
}
// Update File
SaveMember sm = new SaveMember();
sm.update(p);
Inside our SaveMember.java we got:
public void update(String x) throws Exception {
File f = new File("Members/Members.txt");
PrintStream output = new PrintStream(f);
output.print(x);
So by now when we go and change the data and press the button update, it doesn't do anything at all, and doesn't replace the old data with the new.. Thanks for reading! :)
I'm not sure. If you have double checked that your code is executed at all (maybe you forgot to attach the ActionListener to your button - we all do that from time to time...) try flush the output stream and close the stream afterwards.
First check if your code in the for loop is executed at all. Set a breakpoint after the for loop and inspect the string p. If you are not familiar with debugging, print the string to the console with System.out.println(p).
If your code is NOT executed: Check why the method your code is in is not called. Perhaps you forgot to attach an action listener to your update button or the action listener has an early return under some circumstances.
If your code is executed: What do you do with the exception that is thrown by the method update? Make sure to log it with your logger or print it to the console (again via System.out.println(exc)). If you get a FileNotFoundException the path to the file is not correct.
Related
I am currently trying to making a custom rules plugin (for minecraft) and I am trying to see if the player has something activated which I stored in the config file. It is in the listener class (which calls the config from the main). Here is my code:
#EventHandler
public void onEvent(AsyncPlayerChatEvent e) {
Player player = e.getPlayer();
if (config.getInt("EditingLine." + player.getName().toLowerCase()) == 1) {
int line = 0;
try {
line = Integer.parseInt(e.getMessage());
} catch (Exception b) {
player.sendMessage(ChatColor.RED + "Invalid Number.");
config.set("EditingLine." + player.getName().toLowerCase(), 0);
}
if (!(line == 0)) {
config.set("EditingLine." + player.getName().toLowerCase(), 0);
config.set("EditingText." + player.getName().toLowerCase(), 1);
e.setCancelled(true);
player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Custom Rules" + ChatColor.GRAY + "]" + ChatColor.GREEN + " Enter the text you would now like on that line.");
}
}
}
The, config.getInt() function in the if then statement currently returns nothing. This may be happening because the config in the Listener Class is actually calling a custom made config, called 'playerdata.yml' and not the actual 'config.yml'. If there is any easier way to write this script, also let me know. I'm trying to make this as simple as I can.
The answer has been solved by merging my two configuration files together.
I want the "Module Code = " and "Result = " to be separated by a tab but whenever I run the code below it literally just outputs
"Module Code = Biology\tResult = 40.0"
public String toString()
{
return "Module Code = " + moduleCode + "\t" + "Result = " + result;
}
The problem is that you're viewing the value of the produced string in the BlueJ window. That window is good for debugging purposes, but it won't exhibit the same behavior that a proper output device would, especially with respect to characters such as newline, tabulation, etc. Those characters will still appear with their escape sequences, just like you typed them in your source code.
In other words, your toString() method is fine and it works as intended. If you want to see its results formatted properly, don't view them using BlueJ -- print them somewhere else. The console is a good choice:
System.out.println(module.toString());
Why won't “\t” create a new line?
well, that is because “\t” is a tabulation not a new line “\n”
if you need a new line try instead
return "Module Code = " + moduleCode + "\n" + "Result = " + result;
How can I automatically print without poping up dialog box or automatically accept print dialog? Here is some of my code:
if ("OUT".equals(rs.getString("empattendance"))) {
String date = dft.format(dNow);
String time = tft.format(dNow);
textArea.setText(date + "\n" + "\n" +
fullname +"\n" +
"Time In: " + time + "\n" +
"Status: "+ statusin +
"\n" +
"\n" +
"____________________\n" +
" Sign by Supervisor");
try {
//printing
Boolean complete = textArea.print();
if(complete){
}
else{
}
} catch (PrinterException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
and here's the screenshot of the current behaviour.
thanks
When I look at your code I have few thoughts before answer.
1) Do not use String. Better for comparing stuff is Enumerators I believe.
2) If you would like to set text to textArea previously create some method using StringBuilder for example which will be creating the String you would like to set. Joshua Bloch says
Item 15: minimize mutability (...) If a client requires performing expensive multi-stage operations on your class, expose them as primitive methods, or provide a mutable companion class (like StringBuilder for String).
And take a look at this topic for more.
3) To print data from textArea if I were you I would try to use this.
I believe that would help you
I'm writing a folder synchronization app. Currently, I'm working on the part responsible for recursively going through the two user-specified directory structures, comparing the folders and files in them to the other structure, and displaying whether each file or folder is unchanged, changed, or new, by means of a colored dot. The problem is that the program in its current state, while evaluating the relation correctly, only displays the dot on one TreeItem per dot color instead on all of them. Pic for reference:
Whats causing this? I have a suspicion that it has to do with the way object assignment works in Java, so I'm reassigning one and the same object somehow to all the proper TreeItems, only stopping at the last one, but that's too broad to work with. See the offending function below.
private void compareAndFillInSourceTreeView(Path x, TreeItem root) throws IOException {
String xSourceName = x.getName(x.getNameCount() - 1).toString();
String xTargetName = (getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1))
.toString();
System.out.println("-----------------------------------------------------------------------------------------");
System.out.println("NEW CALL: " + x.toString() + " " + root);
System.out.println("EQUIVALENT: " + getEquivalentFileInTarget(x) + " EXISTS: " +
getEquivalentFileInTarget(x).toFile().exists());
System.out.println("IS NEW: " + xTargetName + ", " + (xTargetName == null));
System.out.println("UNCHANGED: " + x + " " + getEquivalentFileInTarget(x) + " NAMES: " + xSourceName + ", "
+ xTargetName);
System.out.println("CHANGED: " + ((x.getName(x.getNameCount() - 1)) ==
getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1)));
if (x.toFile().isFile()) {
System.out.println("THIS IS A FILE: " + x.toString());
//if new, i.e. doesn't exist in the target
if (!getEquivalentFileInTarget(x).toFile().exists()) {
System.out.println("EQUIVALENT DOESN'T EXIST FOR THIS FILE IN TARGET");
TreeItem newBranch = makeBranch(xSourceName, root);
newBranch.setGraphic(blueDotIcon);
}
//if unchanged
else if (sameContents(x, getEquivalentFileInTarget(x)) && (xSourceName.equals(xTargetName))) {
System.out.println("THIS FILE AND ITS EQUIVALENT ARE EQUAL");
TreeItem newBranch = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
newBranch.setGraphic(greenDotIcon);
}
//if same name, but different contents, i.e. changed
else if ((x.getName(x.getNameCount() - 1)).equals(
getEquivalentFileInTarget(x).getName(getEquivalentFileInTarget(x).getNameCount() - 1))) {
TreeItem newBranch = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
newBranch.setGraphic(yellowDotIcon);
} else {
System.out.println("BAD, putInTreeView() Error, it should never reach this line");
System.out.println("Error log: " + x + ", " + getEquivalentFileInTarget(x));
}
} else if (x.toFile().isDirectory()){ //if it's a folder, checked explicitly because it's behaving weird
System.out.println("THIS IS A DIRECTORY: " + x.toString());
if (getEquivalentFileInTarget(x).toFile().exists()) {
System.out.println("EQUIVALENT EXISTS FOR THIS DIRECTORY IN TARGET.");
//make new branches and mark them as existing folders
TreeItem currentSourceTreeViewRoot = makeBranch(x.getName(x.getNameCount() - 1).toString(), root);
currentSourceTreeViewRoot.setExpanded(true);
currentSourceTreeViewRoot.setGraphic(greenDotIcon);
for (File i : x.toFile().listFiles()) {
System.out.println("Rec. called for: " + currentSourceTreeViewRoot);
compareAndFillInSourceTreeView(i.toPath(), currentSourceTreeViewRoot);
}
} else {
System.out.println("EQUIVALENT DOESN'T EXIST FOR THIS DIRECTORY IN TARGET.");
//if they don't exist, make the branches anyway and mark them as representing nonexistent folders
TreeItem currentSourceTreeViewRoot = makeBranch((x.getName(x.getNameCount() - 1)).toString(), root);
currentSourceTreeViewRoot.setExpanded(true);
for (File i : x.toFile().listFiles()) {
System.out.println("Rec. called for: " + currentSourceTreeViewRoot);
compareAndFillInSourceTreeView(i.toPath(), currentSourceTreeViewRoot);
}
}
}
}
Your assumption is correct. When assigning a graphic with setGraphic you are telling JavaFX where in the scene graph to locate this node. When you call setGraphic again with the same object as parameter you are effectively moving it to a different place in the scene graph.
Create a new dot/circle for every item and your problem should be solved.
I am using following code for local storage.
for(int i=0; i< files.length; i++)
{
System.out.println("base = " + files[i].getName() + "\n i=" +i + "\n");
AudioFile f = AudioFileIO.read(files[i]);
Tag tag = f.getTag();
//AudioHeader h = f.getAudioHeader();
int l = f.getAudioHeader().getTrackLength();
String s1 = tag.getFirst(FieldKey.ALBUM);
out.print("writeToStorage("+s1+","+s1+");");
}
getting uncaught syntex erroe: unexpected identifer as a error.
Im guessing you meant java rather than javascript?
Your unexpected identifier is here out.println you need System. infront of it.
The reason for this is that out is not defined in your code. You need to access it by using the static variable in the System class. Hence why you use System.out.
Alternatley you could set a variable out to be equal to System.out for shorthand, although I don;t tend to. But this can allow you to switch out to a different type of output stream without having to refactor your code much.
Have you added following ?
import static java.lang.System.out;
Probably you need to output "s in the last line to surround the s1 values.
"writeToStorage("+s1+","+s1+");"
->
"writeToStorage('"+s1+"','"+s1+"');"
Btw for the same reason you have to fix the other line too:
"base = " + files[i].getName() + "...
->
"base = '" + files[i].getName() + "'...