|
Well, without your explaining how each String in the array should become an int, we can't really answer. Are they Strings like "1", "2", "3"? Are they Strings like "one", "two", "three"? Are they "red", "blue", "green" and the ints should be indexes into some list, somewhere? Without our knowing how each single String should be converted to an int, there's ... |
|
i have a for loop and now i dont know how i would add the int values in an array? for( int i = 0; i < input.length(); ++i ) { int n = Integer.parseInt( input.substring( i, i+1 ) ); IO.println(n); } i need it to add the vaules together and then see if its a % of 10 and if ... |
OK, I'm about to go crazy! I wrote a class that compares ClockTime - I set the class up using arrays. I want to compare two ClockTime objects and test for equality. My instance variable looks like: private int[] myTime = new int[3]; My default constructor looks like: public ClockTime() myTime[0] = 0; myTime[1] = 0; myTime[2] = 0; And my ... |
Since an int and a char are both just integral primitives in Java, within the range of a character's valid values, they are indistinguishable... except for the type of the variable. If the array is defined to be a char[], anything that the compiler KNOWS to be an int will give a compile error if you try to assign it directly ... |
|
|
I am writing a method for a CS assignment that is supposed to take in an array of ints and return the array shuffled. I read elsewhere on these boards where someone explained how to do this but with strings. I tried to follow their code making the appropriate changes for ints but I am having a problem. public static int[] ... |
What do you mean by "sum two int arrays"? Does this mean to sum each corresponding entry? This seems the logical choice since it mimics vector arithmetic from mathematics and physics. What if the two arrays are different lengths? So the first step to answering "How do I..." questions is defining what the operations mean. In this case, a simple for ... |
I was making up some code to demonstrate my solution. X was the array's sample component type. You have your own component type and your own index variables. My point was that instead of trying to write things like array[false][y] or array[true][y], use 0 for false and 1 for true, for example. [ June 05, 2006: Message edited by: Jeff Albertson ... |
Local variables -- variables defined in a block of statements -- always must be initialized before use. But member variables -- those defined directly in the body of a class -- are automatically initialized to 0/null/false when the object is created, so you're not required to initialize them yourself. If that doesn't clear it up: can you show brief examples of ... |
|
why they designed main should only have String array? Because if both "public static void main(String[] args)" and "public static void main(int[] args)" were permitted, you would get all kinds of strange issues. Suppose you write a program that has both of the methods: public class Example { public static void main(String[] args) { // ...do something... } public static void ... |
I have an array of int type, I want check that array to see if it has equal elements for example: {1,1,1,1,1} return true, otherwise false. I have written this code, it works well but I want know if its good or there better algorithm. public boolean testEqual(int[] array) { int a = array[0]; for(int i = 1; i < array.length; ... |
Originally posted by Ryan Waggoner: Just to make things easier, or make everyone happy? Or is there a better reason? We might be speculating about what was on the minds of the compiler developers; but I think I've read somewhere that aiding the transition from other languages was involved in the decision - like Stan said. The only other possible "better ... |
lets say we have int[] c = new int[5]; this is later filled with 5 different values from 2 to 14 (repetition is allowed) I need a way to find out how many numbers are repeated and how many times this number is repeated. lets say the array was { 3 , 12 , 3 , 3 , 12 } 3 ... |
|
Chan , char always assume the ASCII value if its surronded by sinlge quotes say '0' , otherwise if not specified it assumes as number ( and no need of -48 ) and when casting int to char , it assumes as ASCII value and certainly you cannot get desired output . char is not good agent when to use numbers ... |
Relatively new here, and working on an excercise from a book that involves creating an array of random integers. I'm trying to write a method that will remove all instances of a particular integer from the array, which I've called list[]. It seems to work fine in most circumstances, but sometimes I get weird results. Here's the code: //-------------------------------------------------------- //removes all ... |
|
Not that it matters too much most of the time, but you should always use ++i, not i++, in loops like this, because i++ implies just a tiny bit more work for the computer. ++i means "add one to the variable, and use the result", while i++ means "make a copy of the variable, increment the variable, and use the copy." ... |
|
I am trying to validate an array of int data to check if the number is a certain number, ie. a mobile number 0 7 7 7 1 8 7 6 5 4 3. So far the only way I can do this is to iterate through the array and check if each value matches one of the above numbers, eg. ... |
I need to be able to load an array with random integers. Here is what I have so far and I was wondering if someone could provide a little insight as to what I'm doing wrong or maybe point me a bit more in the right direction. The method I have currently gives me the error java.lang.ArrayIndexOutOfBoundsException: 10 at Lab9b.loadArray(Lab9b.java:17) (where ... |
Hi all, I need to make a recursive function that can make me all possible combinations of an int array, I don't want to use a for loop because the size of the array is user input. Could anybody please guide me thought this because I have no idea how to implement a recursive function. ex: int array[] {1,2,3,4} possibilities : ... |
Hello, so I am in a Java class and there is this program that I am struggling to even understand how to do. There is a Survey Array that takes values from a survey file stored on one's computer (simply known as "survey" with a TXT extension). Each Survey value is composed of 5 elements: "MONTH, DAY, HOUR, MINUTE, ANSWER" and ... |
|
Raj Kumar Bindal wrote:I need to search only once. But, i was just thinking if there can be any better approach than using linear search.May be if we can use some data structure to optimise the performance. Absolutely, you could use some data structure. But that wasn't what you said. You said "I have an integer array. Elements in this array ... |
Hi all, I recently took an interview and got a challenging question on integer arrays. Basically if you have an int array of both positive and negative and you want get a sub array where the total is the largest sum possible, how would you do this? The interviewer said he needed the beginning and ending indecies from the original array. ... |
|
Hey all! I'm trying to run a chat application where a client (aside of chatting) can ask to calculate the factorial of a number. For the number 12, the client types for example "factorial:12" When I get the numbers I put them into an array and then convert them into a string, ultimately converting them back into ints, afterwards which I ... |
I've been staring at this for hours, trying this and that, and still don't see what's wrong. I know that it's ok to: int newvalue = Integer.parseInt(new String(char[] something)); But in the context of this code, I get a NumberFormatException. As you can see, I've been looking at the character array and String and don't see any problem with it. When ... |
Hello, I'm trying to generate 50 random integers between 0 and 10 using a 'for' construct and the 'Math.random()' method. I know how to do this, but I don't know how to store all these integers in an array (one dimension). I really only know how to store integers by manually inputting them into the array. I haven't made much progress ... |
Hi Friends, I work for a cable company. I am sending channels to setop box using my application. I could able to send channels like 999,123,345 etc. There is a requirement to send new channels like 4-1,11-1. How could i save the "-" (hypen) into integer array. My remote program expects integer as parameter. Please help. Thanks, Siv |
I am not sure what is wrong with my code Basically, for whatever int I put in, I get the biggest prime number (that is smaller than that int), print the amount of times that there are prime numbers in the array. I very much appreciate anyone who can help me out. import java.util.Scanner; public class aa { public static void ... |
Hi, I've an array of tCost=0 2 3 3 0 1 2 1 0 0 5 2 1 0 3 3 2 0 0 2 1 3 0 1 5 4 0 0 1 3 2 0 1 2 2 0, and I would like to map it into 3 dimensional matrix, tCost1[j][i][k] for(i=0; i<5; i++) for(j=0; j<3; j++) for(k=0; k<3; ... |
I'm trying to compare a 2 dimensional array of Strings with a String variable and for some reason I get a null pointer exception when I use this method and I get error message at the line that contains if (rows[0][x].equals(code)) public int itemPrice(String code) { for(int x = 0; x<5; x++){ if (rows[0][x].equals(code)){ price = rows[2][x]; } } int cost ... |
|
@carderne: if you really want to help, then please put some effort into it. Sending someone on a wild goose chase is not very helpful. If you can't remember what the name of the method is, then take the effort look it up. Integer.rand() is in no way or form even close to math.random(). We all appriaciate any help we can ... |
I was wondering if anyone had a few minutes to look over this code. I'm trying to make a recursive method that will take an array of int and sort the elements so that all even values appear before the odd values. This is not my homework. I am actually trying to teach myself Java. I am not looking for a ... |
|
|
Hi, I'm very very new to Java (just started learning about 3 days ago) and I have a problem. My goal is to convert an int (also negative numbers need to be converted!) into an array. If the int is, for example, -38921, I would want it to be stored in an array like this: {3, 8, 9, 2, 1}. I ... |
I found a way to do it without an array. I am doing something about vampire numbers. http://en.wikipedia.org/wiki/Vampire_number my pseudo code is this Java Code: 1. loop i from 10-99 2. convert i to a sum by adding (i % 10) + (i / 10) and store in a variable names isum 3. loop j from 10-99 4. convert j same ... |
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String Months[] = {"January","February","March","April","Main", "June", "July", "August", "September","October","November","December"}; System.out.print("Please enter an integer value for the month (e.g., 2 for February) : "); int monthvalue = keyboard.nextInt(); while (monthvalue<=0 || monthvalue>=13) { System.out.print("You have entered an invalid month entry, please enter a value from 1-12:"); monthvalue = keyboard.nextInt(); } if(monthvalue == ... |
Hi , I'm newbie to Java. Could anyone pls tell me the diff between int and Integer in terms of Array. int[] iarray = new int[10]; Integer[] iarray = new Interger[10]; I know that , first one is primitive and second one is class. The second one requires 80 bytes of memory in order to complete the task, as it stores ... |
|
Suppose you wrote a five digit number on a piece of paper, like say 27972. Suppose you wrote another five digit number underneath it -- say 98374. Now if you only know how to multiply a one digit number by another one digit number (to get a possibly two digit result), do you suppose that you could multiply those two numbers, ... |
Hi I'm new to java but I'm trying to start a project for a class but i am having trouble understanding what exactly the outcome of the specs would do. Im not asking for code but in words could someone tell me what exactly i need to do for this project? here are the specs... "Create a class HugeInteger which uses ... |
Below is my code, im trying to change the code such that the array num[] will store integers, and the 2nd input within the code will store Strings to the array num1[]. Do i have to keep declaring the Scanner and input class? eg. Scanner in =new Scanner(System.in); [code for integer input] Scanner scan=new Scanner(System.scan); [code for string put] Is this ... |
|
|
|
This is taken from sun website, I can't compile it.. Here's the output: C:\Adventa>javac Gamut/*.java Gamut\GamutMatrix.java:90: illegal start of type for (int i = 0; i < ia.length; i++) ^ Gamut\GamutMatrix.java:90: ')' expected for (int i = 0; i < ia.length; i++) ^ Gamut\GamutMatrix.java:90: illegal start of type for (int i = 0; i < ia.length; i++) ^ Gamut\GamutMatrix.java:90: expected ... |
|
Hi everyone, newbie here.. could any one help me to Write a program that converts numbers entered in Roman Numerals to decimal? The program should consist of a Class, say Roman. An object of the type Roman should do the following: a. Store the numbers as a Roman numeral b. Convert and store the number into integer. c. Print the number ... |
Your code confuses me some. A couple of questions: Do you have an array that holds number of vowels and words for certain line? If so did you name your array the same as the local variables? Do you need to pass line as one of your parameters? Do you really mean for vowelCount to be an int and not a ... |
Hi I want to populate an int array (of length 11) with values 0-10 in random order. All numbers from 0-10 must be used up in the indexes and there must not be any duplicate integers. I am finding this concept tricky and haven't found any help on the net. I want to use this array for ordering. Any ideas? Thanks ... |
|
|
|
but your array has data in those indexes...right? As long as the j is an integer and your array has data in there that is an int itself. But to me it looks like you only need to hold the current place of the one clicked and the next one to be clicked...I guess I am not quite sure what you ... |
for (int i = 0; i < generate; i++) { numgen = randomNumbers.nextInt(250); } *bold*//Emliminate Duplicates*bold* for (int z = 0; z < generate; z++) { for(int y = 1; y < generate; y++) { if (numgen[z] == numgen[y]) { numgen[y] = randomNumbers.nextInt(250); } this is what i have tried to far this allows the program to generate the numbers. I ... |
I'v been trying to convert int to String array and its not working for me my codes are String a[]={"10","24","5","3"}; int b=20; a[0]=Integer.toString(b); this code is ok when I drawString but is't not ok when I want to compare the value in the string exp. String c[]={"20","2","4","7"} if(a[0]==c[0]) //do something can someone please help me |
Taking the bother of assiging array.length to another variable. You do realize that length is a simple field with a set value and so there is no cost using it (as opposed to the size() functions of the collections that will actaully evaluate the actual size), so it makes this assignment frivolous, right? |
I need to create an integer array that is variable meaning I want to start counting at 0 and increment by one as long as there is something in input.nextInt(). For instance, i am reading numbers from a flat file and as I read each number from the flat file, I needed to create an int array starting at 0 and ... |
|
well see im working on a problem that asks the user to input a users first name(string), last name(string) and zip code(int) then at the end I have to sort them in increasing order by zip code. Im trying to even just put each of them in an array but when i get to sorting them, im going to be in ... |
Hey all, I'm a junior in high school and my teacher assigned me a lab to do. Its a program called "Penny Pitcher" where you "throw" a penny onto the board by generating a random number, then replacing the integer with a "P" character. Obviously there is much more to do in the lab, but this is the only part thats ... |
Hi there, i'm not Java expert here so please bare with me. I'm trying to convert an integer (of 5 numbers) to an array. Basically the user enters 12345 and then it would convert it to an in[] = {1, 2, 3, 4, 5}. Any help is much appreciated! (I've taken a look at the API and can't find anything that ... |
|
Looking for a method or help coding a method that accepts an int ar[] as an input array and calculates the median and also one that calculates the standard deviation. They have to begin with: float Median(int ar[], int n) and float Standard Deviation(int ar[], int n) Any help would be appreciated. Code //returns the median value for the array "ar" ... |
No, that's the wrong thing to do. If you want to do that, go write C or Fortran. It's procedural style. Java's an object-oriented language. If those five things are related, encapsulate them together into an object. When you read those in, hold onto them in a better data structure than an array, like a List or Set. % |
|
|
A "List" is Not the same as an "array". You could work with either one but don't mix up the terminology. I don't think there is an "ArrayList". But do you really need a list or array anyhow? Aren't you really just trying to get one valid string for an id #? |
partition(riga, 6, 1, 0); } } the output I get it from is like this: 1 5 1 4 1 1 3 2 1 3 1 1 1 2 3 1 2 2 1 1 2 1 2 1 2 1 1 1 what i'm actually trying to understand how to proceed is to have it with a fixed terms which ... |
kevinj1977 wrote: I am now trying to learn how array's work. Here is what I would like to do. Here is what I envision it looking like: Before that, sort out what tasks you've given yourself to do: 1. I would like to create a simple class that will create an array of 10 integers. 2. I ... want to loop ... |
It's a school project, and according to my instructor we're supposed to use arrays. If I could take the users input (1,2,3,4,4,6,4,2) for instance, and then sort it, remove duplicates using a HashSet, then put it back into an array to do the rest of my calculations, I'm sure she wouldn't mind. But I have learned absolutely nothing about HashSets as ... |
|
|
82. int array forums.oracle.comClose, but why guess? Have a read of a textbook or he section on [Arrays|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html] in Sun's Tutorial. In fact here are two cases: if you are creating a new array then you need to use the "new" keyword. If you are reusing an old array you have to assign the element values one by one - there's no shortcut notation ... |
My problem is iterating through an array (recursively of course). How to change the array each time I call the function? How to implement changes in the array? I feel like there is some functionality with arrays that I'm missing and was hoping it would be apparent to someone on these message boards. FJ |
|
I don't think so. According to the API, it says: "Returns true if the two specified arrays of ints are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the ... |
pb, Dude, no offence, but that code is seriously screwed up bowl of scrambled spegetti, with egg, cheese, bacon, and lime juice... The lime juice is a sure sign that you've been hacking it here there and everywhere, with ever increasing desperation, instead of writing it from top-to-bottom as you would an essay... My advice is to chuck it out, and ... |
Alright, I am having trouble inserting information into my int[] array. I initiated that are as follows: int[] test = null; and I made a for loop that every time it found what it was looking for would insert into the array. for (int i = 0; aSearch.indexOf(mNeedle, searchTmp) != -1; i++) { test = aSearch.indexOf(mNeedle, searchTmp); searchTmp = aSearch.indexOf(mNeedle) + ... |
Hi to All, I am basically creating a very simple application. The part that is giving me some difficulty is how to create and place an array object onto the User Interface to allow the user to enter as many integer values as needed. It would be great to have a label and a field for input, and as the user ... |
For a small number of small strings, that will be okay, but as the number of strings and their sizes grow, you'll end up creating and GCing lots of objects, and spend lots of time copying from one String to another. In general, when building up a String in a loop, you'll want to use StringBuffer or StringBuilder. |
It has 256Mb so I agree with you there, it's a lot for two 300 value long arrays. But if it's continuously doing this for every player multiple times with about 150Mb already used by the rest of the server, it may cause problems. I probably need to give a more detailed description of the server architecture, but it's not that ... |
hi, i'd like to have an int array to contain sequential values from 0 to 99 (100 numbers) without using a loop. i somehow remember i had done this before but can't now recall how it was done. is my memory faulty? if it can't be done at initialization, can it be done without a loop? thanks |
I don't know if this topic is in the right place but I just have one quick small question that I can't seem to find the answer to. Is there a built in java class or method that you can call where it returns an int array of length [variable] where all the ints are randomly generated and where none of ... |
Basically I have a 2d of 9 by 9, each with a number from 0 to 9 in it. I have also written code that displays any zeroes as periods. I want to make an array of strings from this 2d array of integers, then make that an array. I think what I need to do is have my INPUT be ... |
|
Use a DataOutputStream . The code it uses is very similar you yours so don't expect a dramatic performance improvement but I would not expect it to be much slower than writing the same number of bytes. Are you linking to a BufferedOutputStream? That could give a dramatic performance improvement. Edited by: sabre150 on Aug 13, 2009 7:10 PM |
hello NG! i am working on a class that manipulates graphics (independend from swt or swing) therefore i pass the data as array to my class (int[] color values) internal i want to use Graphics (this way i could easily draw lines etc) i did not find a way (already tried google) to convert an array to graphics and vice versa... ... |
|
|
|
consider my static method below - I want my method to print out all numbers in my array, named ArrayOfNumbers .... Since I do not want the empty spaces to be printed, I have used an if statement inside the for loop to make sure empty spaces are skipped(and not printed) public static void printNumbersToScreen() { for(int i = 0; i |