• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=blacksam;45988967]I'm trying to read a text file filled with customer ID's in java. I want only the ID's in said file. The good news is that the ID is a string of numbers " Customer id=9115584444938613376" so how do I remove everything except the ID's. Some ID's vary in length too. It's over 200 people. Never touched Regex, tried using String.match(), and coming up with ways to do it in Ruby. Any quick way to edit a text file? Also, cool story, they've stored it in an array outputting it's contents to the text file.[/QUOTE] Check out [url=http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29]String.split().[/url] You should be able to easily enough create something that can extract customer IDs if they're separated by a simple delimiter. [editline]15th September 2014[/editline] For example, in your case, it'd be as simple as iterating over the lines in the text file: [code] for (String line : lines) { String[] parts = line.split("="); String customerId = parts[1]; } [/code]
Regex is beautiful. Try Regex101.com; it has a handy 'this is what each part does' commentary thing and nice highlighting. also: [code] Customer id=(?<ID>\d*) Customer id= matches the characters Customer id= literally (case sensitive) (?<ID>\d*) Named capturing group ID \d* match a digit [0-9] Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy] [/code]
So I really like doing math. Given, I'm only in precalculus, but the math aspects of computer programming and just reducing numbers to other forms and getting solutions from functions is just fun to me. What kind of field could I use that in?
So I'm doing this thing in AP Comp Sci. where I have to make some fraction calculator in Java. So I decided I'd start out with a non-fraction based one first, but I have ran into an issue for where the user is unable to select the type (integers and real numbers) of calculation to be ran, and I have no idea why. Here is part of the code where the error is: [code] import java.util.Scanner; public class Calculator { @SuppressWarnings("resource") public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Select Calculator Type (Decimals or Integers?): "); int selection = console.nextInt(); if (selection = Decimals){ Decimals(); } else if (selection = Integers) { Integers(); } else { System.out.println("Not a valid type"); } } [/code] Currently, the error is in regards to the "selection" variable, and the error reads that Integers and Decimals are not resolved as variables. How do I fix this?
There are a few things wrong with your code: [code]int selection = console.nextInt();[/code] If you expect the user to type in either decimals or integers you have to ask for a String, not an int. I believe the function is just next() instead of nextInt(). [code]if (selection = Decimals){ ...[/code] Single equals (=) is for assignment while double equals (==) checks for equality (only use for primitive types like ints and doubles). Decimals should be a String (surround in quotes) - this is the cause of the error you mentioned. To check for equality of Strings use the equals method: [code]if (selection.equals("Decimals")) { ...[/code] If you want the comparison to be case insensitive (e.g. the user enters "decimals" instead of "Decimals"), there's an equalsIgnoreCase() method.
[QUOTE=Fredo;45998954]There are a few things wrong with your code: [code]int selection = console.nextInt();[/code] If you expect the user to type in either decimals or integers you have to ask for a String, not an int. I believe the function is just next() instead of nextInt(). [code]if (selection = Decimals){ ...[/code] Single equals (=) is for assignment while double equals (==) checks for equality (only use for primitive types like ints and doubles). Decimals should be a String (surround in quotes) - this is the cause of the error you mentioned. To check for equality of Strings use the equals method: [code]if (selection.equals("Decimals")) { ...[/code] If you want the comparison to be case insensitive (e.g. the user enters "decimals" instead of "Decimals"), there's an equalsIgnoreCase() method.[/QUOTE] Holy shit, thank you.
I have an unbounded array with methods to add and remove from it. If I exceed the maximum size then I have to make a new array twice the size and copy everything over. If I fall below 25% then I have to make a new array half the size and copy everything over. I want to put the code to make a new array and copy it into a method of it's own. I'm wondering if there's a "convention" to do it in Java. Should I use a boolean parameter like this? [code] public void resize(boolean bool) { if (bool) maxSize = maxSize*2; else maxSize = maxSize/2; //code to copy array } [/code] EDIT or just call the resize method in my add and remove methods and do this instead [code] public void resize() { if (currentSize == maxSize) maxSize = maxSize*2; else if (currentSize < (maxSize*0.25)) maxSize = maxSize/2; //code to copy array } [/code]
-snip I FINALLY FUCKING DID IT- [editline]16th September 2014[/editline] [QUOTE=wlitsots;46000209]I have an unbounded array with methods to add and remove from it. If I exceed the maximum size then I have to make a new array twice the size and copy everything over. If I fall below 25% then I have to make a new array half the size and copy everything over. I want to put the code to make a new array and copy it into a method of it's own. I'm wondering if there's a "convention" to do it in Java. Should I use a boolean parameter like this? [code] public void resize(boolean bool) { if (bool) maxSize = maxSize*2; else maxSize = maxSize/2; } //code to copy array [/code] EDIT or just call the resize method in my add and remove methods and do this instead [code] public void resize() { if (currentSize == maxSize) maxSize = maxSize*2; else if (currentSize < (maxSize*0.25)) maxSize = maxSize/2; //code to copy array } [/code][/QUOTE] Why not this? [code]int[] a = new int[N] if(numVals > N-1 || numVals < N*.25) a = new int[numVals]; //then initialize the array with values[/code]
Hey guys, I'm trying to understand this question in my C textbook: [quote]Which one of the following statements is not equivalent to the other two (assuming that the loop bodies are the same)?[/quote] _______________________________________________ [quote]a[/quote] [code] while (i < 10) {...} [/code] _______________________________________________ [quote]b[/quote] [code] for (; i < 10;) {...} [/code] _______________________________________________ [quote]c[/quote] [code] do {...} while (i < 10); [/code] _______________________________________________ Shouldn't it be example C, since a do loop executes and then does a check? My understanding is that A and B are the same, and the for loop is just a while loop in disguise. I tried to figure it out by creating a program, but the outputs were all exactly the same with: [code] printf("Loop 1 %d\n", i); i++;[/code]
[QUOTE=Cittidel;46000883]C stuff[/QUOTE] You are correct, but your test program won't output any different because their conditions are the same and are initially true. Assuming i is initialized to 0, try changing the condition to i < 0 and see how it runs. You should see the do while loop execute, but the others won't.
[QUOTE=Foxtrot200;46000926]Assuming i is initialized to 0, try changing the condition to i < 0 and see how it runs. You should see the do while loop execute, but the others won't.[/QUOTE] Thanks for pointing that out, for some reason I was only thinking of a way to differentiate them after they had been run (as in they all run, but one of the loops run an extra time.)
So I'm having an odd issue that I've been scratching my head over for the past couple hours. I got CEGUI hooked up correctly earlier today to a rendering/game engine project I've been working on for a couple months. Whenever I call the gui render function the models in my scene (which rendered normally before) are suddenly all transparent. I've attached a picture to illustrate. I've naively looked into some depth buffer and blend function settings for openGL but have had no luck, and I'm pretty lost at where to start with this problem. Any insight or potential explanation would be extremely helpful, thanks! [img]https://dl.dropboxusercontent.com/u/39952031/CEGUITransparencyIssue.jpg[/img]
I know lua quite well and enjoy working with it, but I have no experience with any other language or programming at all before hand before I started. If I wanted to try out a new language or do something different, would it be best to become more confident with lua or just go for it now? If so, what would you recommend? Thanks!
so i'm working on an animation system in java, using assimp. i've managed to import all the mesh/material/bone data and construct a set of keyframes and a skeleton. i've tested that everything is loading correctly, and i'm passing the right weights/boneid's into my VBO, but nothings showing up on my screen.. this is the code that binds the attribs for my shader: [CODE] public class ShaderDefault extends GLShader { public static final int VERTEX_DATA_SIZE = (3 + 2 + 3 + 4 + 4) * 4; public static final int COMPONENT_SIZE = 4; public static final int OFFSET_POSITION = 0; public static final int OFFSET_TEXCOORD = 3; public static final int OFFSET_NORMAL = 3 + 2; public static final int OFFSET_BONEIDS = 3 + 2 + 3; public static final int OFFSET_BONE_WEIGHTS = 3 + 2 + 3 + 4; public int attribPosition; public int attribTexCoord; public int attribNormal; public int attribBoneIDs; public int attribBoneWeights; public ShaderDefault(){} public ShaderDefault(String filename) { super(filename); } public void loadAttribs() { attribPosition = glGetAttribLocation(m_program, "in_Position"); attribTexCoord = glGetAttribLocation(m_program, "in_TexCoord"); attribNormal = glGetAttribLocation(m_program, "in_Normal"); attribBoneIDs = glGetAttribLocation(m_program, "in_BoneIDs"); attribBoneWeights = glGetAttribLocation(m_program, "in_BoneWeights"); //System.out.println(attribPosition + " " + attribTexCoord + " " + attribNormal + " " + attribBoneIDs + " " + attribBoneWeights); } public void bindAttribs() { glEnableVertexAttribArray(attribPosition); glEnableVertexAttribArray(attribTexCoord); glEnableVertexAttribArray(attribNormal); glEnableVertexAttribArray(attribBoneIDs); glEnableVertexAttribArray(attribBoneWeights); glVertexAttribPointer(attribPosition, 3, GL_FLOAT, false, VERTEX_DATA_SIZE, OFFSET_POSITION * COMPONENT_SIZE); glVertexAttribPointer(attribTexCoord, 2, GL_FLOAT, false, VERTEX_DATA_SIZE, OFFSET_TEXCOORD * COMPONENT_SIZE); glVertexAttribPointer(attribNormal, 3, GL_FLOAT, false, VERTEX_DATA_SIZE, OFFSET_NORMAL * COMPONENT_SIZE); glVertexAttribPointer(attribBoneIDs, 4, GL_INT, false, VERTEX_DATA_SIZE, OFFSET_BONEIDS * COMPONENT_SIZE); glVertexAttribPointer(attribBoneWeights,4, GL_FLOAT, false, VERTEX_DATA_SIZE, OFFSET_BONE_WEIGHTS * COMPONENT_SIZE); } } [/CODE] this is my shader: [CODE] const int MAX_BONES = 100; attribute vec3 in_Position; attribute vec2 in_TexCoord; attribute vec3 in_Normal; attribute vec4 in_BoneIDs; attribute vec4 in_BoneWeights; uniform mat4 u_ViewProjection; uniform mat4 u_Model; uniform mat4 u_Bones[MAX_BONES]; varying vec2 v_TexCoord; varying vec3 v_Normal; varying vec3 v_WorldPos; void main() { mat4 bone = u_Bones[int(in_BoneIDs.x)] * in_BoneWeights.x; bone += u_Bones[int(in_BoneIDs.y)] * in_BoneWeights.y; bone += u_Bones[int(in_BoneIDs.z)] * in_BoneWeights.z; bone += u_Bones[int(in_BoneIDs.w)] * in_BoneWeights.w; in_Position = (bone * vec4(in_Position, 1.0)).xyz; in_Normal = (bone * vec4(in_Normal, 1.0)).xyz; gl_Position = u_ViewProjection * u_Model * vec4(in_Position, 1.0); v_TexCoord = in_TexCoord; v_Normal = normalize((u_Model * vec4(in_Normal, 0.0)).xyz); v_WorldPos = (u_Model * vec4(in_Position, 1.0)).xyz; } [/CODE] now when i run it as-is, i don't see anything. but if i change this piece of the shader [CODE] mat4 bone = u_Bones[int(in_BoneIDs.x)] * in_BoneWeights.x; bone += u_Bones[int(in_BoneIDs.y)] * in_BoneWeights.y; bone += u_Bones[int(in_BoneIDs.z)] * in_BoneWeights.z; bone += u_Bones[int(in_BoneIDs.w)] * in_BoneWeights.w; [/CODE] to this [CODE] mat4 bone = u_Bones[0] * in_BoneWeights.x; bone += u_Bones[0] * in_BoneWeights.y; bone += u_Bones[0] * in_BoneWeights.z; bone += u_Bones[0] * in_BoneWeights.w; [/CODE] it works perfectly (granted, the animations don't work because u_Bones[0] is an identity matrix) i'm super fucking confused right now. it seems like everything is working fine when i access the array with constants, but when i try to access it dynamically it fails horribly. i've also uploaded everything to [URL="https://github.com/MGinshe/JEngine"]github [/URL]if that helps (animations aren't actually being set/used yet, because of this weird bug i'm getting)
[QUOTE=NiandraLades;46002162]I know lua quite well and enjoy working with it, but I have no experience with any other language or programming at all before hand before I started. If I wanted to try out a new language or do something different, would it be best to become more confident with lua or just go for it now? If so, what would you recommend? Thanks![/QUOTE]I found that once you've learned one high-level programming language, it's a lot easier to learn other high-level programming languages. Java or C++/C# would be some good ones to start with, as they teach you a lot of stuff that carries on to other languages/makes learning other languages easier.
[QUOTE=NixNax123;46000360]-snip I FINALLY FUCKING DID IT- [editline]16th September 2014[/editline] Why not this? [code]int[] a = new int[N] if(numVals > N-1 || numVals < N*.25) a = new int[numVals]; //then initialize the array with values[/code][/QUOTE] It's for homework so I have to do it this way. Also I'm not sure if we're talking about the same thing. I don't initialize the array, I just declare it with a fixed maximum size. I add to it using an add method. If the array is full and I try to add then I have to make a new array twice the size.
[QUOTE=wlitsots;46003899]It's for homework so I have to do it this way. Also I'm not sure if we're talking about the same thing. I don't initialize the array, I just declare it with a fixed maximum size. I add to it using an add method. If the array is full and I try to add then I have to make a new array twice the size.[/QUOTE] So if it's for homework, then I guess you can't use Lists, which is what you would normally use in this situation. But I still don't understand why you wouldn't know how many elements you're adding to the array beforehand? I guess you would just check the bounds before every addition of an element, and then, if it's successful, check how many times the loop to add items iterated, and compare that against the array size, and modify it based on that.
You know I'm bad at programming, but i've got a problem. Basically i have three decimals : [CODE] decimal aTriangle; decimal hTriangle; decimal tResult; [/CODE] And i need to add the two decimals together and divide by two : [CODE] tResult = aTriangle * hTriangle / (decimal)2; [/CODE] This doesn't work. [CODE] tResult = aTriangle * hTriangle / 2; [/CODE] Doesn't work either. How would i divide it?
Did you mean addition or multiplication? And what do you mean by "it doesn't work"? Wrong result? An error message? What language is that, anyway?
[QUOTE=NiandraLades;46002162]I know lua quite well and enjoy working with it, but I have no experience with any other language or programming at all before hand before I started. If I wanted to try out a new language or do something different, would it be best to become more confident with lua or just go for it now? If so, what would you recommend? Thanks![/QUOTE] I'd recommend Java or C++ simply for the fact that they are very commonly used. This makes googling for solutions to problems that much easier. I personally started with C++ and learned lua later, and I can say the transition to it will be easy but still will require some learning. C++ is my favorite language because of how widely used it is and how much neat stuff you can do with it. I've been learning Java in college and that is a lot simpler than C++, but the two are very similar in syntax. Do you have any projects in mind that you want to use your ne language to solve? As for if you should wait or jump in now, I'd say jump straight into it. If you want to learn effectively, pick up a book to teach yourself how to use the language, and either a book or a resource that has a bunch of programming problems for you to practice on.
[code]public static void main(String[] args) throws FileNotFoundException { FileInputStream is = new FileInputStream(new File("input.txt")); System.setIn(is); Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); long[] A = new long[N]; long[] B = new long[M]; long[] C = new long[M]; for(int i = 0; i < N; i++) { A[i] = in.nextInt(); } for(int i = 0; i < M; i++) { B[i] = in.nextInt(); } for(int i = 0; i < M; i++) { C[i] = in.nextInt(); } for(int i = 0; i < M; i++) { for(int j = 0; j < N; j++) { if((j+1) % B[i] == 0) A[j] = ((A[j] % ((int)(Math.pow(10, 9) + 7))) * (C[i] % ((int)(Math.pow(10, 9) + 7)))) % (int)(Math.pow(10, 9) + 7); } } for(int i = 0; i < N; i++) { System.out.print(A[i] + " "); } }[/code] So, this code works, but is taking way too long to execute. How can I optimize it? It's for this problem: [url]https://www.hackerrank.com/challenges/sherlock-and-queries[/url]
nevermind, found a solution
[QUOTE=Fourier;46006968]nevermind, found a solution[/QUOTE] Chrome extensions can have a background page running (like a Gmail status icon checking for new emails), but unless you give the extension permission to access every site, it can't see what you're visiting. [editline]17th September 2014[/editline] Ninja snip
why put void in [code]int main()[/code] so it becomes [code]int main(void)[/code] What difference does it make?
Having a void in the parameters is a standard C thing. In the past it was required, I'm not sure now but if anything it's a carryover from older compilers that required it. If you aren't coding in C, so C++, C#, or Java, don't add a void into the parameters. Not sure about any other C style languages.
in C++, they are both the same in C, an empty parameter list specifies that the prototype could take in any argument, but if the parameter is [I]void[/I], then it specifies that it takes no arguments.
[QUOTE=Cittidel;46000883]Hey guys, I'm trying to understand this question in my C textbook: _______________________________________________ [code] while (i < 10) {...} [/code] _______________________________________________ [code] for (; i < 10;) {...} [/code] _______________________________________________ [code] do {...} while (i < 10); [/code] _______________________________________________ Shouldn't it be example C, since a do loop executes and then does a check? My understanding is that A and B are the same, and the for loop is just a while loop in disguise. I tried to figure it out by creating a program, but the outputs were all exactly the same with: [code] printf("Loop 1 %d\n", i); i++;[/code][/QUOTE] Do-While loops will execute the code once before checking the conditional. so if i is 20, [code] int i = 20; while(i<10){ printf("executed the while loop"); } do{ printf("executed the do-while loop"); }while(i<10) [/code] the output will be one line of "executed the do-while loop" a regular while loop checks the conditional before executing [editline]17th September 2014[/editline] [QUOTE=frdrckk;46007697]why put void in [code]int main()[/code] so it becomes [code]int main(void)[/code] What difference does it make?[/QUOTE] C sees int main() as a main method that could have some or no parameters. C sees int main(void) as having only no parameters, so if the program tries to call a function with (void) in the parameters, the compiler (should) throw an error and prevent you from doing that.
[QUOTE=WTF Nuke;46008047]Having a void in the parameters is a standard C thing. In the past it was required, I'm not sure now but if anything it's a carryover from older compilers that required it. If you aren't coding in C, so C++, C#, or Java, don't add a void into the parameters. Not sure about any other C style languages.[/QUOTE] [QUOTE=NixNax123;46008059]in C++, they are both the same in C, an empty parameter list specifies that the prototype could take in any argument, but if the parameter is [I]void[/I], then it specifies that it takes no arguments.[/QUOTE] [QUOTE=proboardslol;46008111] C sees int main() as a main method that could have some or no parameters. C sees int main(void) as having only no parameters, so if the program tries to call a function with (void) in the parameters, the compiler (should) throw an error and prevent you from doing that.[/QUOTE] I see, thanks! I asked because the book I'm reading uses int main(void) but in class we usually just use int main() and yet the program runs just fine if I use either one.
Nevermind, forgot that Java casts the result to an int when you multiply a long and an int, which can result in overflow.
[QUOTE=DrTaxi;46004971]Did you mean addition or multiplication? And what do you mean by "it doesn't work"? Wrong result? An error message? What language is that, anyway?[/QUOTE] Wrong result. C#. I want to execute : a * h / 2
Sorry, you need to Log In to post a reply to this thread.