Wednesday, May 2, 2012

Editing .txt file Java

I need to to write a Java program that will write entered information in the console to a .txt file. If the .txt file already exists it will have to open it and write the additional information on another line. If the .txt file doesn't exist for a new "Lifter" it will create the .txt and write it in. Basically I don't know how to create a new .txt for each name entered and how to edit it if the person's .txt already exists. How would I go about doing that?



import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

public class MeetPrinter {

public static void getInfo() {

Scanner scanner = new Scanner(System.in);
int question;
String firstName;
String lastName;
int squat;
int bench;
int lift;
String meetName;
do {

System.out.println("Enter first name: ");
firstName = scanner.next();

System.out.println("Enter Last Name: ");
lastName = scanner.next();

System.out.println("Enter new Meet Name: ");
meetName = scanner.next();

System.out.println("Enter max squat weight in kg: ");
squat = scanner.nextInt();

System.out.println("Enter max bench press in kg: ");
bench = scanner.nextInt();

System.out.println("Enter max deadlift in kg: ");
lift = scanner.nextInt();

System.out
.println("Enter '1' to enter more lifters or '2' if you are done entering.");
question = scanner.nextInt();
} while (question == 1);

try{
PrintWriter out = new PrintWriter("output.txt");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100000);
out.println(lastName + ", " + firstName + " Record #: " + randomInt);
out.println("");
String meet = "Meet Name";
String sq = "SQ Max";
String bp = "BP Max";
String sub = "SUB Total";
String dl = "DL Max";
String tot = "Total";
out.printf("%20s %15s %18s %19s %18s %18s",meet ,sq,bp,sub,dl,tot);
out.println("");
out.printf("%20s", meetName);
out.printf("%10d (%6.2f)", squat, squat * 2.2);
out.printf("%10d (%6.2f)", bench, bench * 2.2);
float subPounds = (float) ((float)(squat + bench) * 2.2);
out.printf("%10d (%6.2f)", squat + bench, subPounds);
out.printf("%10d (%6.2f)", lift, lift * 2.2);
float tPounds = (float) ((float)(squat + bench + lift) * 2.2);
out.printf("%10d (%6.2f)", squat + bench + lift, tPounds);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}

}




Load lots of large UIImages in UITableView

I'm using a UITableView to show lots of photos. I created a custom UITableViewCell , where are 4 UIButtons. Each button is used to represent an image. At one time it will be visible just 16 buttons (4 cells). There is no problem if i use lightweight images, but when i try to load photos(MB's) performance "suffers" very much. Which is the best way to load large photos/images ?





Why not to use Return statement in vb.net

I've been given some code to go through and find problems and things that could be improved and changed (it's a homework task, but this question is unrelated to the task itself), part of the code is:



Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
Dim InSameCell As Boolean
InSameCell = False
If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
InSameCell = True
End If
CheckIfSameCell = InSameCell
End Function


I can't understand why the InSameCell is variable is created, when it can just be assigned to the function name CheckIfSameCell?



Or just use return statements:?



Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
Return True
End If
Return False
End Function


I can understand not returning the expression in the If statement directly, to increase readability.



I know that assigning a return value to the Function name doesn't exit the function, whereas Return does, but is it just a person's style, or is there any advantage to the first version (imo, the second is more readable)?





Hello World phonegap app crashing

Alright, I am trying to just get the demo working on their website, but when I try to install it and run on my phone, I get this exception in Eclipse:



05-01 13:10:49.637: E/AndroidRuntime(19669): java.lang.SecurityException: ConnectivityService: Neither user 10128 nor current process has android.permission.ACCESS_NETWORK_STATE.



I thought maybe it was because it was on WiFi, but it persisted after I disabled that as well. Does anyone know what is causing this? I am literally doing the bare minimum. This is my HTML:



<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0rc1.js"></script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>




IIS7 HttpModule and ISAPI Filter execution order

I have a site using ISAPI Rewrite as well as a custom HttpModule that both do Url redirects and rewrites.



In IIS 6, everything worked fine: The ISAPI Rewrite filter would run first, followed by the HttpModule. In IIS 7 (Integrated mode) the order is now the reverse, which poses a problem.



My problem, specifically, is that the HttpModule has a condition where it will issue a Url rewrite using context.RewritePath. It will explicitly add "index.aspx" to the path if no document was provided, so a request to /test/ gets rewritten to /test/index.aspx.



At some point after the path is rewritten, the ISAPI Rewrite filter executes. We've got a rule that does the opposite of the module: a request to /test/index.aspx gets 301-redirected to /test/. Thus, we have an endless loop.



How is the execution order of HttpModules and ISAPI Filters determined in IIS 7? Can the order be changed? I found this question, but it didn't really help. I'm not a master of IIS 7, but I do understand to some extent that modules and ISAPI filters run "together". Unfortunately, they are still administered differently and I can't figure out how to force one to run before the other. Help!



Note: let's assume I cannot change the existing code. It worked in IIS 6. I just want to know if there's a way to make it work in IIS 7 Integrated mode.





set attribute from value of method

I am sure I am just doing this wrong, can someone point me in the right direction?



I need to set the value of effective_date to a modified format date



effective_date is an attribute of Request



class Request < ActiveRecord::Base

validates_uniqueness_of :effective_date

def format_date=(format_date)
date_a = format_date.split("/")
month, day, year = date_a
effective_date = Date.parse("#{year}-#{month}-#{day}")
end

def format_date
effective_date
end
end


Request.create(format_date: "04/21/2012") is not setting the value to effect_date





JavaScript Separating Axis Theorem

I'm trying to wrap my head around using the Separating Axis Theorem in JavaScript to detect two squares colliding (one rotated, one not). As hard as I try, I can't figure out what this would look like in JavaScript, nor can I find any JavaScript examples. Please help, an explanation with plain numbers or JavaScript code would be most useful.



PS This link helped the most so far Algorithm to detect intersection of two rectangles?



My understanding so far is I'm supposed to get the edge difference of both squares with a method as so, but this may be completely wrong:



// "a" is an entity with width, height, x, y, and an angle.
// Note: I have no clue how to process the Canvas angle into
// projected points on an axis to test a side.
getEdges: function(a) {
var top = {
x: a.x - (a.x + a.width),
y: a.y - a.y
},
bottom = {
x: a.x - (a.x + a.width),
y: a.y + a.height - (a.y + a.height)
},
left = {
x: a.x - a.x,
y: a.y - (a.y + a.height)
},
right = {
x: a.x - a.x,
y: a.y - (a.y + a.height)
};

return [top, right, left, bottom];
}