Skip to main content

Posts

Java - Order of Operations - Using Two Assignment Operators in a Single Line

What are the order of operations when using two assignment operators in a single line? public static void main(String[] args){ int i = 0; int[] a = {3, 6}; a[i] = i = 9; // this line in particular System.out.println(i + " " + a[0] + " " + a[1]); } Edit: Thanks for the posts. I get that = takes values from the right, but when I compile this I get: 9 9 6 I thought it would have been and ArrayOutOfBounds exception, but it is assigning 'a[i]' before it's moving over the 9. Does it just do that for arrays?

Filereader null declarations and appending best practice

I want to optimise my file reader function but am not sure if it is best practice to declare the nulls outside of the try loop. Also, is looping and appending chars to a Stringbuffer considered bad practice? I would like to use the exception handling here, but maybe it is better to use another structure? any advice most welcome, thanks. public String readFile(){ File f = null; FileReader fr = null; StringBuffer content = null; try{ f = new File("c:/test.txt"); fr = new FileReader(f); int c; while((c = fr.read()) != -1){ if(content == null){ content = new StringBuffer(); } content.append((char)c); } fr.close(); } catch (Exception e) { throw new RuntimeException("An error occured reading your file"); } return content.toString(); } }

I wrote a "Rock, Paper, Scissor, Shoot” game in one method using Java. I need help looping the program

I am able to loop the program, but each time I input a value it will return 2 values, the user winning and the user losing. I've experimented using multiple methods and creating a new class which was the tester, but had some problems figuring out the logic. As for loops, I have tried using a for loop, while, and do while. Thanks in advance! // Rock Paper Scissor Shoot Game import java.util.Random; import java.util.Scanner; public class RockPaperSciccor { public static void main(String[] args){ int wins = 0; int losses = 0; int rnd; for(rnd=0;rnd<=10;rnd++) { Random GAME = new Random(); int PC = 1+GAME.nextInt(3); Scanner input = new Scanner (System.in); int SCISSOR, ROCK, PAPER; SCISSOR = 1; ROCK = 2; PAPER = 3; System.out.println(""); System.out.println("Choose Your Weapon! "); System.out.println("1 = Scissor| 2 = Rock| 3 = Paper"); System.out.println(""); int USER

Extending Protocol Buffers in Java

I'm having trouble accessing extended protocol buffer members. Here is the scenario: Message Foo { optional int i = 1; } message Bar { extend Foo { optional int j = 10001; } } I don't have the Bar message within any of my other protos. How can I get Bar.j in Java? All examples I've found require a Bar within a message. Thanks!

Respond encoding of Google App Engine(can not change response encoding)

public class FeedUpdaterServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { PrintWriter out = resp.getWriter(); req.setCharacterEncoding("utf-8"); resp.setLocale(Locale.TAIWAN); resp.setContentType("text/html; charset=utf-8"); resp.setCharacterEncoding("utf-8"); resp.getWriter().println("Hello, world!!@!"); out.println("我是人"); //some chinese character out.println(resp.getCharacterEncoding()); out.flush(); out.close(); } } web xml <locale-encoding-mapping-list> <locale-encoding-mapping> <locale>zh_TW</locale> <encoding>utf-8</encoding> </locale-encoding-mapping> </locale-encoding-mapping-list> Output: Hello, world!!@! ??? ISO-8859-1 It seems that the encoding of the respond can not be changed, what is happening???

Java - Get first and last name of user

Here is my situation. I am writing a program that will store a database. Currently, the way of identifying the user is by their computer user name. I am very desperate here, so any ideas will work. I am looking for ANY method (doesn't always need to work) for BOTH OSX and Windows computers that will somehow fetch the user's first and last name. Thank you all in advance!