• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=Hypershadsy;42880166].NET has '[url=http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.110).aspx]is[/url]'.[/QUOTE] [quote] An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown. Otherwise, the expression evaluates to false. [/quote] Then it looks like DooD should determine the object he gets from the list and cast appropriately.
[QUOTE=Tamschi;42880657]Something is seriously off with your class hierarchy or naming scheme.[/QUOTE] I changed my naming scheme like as soon as I made that change lol. I'm going to try the "is" now instead. I tried it but wasn't really sure what I was trying to accomplish. I'm pretty sure I'm already doing this wrong so I'm going to just have separate lists for each object type instead, since it sort of makes more sense to me now.
[QUOTE=The DooD;42881141]I changed my naming scheme like as soon as I made that change lol. I'm going to try the "is" now instead. I tried it but wasn't really sure what I was trying to accomplish. I'm pretty sure I'm already doing this wrong so I'm going to just have separate lists for each object type instead, since it sort of makes more sense to me now.[/QUOTE] [cpp] for(Thing t : things) { if(t is Player) { (Player) t.doPlayerStuf(); } elseif(t is Enemy) { (Enemy) t.doEnemyStuff(); } } [/cpp] If a foreach is actually called a foreach and not an enhanced for loop in C# then replace the for with foreach, of course. [editline]16th November 2013[/editline] I'm fairly kind of somewhat sure this works.
[QUOTE=mobrockers;42882050][cpp] for(Thing t : things) { if(t is Player) { (Player) t.doPlayerStuf(); } elseif(t is Enemy) { (Enemy) t.doEnemyStuff(); } } [/cpp] If a foreach is actually called a foreach and not an enhanced for loop in C# then replace the for with foreach, of course. [editline]16th November 2013[/editline] I'm fairly kind of somewhat sure this works.[/QUOTE] or use method overrides
[QUOTE=Perl;42882246]or use method overrides[/QUOTE] That only works for methods both classes share, in which case he didn't have his casting problem in the first place.
Does anyone have any idea why my SFML vertices won't draw? I'm trying to create a simple quad tilemap, the current map only has one tile. I've made sure the vertices are placed correctly. Here's the tilemap generator code: [code] for (int y = 0; y < tileArray.Length; y++) { for (int x = 0; x < tileArray[y].Length; x++) { int index = x + y; int tileNum = tileArray[x][y]; int tX = tileNum % (int)(texture.Size.X / tileSize.X); int tY = tileNum / (int)(texture.Size.X / tileSize.X); Vector2f tilePos = new Vector2f(x * tileSize.X, y * tileSize.Y); vertices[(uint)(index * 4)] = new Vertex(tilePos, Color.Red, new Vector2f(tX * tileSize.X, tY * tileSize.Y)); vertices[(uint)(index * 4 + 1)] = new Vertex(tilePos + new Vector2f(tileSize.X, 0), Color.Green, new Vector2f((tX + 1) * tileSize.X, tY * tileSize.Y)); vertices[(uint)(index * 4 + 2)] = new Vertex(tilePos + new Vector2f(tileSize.X, tileSize.Y), Color.Blue, new Vector2f((tX + 1) * tileSize.X, (tY + 1) * tileSize.Y)); vertices[(uint)(index * 4 + 3)] = new Vertex(tilePos + new Vector2f(0, tileSize.Y), Color.Yellow, new Vector2f(tX * tileSize.X, (tY + 1) * tileSize.Y)); } } [/code] And here's the drawing code: [code] TILEMAP: public virtual void Draw(RenderTarget target, RenderStates states) { target.Draw(vertices, states); } GAME: Window.Clear(); Window.Draw(map, RenderStates); Window.Display(); [/code]
[QUOTE=mobrockers;42882050][cpp] foreach (Thing t in things) { if (t is Player) { ((Player)t).doPlayerStuf(); } else if (t is Enemy) { ((Enemy)t).doEnemyStuff(); } } [/cpp][/QUOTE] Fixed. (This indentation style is standard in C# in part due to the auto-formatter. I prefer the other one if both are common though.)
[QUOTE=Tamschi;42884267]Fixed. (This indentation style is standard in C# in part due to the auto-formatter. I prefer the other one if both are common though.)[/QUOTE] I don't think the indentation style has anything to do with the language, but rather the default settings of the IDE, and I'm sure my way of type casting works perfectly fine in C#, but I didn't know the foreach syntax, thank you.
[QUOTE=mobrockers;42885885]I don't think the indentation style has anything to do with the language, but rather the default settings of the IDE, and I'm sure my way of type casting works perfectly fine in C#, but I didn't know the foreach syntax, thank you.[/QUOTE] Well, your typecasting would've cast whatever the method returns, not the object you're calling the method on.
[QUOTE=esalaka;42886016]Well, your typecasting would've cast whatever the method returns, not the object you're calling the method on.[/QUOTE] Hm. I guess you're right. Didn't think of that.
Getting this trying to debug my applciation. Basically says that it can't find or open the PDB file. It used to work a couple of days ago, now it gives this and closes: [code] 'External Overlay.exe' (Win32): caricamento di 'C:\Users\Ludovico\Documents\Visual Studio 2012\Projects\External Overlay\Debug\External Overlay.exe' completato. Simboli caricati. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\ntdll.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\kernel32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\KernelBase.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\user32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\gdi32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\lpk.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\usp10.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\msvcrt.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\advapi32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\sechost.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\rpcrt4.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\sspicli.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\cryptbase.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\msvcp110d.dll' completato. Simboli caricati. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\msvcr110d.dll' completato. Simboli caricati. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\D3DX9_43.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\d3d9.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\version.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\d3d8thk.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\dwmapi.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\apphelp.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\AppPatch\AcLayers.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\shell32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\shlwapi.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\ole32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\oleaut32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\userenv.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\profapi.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\winspool.drv' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\mpr.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\imm32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\msctf.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\uxtheme.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Program Files (x86)\Common Files\microsoft shared\ink\tiptsf.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\ole32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): scaricamento di 'C:\Windows\SysWOW64\ole32.dll' completato 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\ole32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): scaricamento di 'C:\Windows\SysWOW64\ole32.dll' completato 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\clbcatq.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\msctf.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): scaricamento di 'C:\Windows\SysWOW64\msctf.dll' completato 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\oleacc.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\nvd3dum.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\psapi.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\powrprof.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\setupapi.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\cfgmgr32.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Windows\SysWOW64\devobj.dll' completato. Impossibile trovare o aprire il file PDB. 'External Overlay.exe' (Win32): caricamento di 'C:\Program Files (x86)\NVIDIA Corporation\3D Vision\nvSCPAPI.dll' completato. Impossibile trovare o aprire il file PDB. Il thread 0x1cc4 è terminato con il codice 0 (0x0). Il programma '[7312] External Overlay.exe' è terminato con il codice 0 (0x0).[/code]
It found the symbols, the program just completed without error. You should throw an exception if something goes wrong.
Hey guys, I'm back again. I pretty much re-coded my last post entirely and got it to somewhat work. I somehow posted the wrong code that didn't work correctly. I apologize. For this assignment, I'm supposed to find all perfect numbers between 1 and 1000, and then find the fifth number. The perfect numbers from 1 to 1000 is the easy part, but the fifth is a doosy. I originally coded it in brute force and that doesn't work for the fifth integer, so I had to re-code it. I found the use of Mersenne formula and tried to implement that. It simply is supposed to compare the number from the range, 1 to 1000 or whatever, to the current perfect number and see if they match. That way I can keep the perfect number within whatever range I specify. However, when I try to calculate the fifth perfect number, it takes way too long to execute, and I'm not quite sure where the error is. I've spent a long time trying to figure this out, and I've coded it five different times now. [cpp]#include <iostream> #include <vector> #include <cmath> using namespace std; bool prime(int result); bool perfect(int n); int n=2; int main() { vector<int> resultfinal; cout << "Perfect integers between 1 and 1000:\n"; for (int i=1;i<=33550336;i++){ // Used to check how many perfects in range, set to the fifth as max for example if (perfect(i)){ // If perfect comes back true int numberoftimes=0; resultfinal.clear(); for (int j=1;j<=(i/2);j++){ // Just used to find the factors of i if (i%j == 0){ resultfinal.push_back(j); numberoftimes++; } } cout << i << " = " << resultfinal[0]; // Output i and its factors for (int counter = 1; counter < numberoftimes; counter++){ cout << " + " << resultfinal[counter]; } cout << endl; n++; } } return 0; } bool prime(int result){ // Checking if n is a Mersenne Prime int primeresult; primeresult = (pow(2,result)-1); for (int a=(primeresult/2); a>1; a--){ if (primeresult%a == 0){ return false; } } return true; } bool perfect(int i){ // Function to calculate the result to compare to i int result; while(prime(n) == false){ // If n is not a mersenne prime, increase it by one, check again n++; } result = (pow(2,n-1)*(pow(2,n)-1)); // Calculate n into the perfect number if (result == i){ // Check if i is a perfect number return true; } else { return false; } } [/cpp]
Can someone point me out to a tutorial on how to use spritesheets in C#? Working on a game that's due in 10 days, and I'm very far behind. Really stressing out... Please, and thank you. I've been trying to use an XML file, but I just don't understand how it's supposed to work with a sprite class.
Its hard to help you without knowing anything about the engine you're working with :/ Is it a selfmade engine? Basically the XML file just describes how the image is structured. Depending on what should be drawn you just use a sub-region of the spritesheet.
Does anyone know of a resource showing how to use OpenGL Meshes as collision objects with Bullet Physics? I can't seem to find anything at all.
Is there any reason Bing Maps API for WPF is now causing my program to crash under a null reference exception, I looked it up and apparently it's something to do with the Map Control not being able to download tile data. Sometimes it works fine, others it just decides it doesn't want to download the tile data.
[QUOTE=Zareox7;42887357]Hey guys, I'm back again. I pretty much re-coded my last post entirely and got it to somewhat work. I somehow posted the wrong code that didn't work correctly. I apologize. For this assignment, I'm supposed to find all perfect numbers between 1 and 1000, and then find the fifth number. The perfect numbers from 1 to 1000 is the easy part, but the fifth is a doosy. I originally coded it in brute force and that doesn't work for the fifth integer, so I had to re-code it. I found the use of Mersenne formula and tried to implement that. It simply is supposed to compare the number from the range, 1 to 1000 or whatever, to the current perfect number and see if they match. That way I can keep the perfect number within whatever range I specify. However, when I try to calculate the fifth perfect number, it takes way too long to execute, and I'm not quite sure where the error is. I've spent a long time trying to figure this out, and I've coded it five different times now. [cpp]//code[/cpp][/QUOTE] A quick search for "Mersenne formula" and "perfect number" turned up a formular by Euler. Although it uses a Mersenne prime, the formular for the perfect numbers is by Euler, not Mersenne :) I think it takes long because you're checking if n is a Mersenne prime again and again. But the logic seems a bit spaghetti in general, because perfect() depends on globally modified state. I also think the names you've chosen are imprecise. I'd use a different approach and make perfect(n) return a perfect number given that n is a Mersenne prime (or rather just use the formular inline without creating a new function). In the "main-loop" I'd use prime(n) to return the next Mersenne prime after n (and probably name it nextmersenneprime (assuming all-lower case is your naming scheme)), break if it's bigger than 1000 and otherwise add it to a vector of perfect numbers. If your instruction is to actually [i]compare[/i] the numbers [0..1000] to perfect numbers, you can just do so in an additional step. To find the fifth number you'd of course have to loop min(0, 5 - perfectnumbers.size()) (disregarding possible underflow) additional times after finding the perfect numbers in that range. Also, numberoftimes == resultfinal.size(). It'd just print the factors directly instead of putting them in a vector first. And if you insist on the vector, you could just declare it later, in the scope of the if(prime(i))-branch. That way you don't need the resultfinal.clear(). And uhh... 4 &#8800; 1 + 2 = 3. Not sure if that's what you're looking for.
[QUOTE=ZeekyHBomb;42891684]A quick search for "Mersenne formula" and "perfect number" turned up a formular by Euler. Although it uses a Mersenne prime, the formular for the perfect numbers is by Euler, not Mersenne :) I think it takes long because you're checking if n is a Mersenne prime again and again. But the logic seems a bit spaghetti in general, because perfect() depends on globally modified state. I also think the names you've chosen are imprecise. I'd use a different approach and make perfect(n) return a perfect number given that n is a Mersenne prime (or rather just use the formular inline without creating a new function). In the "main-loop" I'd use prime(n) to return the next Mersenne prime after n (and probably name it nextmersenneprime (assuming all-lower case is your naming scheme)), break if it's bigger than 1000 and otherwise add it to a vector of perfect numbers. If your instruction is to actually [i]compare[/i] the numbers [0..1000] to perfect numbers, you can just do so in an additional step. To find the fifth number you'd of course have to loop min(0, 5 - perfectnumbers.size()) (disregarding possible underflow) additional times after finding the perfect numbers in that range. Also, numberoftimes == resultfinal.size(). It'd just print the factors directly instead of putting them in a vector first. And if you insist on the vector, you could just declare it later, in the scope of the if(prime(i))-branch. That way you don't need the resultfinal.clear(). And uhh... 4 &#8800; 1 + 2 = 3. Not sure if that's what you're looking for.[/QUOTE] Yea I figured the code was kinda all over the place. For one program as part of a 5 program assignment, I've spent probably a total of 12 hours trying to figure it out and completely erased it and coded it again like five times, because I get so into the logic and I make it far too complex, to which one little thing makes it not work. Probably why it looks so spaghetti haha. Thanks for the help, and yeah I really gotta work on my naming schemes.
[QUOTE=Felheart;42888439]Its hard to help you without knowing anything about the engine you're working with :/ Is it a selfmade engine? Basically the XML file just describes how the image is structured. Depending on what should be drawn you just use a sub-region of the spritesheet.[/QUOTE] Self-made engine. Using a premade sprite class that we were given, though. I understand how a XML file works, I just don't know how to have it work with a sprite class. The one I'm using now doesn't have any functionality with the XML file. So, I think I either need to make my own sprite class or make a spritesheet class? I'm not entirely sure. I have no clue how to do either.
[QUOTE=Emugod;42893347]Self-made engine. Using a premade sprite class that we were given, though. I understand how a XML file works, I just don't know how to have it work with a sprite class. The one I'm using now doesn't have any functionality with the XML file. So, I think I either need to make my own sprite class or make a spritesheet class? I'm not entirely sure. I have no clue how to do either.[/QUOTE] Write a loader that turns the XML file and image into a collection of that sprite class's instances, that's probably the fastest way to get it done.
[QUOTE=Tamschi;42893459]Write a loader that turns the XML file and image into a collection of that sprite class's instances, that's probably the fastest way to get it done.[/QUOTE] How would I read the XML file to know what images need to be taken out?
[QUOTE=Zareox7;42893225]Yea I figured the code was kinda all over the place. For one program as part of a 5 program assignment, I've spent probably a total of 12 hours trying to figure it out and completely erased it and coded it again like five times, because I get so into the logic and I make it far too complex, to which one little thing makes it not work. Probably why it looks so spaghetti haha. Thanks for the help, and yeah I really gotta work on my naming schemes.[/QUOTE] Ahh much better. I guess since I'm not wracking my mind over and over again, I was able to think clearly and focus on the end goal a lot easier. Code works great and it finds the fifth integer in no time flat. I think I was trying to do it a certain way because of the way my professor worded his question, instead of doing it the easier way. I also changed all my variable names to something that clearly identifies it. No more of those "a", "i", "j" variables. Also implemented capital letters into them such as "PossibleMersennePrime".
[QUOTE=Emugod;42893724]How would I read the XML file to know what images need to be taken out?[/QUOTE] The System.XML namespace contains classes for dealing with XML, if you use System.XML.Linq instead you can filter the file with LINQ comprehension syntax, which is a lot shorter if you know how it works. (Don't try to learn it if you're short on time anyway.) Other than that it really depends on your format, you're posting so little information that you probably get better anwers from just googling it.
I want to start making a very simple pong game in C# with XNA, just to actually have a 2d game that I've started and finished. Is there a way that I can just use functions like spriteBatch.DrawRect(x, y, w, h) or spriteBatch.DrawEllipse(x, y, w, h)? I keep getting to points where I have to open up Paint.NET and make the shapes myself all though they are very generic solid black shapes. EDIT: If there is no such commands, any tips on how I can not be so stupid at Paint.NET? I am trying to make the ball in pong but I can't figure out how to draw a perfect 32x32 pixel circle in Paint.NET.
[QUOTE=BooTs;42894694]I want to start making a very simple pong game in C# with XNA, just to actually have a 2d game that I've started and finished. Is there a way that I can just use functions like spriteBatch.DrawRect(x, y, w, h) or spriteBatch.DrawEllipse(x, y, w, h)? I keep getting to points where I have to open up Paint.NET and make the shapes myself all though they are very generic solid black shapes. EDIT: If there is no such commands, any tips on how I can not be so stupid at Paint.NET? I am trying to make the ball in pong but I can't figure out how to draw a perfect 32x32 pixel circle in Paint.NET.[/QUOTE] Another option would be not using XNA. If you want a simple drawing library with basic functions for drawing circles, rectangles and stuff, try [URL="http://www.matrix44.net/old/sdl/sdlnet.html"]SDL.NET[/URL] ([URL="http://sourceforge.net/projects/cs-sdl/files/SDL.NET/6.1.1beta/"]download[/URL])
Honestly, I've been flip flopping between C# XNA, C++ SDL, C++ Allegro, and Java with game programming. I kinda just want to stick with XNA for now just because it is easy to me. I guess I'll have to just get better at Paint.NET. I keep getting told to switch by different people and I feel like this has a lot to do with why I started learning how to program 6 years ago and still haven't made any noticable progress. I just finished up the pong game, it's one player so all it really is, is a ball bouncing around and you can move the paddle up and down to hit it against the wall, with a scoring system that is based off of how many times the ball collides with the paddle. But now I don't know what I can to do test my abilities, does anyone have any suggestions of a simple program projects( doesn't have to be a game ) that I can work on, so I can get a better understanding how much I actually know. I keep having a feeling that I am ready to start making programs but I just haven't jumped into anything yet.
I'm trying to count the number of passed arguments in Java. (For example, if they pass in one argument it'd do something, but if they pass in two it'd do something else.) Would args.length work? [editline]17th November 2013[/editline] And speaking of args.length, what does it define as 1 arg? If I put in like 2.13 4 51231083 is that arg.length = 3?
Can somebody help me out with files in Java? My project structure is as such: project --src ----java files --res ----txt files and stuff I just want to be able to use "res/myfile.txt" as a file path, but I can't figure out how to do this without using an absolute file path. I've looked around and heard alot about using ClassLoader and getResources but I can't figure out how exactly they work. This is the current function I'm using, but want to use relative paths for: [CODE] /** * Recursively list all files and files in subdirectories starting from the given root. * @param path The path of the root file to search. */ public void searchRoot(String path){ File root = new File(path); File[] files = root.listFiles(); for(File file : files){ if(file.isDirectory()){ searchRoot(file.getAbsolutePath()); }else{ results.add(file); } } } [/CODE]
[QUOTE=Zinayzen;42895996]I'm trying to count the number of passed arguments in Java. (For example, if they pass in one argument it'd do something, but if they pass in two it'd do something else.) Would args.length work? [editline]17th November 2013[/editline] And speaking of args.length, what does it define as 1 arg? If I put in like 2.13 4 51231083 is that arg.length = 3?[/QUOTE] args.length works, assuming you mean the String[]-parameter to main. I don't understand the second question (do you mean first arg, as in what is args[0]? If so, just see next answer). If by that you mean you invoke your program as [i]java MyProg 2.13 4 51231083[/i], then args will be { "2.13", "4", "51231083" }, so yes, args.length is 3 and args[0] = "2.13", args[1] = "4" and args[2] = "51231083".
Sorry, you need to Log In to post a reply to this thread.