Browsing 7239 questions and answers with Jon Skeet

Java JSONObject Parsing only 1 string?

I'm fairly new to JSON parsing in Java but when I try and parse this JSON String & find out it's "ID", it repeats the same one twice. [ {"id":"{ID1}","time":123}, ...
Jon Skeet
people
quotationmark

Your JSON represents an array - so that's how you should parse it. You can then easily get the id property from each JSONObject within the array. For example: import org.json.*; public class Test { public static void main(String[]... more 11/3/2014 5:48:47 PM

people

I follow the tutorial in oracle about JLayer but it doesn't work with this code

import java.awt.*; import javax.swing.*; import javax.swing.plaf.LayerUI; public class MyJLayer extends JFrame { public static void main(String[] args) { MyJLayer...
Jon Skeet
people
quotationmark

Your paint method doesn't override a superclass method, so it's not being called. Change the signature to: public void paint(Graphics g, JComponent c) ... and add the @Override annotation so that in future, the compiler can find the... more 11/3/2014 5:16:42 PM

people

Why this code throws 'Collection was modified', but when I iterate something before it, it doesn't?

var ints = new List< int >( new[ ] { 1, 2, 3, 4, 5 } ); var first = true; foreach( var v in ints ) { if ( first ) { for ( long i = 0 ; i <...
Jon Skeet
people
quotationmark

The problem is that the way that List<T> detects modifications is by keeping a version field, of type int, incrementing it on each modification. Therefore, if you've made exactly some multiple of 232 modifications to the list between... more 11/3/2014 5:04:04 PM

people

Minimal Days In First Week of the Year in Java

I am actually going through the utility class Calender. I find myself confused with, The methods getFirstDayOfWeek() returns the first day of the week; e.g., SUNDAY in the...
Jon Skeet
people
quotationmark

It's relevant for the number of days in a week of the week-year. Unless you're interested in week-years, it's probably irrelevant to you. By default I'd expect it to be 4, as that's the normal ISO-8601 value. I don't know for sure whether... more 11/3/2014 3:17:30 PM

people

dapper query fix for "Unclosed quotation mark near ..." error

This query works; that is, it returns the expected results: var r = sql.Query<T>("select * from TableName where Name = '" + name + "'"); but, if one of the 'names' values...
Jon Skeet
people
quotationmark

Using a parameter is the right way to go. You absolutely don't want to put the value into the query itself as you did in your first snippet. However, you've put the @name in quotes, which means it's being treated as a string literal...... more 11/1/2014 7:48:59 PM

people

Elegant way to convert epoch timestamp to Eastern Time and reverse using Nodatime

I am working on writing a managed wrapper around Massachusetts Bay Transportation Authority (MBTA) Realtime API. They have a API which returns the server time which is unix...
Jon Skeet
people
quotationmark

Firstly, as mentioned in comments, it would be best to use Noda Time types throughout your code - only resort to DateTime when you really have to. This should lead to significantly cleaner code throughout. Converting a Unix timestamp to... more 11/1/2014 6:56:05 PM

people

What special characters need to be escaped when insert String literal to database

I'm using jdbc to insert data to database. For some reasons, I can't use PreparedStatement, I have to execute query directly. What special characters do I have to escape ?. I only...
Jon Skeet
people
quotationmark

The reason I can't use PreparedStatement is, my app receive json data from server and its values are always represented as String That's a non-sequitur. You should be able to determine the schema from your database, and then work out... more 11/1/2014 3:50:10 PM

people

When i checked with function isdigit() two integers, first = 1, and second = 2, it give me false result

Why in my code , isdigit() function dont work , when i print sum it is 0? p.s i include <ctype> lib int main(void) { int sum = 0; int prvi = 0; int drugi = 0; ...
Jon Skeet
people
quotationmark

isdigit is used to check whether a character is a decimal digit - it's unclear to me what character repertoire is used, but I don't know of any character repertoires in which 1 and 2 are characters. Try 48 and 49 for example, which are the... more 11/1/2014 2:33:27 PM

people

C# Creating an instance of a class from type

I'm trying to get this functionality: Class<?> c = Class.forName(dir.substring(6).replaceAll("/", ".") + file.getName().replaceAll(".java",...
Jon Skeet
people
quotationmark

Basically I need to find a way to construct a class from the Type, then create an instance of it. A Type is a class (or a struct, etc). Once you've got an appropriate Type, there are various options for instantiating it... for... more 11/1/2014 2:26:17 PM

people

C# Xml Invalid name character; The ' ' character cannot be included in a name

So Im making a music player in C#. When a user opens a song, i want it to be saved to a xml file (it saves its path, its name and its duration). Whenever I try to send the value...
Jon Skeet
people
quotationmark

This is the problem: databaseWriter.WriteElementString("Song file path", newSongPath); You're trying to create an element with a name of Song file path, and that's not valid in XML. Element names can't contain spaces. You could use... more 11/1/2014 10:17:23 AM

people