Syphon Filter
Member
Can anyone help me? I need to create a dynamic image gallery using JSON for class.
This is not a programming question, but I'm assuming one of you has to be able to answer this. For the "Product of Sums" part, why is ~B + ~C not an essential prime implicant? Can't it only be covered by the 2x2? The 0 I am looking at which makes it essential is the top-right in the 2x2 square.
private function copyToClipboard(e:Event):void {
e.preventDefault();
Clipboard.generalClipboard.clear();
Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, dataGridSelectionText("\t", "\n"));
}
private function dataGridSelectionText(fieldSeparator:String, rowSeparator:String):String
{
const columns:IList = dataGrid.columns;
const dataProvider:IList = dataGrid.dataProvider;
var rowIndex:int = -1;
var text:String = "";
for each (var cp:CellPosition in dataGrid.selectedCells)
{
if ((rowIndex != -1) && (cp.rowIndex != rowIndex))
text += rowSeparator;
else if (rowIndex != -1)
text += fieldSeparator;
rowIndex = cp.rowIndex;
var column:GridColumn = columns.getItemAt(cp.columnIndex) as GridColumn;
var value:Object = dataProvider.getItemAt(rowIndex)[column.dataField];
text += exportText(value, column);
}
return text;
}
public function exportText(item:Object, column:DataGridColumn):String
{
return String(item);
}
ID | SOME1 | SOME2 |
1 | HTML | PHP |
ID | FLEX | GAF |
private function dataGridSelectionText(fieldSeparator:String, rowSeparator:String):String
{
const columns:Array = filtterilista2.columns;
const valinnat:Object = filtterilista2.selectedItems;
var cp:Object = filtterilista2.selectedItems;
var rowIndex:int = -1;
var text:String = "";
for each (cp in valinnat) {
if ((rowIndex != -1) && (cp.rowIndex != rowIndex))
text += rowSeparator;
else if (rowIndex != -1)
text += fieldSeparator;
rowIndex = cp.rowIndex;
var column:DataGridColumn = columns.indexOf(cp.columnIndex) as DataGridColumn;
var value:String = ObjectUtil.toString((cp["itemnumber"]+" "+cp["model"]+" "+cp["desc1"]+" "+cp["desc2"]+" "+cp["material"]));
text += exportText(value, column);
}
return text;
}
public function exportText(item:Object, column:DataGridColumn):String
{
return String(item);
}
ID SOME1 SOME2 |
1 HTML PHP |
ID FLEX GAF |
Can anyone help me? I need to create a dynamic image gallery using JSON for class.
Java gurus:
The following isn't an exact implementation that I need, but I'm curious if it's possible to achieve the same effect without having to resort to if-cases or instanceof.
Basically, I need an abstract class or an interface A declaring a catch-all method myMethod(Object o) making it possible to send anything to the class B (that extends/implements A). B should then contain additional implementations of myMethod with a different parameter type (say, myMethod(String s)). If a string is sent to B, it gets cought in myMethod(String), otherwise the parameter gets routed to mymethod(Object o), which doesn't really do anything.
Overriding isn't an option since I'm dealing with different parameter types.
Overloading doesn't work, because the object is declared as:
A obj = new B();
So obj only knows about myMethod(Object o) from A. Is this even possible? Is there a pattern that works in a similar fashion?
public class A
{
public void foo(Object o)
{
System.out.println("Got an object");
}
}
public class B extends A
{
public void foo(Object o)
{
if(o instanceof String)
{
System.out.println("Got a String");
}
else
{
super.foo(o); //super calls parent class implementation
}
}
}
public static void main(String[] args)
{
A bar = new B();
bar.foo(2);
bar.foo("LOL");
}
Got an object
Got a String
Scott Meyers said:Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself.
Just seeing if I can minimize the number of ifs and elses in my OO code.
public interface Strategy
{
public void execute(Object o);
}
public class DefaultStrategy implements Strategy
{
public void execute(Object o)
{
System.out.println("This is the default behaviour");
}
}
public class StringStrategy implements Strategy
{
public void execute(Object o)
{
System.out.println((String) o);
}
}
public class IntStrategy implements Strategy
{
public void execute(Object o)
{
System.out.println((Integer) o * 2);
}
}
public class Context
{
private Strategy strategy;
public Context(Strategy strategy)
{
this.strategy = strategy;
}
public void Execute(Object o)
{
strategy.execute(o);
}
}
public class B
{
private Context context = new Context(new DefaultStrategy());
public void setStrategy(Strategy strategy)
{
context = new Context(strategy);
}
public void foo(Object o)
{
context.Execute(o);
}
}
public static void main(String[] args)
{
B thingy = new B();
thingy.foo("LOL");
thingy.setStrategy(new StringStrategy());
thingy.foo("LOL");
thingy.setStrategy(new IntStrategy());
thingy.foo(2);
}
This is the default behaviour
LOL
4
First of all, thanks so much for the informative reply. As I've said, I'm just beginning and every detail like the number line makes a world of difference. Had to google how to enable line numbers in Eclipse first, though.You can more easily figure where you should start by using the error information (which includes a stack trace) to find out exactly where in the code it happened.
...
and the next time you use nextDouble, it's trying to parse "Your" as a double, causing the error.
Java gurus:
The following isn't an exact implementation that I need, but I'm curious if it's possible to achieve the same effect without having to resort to if-cases or instanceof.
Basically, I need an abstract class or an interface A declaring a catch-all method myMethod(Object o) making it possible to send anything to the class B (that extends/implements A). B should then contain additional implementations of myMethod with a different parameter type (say, myMethod(String s)). If a string is sent to B, it gets cought in myMethod(String), otherwise the parameter gets routed to mymethod(Object o), which doesn't really do anything.
Overriding isn't an option since I'm dealing with different parameter types.
Overloading doesn't work, because the object is declared as:
A obj = new B();
So obj only knows about myMethod(Object o) from A. Is this even possible? Is there a pattern that works in a similar fashion? Even if I create a parameter interface that every parameter has to implement, making it possible to just use a single method, I'd still have to check just wtf it is that implements it at some point and how to work with it, since it should be possible to send any object to the method.
I'm afraid I don't really know of specific resources. I usually just Google what I'm trying to do, and that will either take me to MSDN or StackOverflow.Well, that's good to know so I can keep an eye out for that.
Zoe - you're really good with c# if I remember correct, currently working out my problems with my database, and it's a fun process - but I realize my knowledge in oleDB is lacking. I hate using MSDN as a learning tool, so I was wondering if you know of any good online literature that I could read today, that'd make me more comfortable with it?
I have no idea why you're trying to reduce the number of if-statements. That's a really weird goal, even if you're trying to eliminate jmps for performance. This task could certainly be performed in a different way that is more performant.Just seeing if I can minimize the number of ifs and elses in my OO code.
public interface ICatchAll {
void myMethod(Object o);
}
public class DoesAll implements ICatchAll {
@Override
public void myMethod(Object o) {
try {
String s = (String)o;
myMethod(s);
return;
}catch(Exception ex) {}
try {
float f = (float)o;
myMethod(f);
return;
}catch(Exception ex) {}
}
private void myMethod(String s) {
System.out.println("string: " + s);
}
private void myMethod(float f) {
System.out.println("float: " + f);
}
}
public class JavaApplication2 {
public static void main(String[] args) {
ICatchAll d = new DoesAll();
String s = "yes";
d.myMethod(s);
float f = 10.5f;
d.myMethod(f);
}
}
output:
string: yes
float: 10.5
The idea is to have A do the work you want to do in B. So you define an interface with a method that does that work. Any class you want to pass to B then implements that interface. You can then call that method from B.
For example (in pseudo code):
interface Worker
public void doWork();
class A implements Worker
public void doWork() {print "I'm an A! Yeah!"}
class A2 implements Worker
public void doWork() {print "I'm an A too! Woohoo!"}
class B
myMethod(Worker toDo) { toDo.doWork()}
B.myMethod(A) = "I'm an A! Yeah!"
B.myMethod(A2) = "I'm an A too! Woohoo!"
Ah, I see. Well, you could put all those classes in a particular namespace (package?) and use reflection to iterate over them, for one that implements some interface like Implementor<string> for example. Pretty sure you could do that in Java, right? I haven't used it for quite a while, and I never used reflection.Yeah, this is another method. The reason I wanted to avoid ifs isn't due to performance, but because adding new classes forces me to keep track of typechecks outside the class itself, that now needs to be updated with new casts/whatever. Will make it a pain to maintain once the number gets bigger (which it will).
Super Mario Galaxy$%&http://www.gamerankings.com/wii/915692-super-mario-galaxy/articles.html$%&WII$%&http://image.gamespotcdn.net/gamespot/images/box/6/9/2/915692_68186_thumb.jpg$%&Nintendo$%&2007$%&97.64%$%&77%&$gamrReview%$&02/01/09%$&9.6 out of 10%$&96.00%%§&Nintendo Life%$&11/12/07%$&10 out of 10%$&100.00%%§&Gamer Limit%$&07/28/09%$&9.6 out of 10%$&96.00%%§&RealGamer%$&01/01/08%$&8.6 out of 10%$&86.00%
What sort of code are you using to parse it? It's always better to have some code to look at.[...]
What sort of code are you using to parse it? It's always better to have some code to look at.
string[] RawData1 = Regex.Split(RawData, "µµµ");
foreach (string elem in RawData1)
{
List<string> Element = new List<string>();
List<string> ElementReviews = new List<string>();
string bla = elem.Replace("$%&", "µµµ");
bla = bla.Replace("%&$", "µµµ");
string[] elempart = Regex.Split(bla, "µµµ");
Element.Add(elempart[0]);
Element.Add(elempart[1]);
Element.Add(elempart[2]);
Element.Add(elempart[3]);
Element.Add(elempart[4]);
Element.Add(elempart[5]);
Element.Add(elempart[6]);
Element.Add(elempart[7]);
elempart[8] = elempart[8].Replace("%$&", "µµµ");
elempart[8] = elempart[8].Replace("%%§&", "µµµ");
string[] ReviewsDivider = Regex.Split(elempart[8], "µµµ");
foreach (string elem2 in ReviewsDivider)
{
ElementReviews.Add(elem2);
}
//loading data into the lists that I want to use throughout my program after they have been divided into game (element) and corresponding reviews.
Variables.Data.Add(Element);
Variables.Data.Add(ElementReviews);
}
Maybe the way you transferred the database file caused it to change slightly, like because of line endings or encoding? Or maybe their regional settings are causing numbers to be recognized differently, like if they're in europe and they use commas instead of periods for decimals, and your number parsing isn't specifying the method so it freaks out when it sees the wrong format.
It's missing the semicolon after PC in the list box, too. What generates that? If it's not happening on their end, that gives you a clue as to what else isn't happening.
Is this a .net app? If it just doesn't work, but also doesn't crash, does that mean you're catching exceptions and just ignoring them? Add some logging statements (like, to an error.txt file) in catch blocks. Find out what errors are happening on their computer that might cause this.
Why the weird file format? There are tons of standard things out there like json, xml or csv. Just wonderin.
Yeeeeah...probably everything about my code looks like eye cancer to others >_> I am super messy and usually go with the first idea that comes to mind. Actually I already improved quite a lot over the past 2-3 years, but its still rather... crude.
out-of-the-blue guesses:
Maybe the way you transferred the database file caused it to change slightly, like because of line endings or encoding?
Or maybe their regional settings are causing numbers to be recognized differently, like if they're in europe and they use commas instead of periods for decimals, and your number parsing isn't specifying the method so it freaks out when it sees the wrong format.
Those variable names are straight out BRAIN cancer, the first thing I'd work on if I was you was adopt good, meaningful variable names and use camel case! It'll make debugging a million times easier, for you and others.
A/A2 don't need to take any object as data, they are the data (and B is your algo that works on that data). It relegates part of the problem to what each class should know, keeping in line with the principles of encapsulation.The problem is that B is the only class that knows what should happen when myMethod is called. This only pushes the problem up one layer: Now it's A/A2 that needs to be able to take every object as indata, but only work with a few specific object types when doWork is called.
If I make separate A objects for each object type that can be worked on, it shifts one layer up again. Now I need to find out what type of object I have, so I can instantiate the correct A object with it. Maybe I'm misunderstanding something.
Yeah, I know. I never have in mind that I need to show my code to someone, usually it tends to work... somehow.
Are you using like, double.Parse() somewhere to parse the numbers from strings? There's an overload which allows you to specify an invariant culture, otherwise it uses the culture of the PC running the code.They are Americans, I am European, so our PCs definitely use different formats. Isnt programming something in c# universal? Why is that being affected? They dont enter any decimals.. how could I even adjust that?
double d = double.Parse("50.55", NumberFormatInfo.InvariantInfo);
var ci = CultureInfo.GetCultureInfoByIetfLanguageTag("fr-FR");
double d = double.Parse("50,55", ci.NumberFormat);
//output: 50.55
var ci = CultureInfo.GetCultureInfoByIetfLanguageTag("en-US");
double d = double.Parse("50,55", ci.NumberFormat);
//output: 5055
Hmm... alright, just to eliminate any weirdness:They are Americans, I am European, so our PCs definitely use different formats. Isnt programming something in c# universal? Why is that being affected? They dont enter any decimals.. how could I even adjust that?
Hmm... alright, just to eliminate any weirdness:
1. Make sure to eliminate any weirdness with delimiters - use one consistent delimiter, like I dunno, '#', or a comma.
2. Make sure to know what format the text file is in - if you're using a StreamReader, for example, and your textfile is Unicode, you'll need to specify that when opening. Or if the textfile is in some other specific European encoding, then change it to Unicode, and proceed.
Hmm... alright, just to eliminate any weirdness:
1. Make sure to eliminate any weirdness with delimiters - use one consistent delimiter, like I dunno, '#', or a comma.
2. Make sure to know what format the text file is in - if you're using a StreamReader, for example, and your textfile is Unicode, you'll need to specify that when opening. Or if the textfile is in some other specific European encoding, then change it to Unicode, and proceed.
edit: The above also applies too, if an error is being thrown when parsing.
I'd agree if it was for an embedded system with low resources, but for a modern PC app a better option would be straight-up XML, forget delimiters altogether, when dealing with a lot of textual strings as data the chances of a rogue "content as delimiter" issue occurring increases quite dramatically, think of the fun he'll have when "#Hack" is released as a game title when he's used # as a delimiter for instance.
Even worse is using $ as a delimiter as he currently is, when "$$$ Cash Money Poker" gets released![]()
Eh, a neat CSV would be good enough if this is just a small app he threw together to show off some neat functionality.I'd agree if it was for an embedded system with low resources, but for a modern PC app a better option would be straight-up XML, forget delimiters altogether.
I'd agree if it was for an embedded system with low resources, but for a modern PC app a better option would be straight-up XML, forget delimiters altogether, when dealing with a lot of textual strings as data the chances of a rogue "content as delimiter" issue occurring increases quite dramatically, think of the fun he'll have when "#Hack" is released as a game title when he's used # as a delimiter for instance.![]()
Yep, but the useful thing about standard delimiters of old is that the file can be imported in Excel and parsed/debuged/manipulated easilly. Toma can easilly find any corrupted input by importing the file and telling Excel what the separator is.
Personnaly, with the plethora of lightweitgh databases available now, I find it's a no brainer to go SQL.
Are you using like, double.Parse() somewhere to parse the numbers from strings? There's an overload which allows you to specify an invariant culture, otherwise it uses the culture of the PC running the code.
Well, how about this - upload the text file you're using somewhere, and it'll be clear to spot whether it's an encoding issue, or something else entirely.
edit: Ah, okay, so it is due to regional settings. Nevertheless, the file would be useful
I'll check this in the meantime, kk.This is a smaller example file, gives the same error though:
https://www.dropbox.com/s/hf72863t1tktvic/Database%20-%2020.rar
string[] RawData1 = Regex.Split(RawData, "µµµ");
Well, how about this - upload the text file you're using somewhere, and it'll be clear to spot whether it's an encoding issue, or something else entirely.
edit2: hmm, lemme stare at the example line for a bit
richTextBox5.Text = Convert.ToString(Math.Round(Convert.ToDouble(elem[6].Replace("%",""))/100)));
string test1 = elem[6].Replace("%", "");
double test2 = Convert.ToDouble(test1);
double test3 = Math.Round(test2);
string test4 = Convert.ToString(test3);
I'll check this in the meantime, kk.
But do you use "µµµ" anywhere in the file? Because I don't seem to see it. I ask because of your first line:
and I guess I should also ask if the file is supposed to be all on a single line, no line breaks.Code:string[] RawData1 = Regex.Split(RawData, "µµµ");
Well, not sure what would be wrong with the short line. That is, as long as you always expect elem[6] to contain a percent, formatted in that specific way.
Although, just wondering, what's the first split you use, before the example code you gave?
Convert.ToString(Math.Round(Convert.ToDouble(elem[6].Replace("%",""))/100));
Actually, something I just looked over completely, since I thought you mistyped it:
Did you mistype it, because according to this, you're taking the percentage (77.77%, for example), dividing the number by 100, then rounding it - that results in 1, since you're rounding a decimal that is gonna probably be between .5 and 1 almost all the time.Code:Convert.ToString(Math.Round(Convert.ToDouble(elem[6].Replace("%",""))/100)));
double d = double.Parse("50.55", NumberFormatInfo.InvariantInfo);
Well, if all the numbers are formatted in a specific format in the read file, then you should pass in the culture when doing the double.Parse, like so:I always got either 0 or 1 after rounding, so that is probably the issue. I am doing exactly what you just said. The...
Oh. Wow. Thanks for pointing that out.
Now I got it. One should never, ever ignore a problem that somehow works otherwise but still hasnt been solved.
elem[6] is actually 77.77% in the txt file, but for some reason, and I didnt figure out why ,Math.Round(77.77) would always give me 7777, so I added the /100 to get back to the proper number. However the US formatting would already see this as 77.77/100=0.77.. which is why it worked when I left out the /100 in the 4 line code.
Now the opposite happens though. How can use Math.Round in a specific format? Either I need /100 for EU or I dont for US. Should I use If clauses that check for pc formatting settings or there an easier way?
// Will parse as 77 and 77/100, using (German - Germany) culture info.
var deEnc = double.Parse("77,77", CultureInfo.GetCulture("de-DE"));
// Will parse as the same thing, using the (English - United States) culture.
var usEnc = double.Parse("77.77", CultureInfo.GetCulture("en-US"));
Well, if all the numbers are formatted in a specific format in the read file, then you should pass in the culture when doing the double.Parse, like so:
Code:// Will parse as 77 and 77/100, using (German - Germany) culture info. var deEnc = double.Parse("77,77", CultureInfo.GetCulture("de-DE")); // Will parse as the same thing, using the (English - United States) culture. var usEnc = double.Parse("77.77", CultureInfo.GetCulture("en-US"));
Basically, once you know what format your numbers are gonna be in, use a specific culture through the above method to parse the number you get.
The problem is that B is the only class that knows what should happen when myMethod is called. This only pushes the problem up one layer: Now it's A/A2 that needs to be able to take every object as indata, but only work with a few specific object types when doWork is called.
If I make separate A objects for each object type that can be worked on, it shifts one layer up again. Now I need to find out what type of object I have, so I can instantiate the correct A object with it. Maybe I'm misunderstanding something.
What makes you think its not essential? That and ~b + ~d look essential to me.
touch file; cat - >> file