I am trying to build something which needs an array of numbers entered by the user. The problem is, I don't know how to do this. I was thinking about using a JTextField but they are in string format not numbers... (I am struggling to word this out :\ ) The textfield's getText method is designed to get a String not an array. Can someone please help me? I will be EXTREMELY grateful if you do as I have been trying for ages :) Thanks in advance.
The textfield's getText method is designed to get a String not an
array
Yes Java has String as it's first Love.
Let me give you an Idea how can you do it.
As you have mentioned you need to use JTextField
Enter Numbers Using Separator (Ex. 1,2,5,60)
Retrive it and Store it as String
Use split() function to get all numbers in String array.
Use Integer.parseInt("1") method to convert String to int.
For Example:
String str=jt.getText();//JtextField Has text as 1,3,4,55,10
String strA[]=new String[5];
strA=str.split(",")
int intA[]=new int[5];
for(int i=0;i<array.length;i++)
{
intA[i]=Integer.parseInt(strA[i]);
}
NOTE: It will only work for specific numbers say here I am passing 5 but if you pass 6 than it will give you IndexOutOfBound Exception.So in that case it would be better to use ArrayList
Related
Hello I have an array list that consists of numbers but are strings.
Like this: ["1.1",1.2]. The problem is that I am running into that I am using a for function to split an item of the array list into a string array.
For example if I have array list: ["1.1","1.2"] I would
String[] string_array= array_list.get(0).split("\\.");
That should be working fine, but the problem that I am facing is that it would create a weird string array like [Ljava.lang.String;#ccac396 .
I am not really sure what causes this problem as I used split and array string before and none of this happened.
Note: I am splitting on . ,but since . is a regex I have to use \\. which should split on ..
Here is the part of the code that is causing the problem:
for (int i = 0; i < array_list.size(); i++) {
String[] splitted_on_for_for_reverse;
splitted_on_for_for_reverse = array_list.get(i).split("\\.");
array_list.remove(i);
String string_to_add = splitted_on_for_for_reverse[1].concat(".").concat(splitted_on_for_for_reverse[0]);
array_list.add(string_to_add);
}
I know the problem is not happening from the array list as I have tested it with logs and its fine. the problem is with the string array but I dont know what to do. Any help is appreciated and thanks in advance
[Ljava.lang.String;#ccac396 is not a the string itself, but its location or the location of the string array, respectively. I think you are you printing the whole String[] array, which results in the weird output. Java uses its default toString() method, which often just returns the objects position/reference.
Try to iterate over this array and print each String separately.
To print a string array you must use
Arrays.toString(string_array);
I am doing my assignment and one off it require a confirm dialog box to check if the people is single or married. But the question state the I'm not allowed to use array. I have search other answer but it all used array.
How to change the text in showConfirmDialog without using array?
See JOptionPane.showConfirmDialog(parent,message). E.G.
int result = JOptionPane.showConfirmDialog(parent, "Are you married?");
Below is the code.\ I imagine you use a for loop and then another but I cannot seem to make it work. I attempted research however most topics were too complex since I am a novice. I'm trying to find a way to get the fifth character out of each string within the variable. I'll use the information given to me so i can then solve the rest of my program. I have more to do
public static void main (String[]args)
{
String[] decoder = {"Nexa2f5", "Z52Bizlm" , "Diskapr" , "emkem9sD", "LaWYrUs", "dAStn78L", "mPTuriye", "aaeeiuUu", "IL8Ctmpn"};
int character = 4;
for(int i=0; i<=decoder.length-1; i++)
{
}
}
I am trying to get the third and fourth characters of the odd numbered Strings. I am trying to put the letters into an array and decode the message. I am also trying to print the 5th character of all other words. I'm having issues commenting right and I've tried to reply a couple times but no dice.
Within your for loop use the array indexing notation. For example, String current = decoder[0] results in current having the value Nexa2f5. Once you have the String Object (in my example, I named it current) you can use the charAt() method shown in the String class documentation to get the 4th and 5th characters. If you need more help than that, read my comments then update your code and ask another question.
I've been reading up on it, but every question I've found has asked for slightly different things, such as only wanting a single letter for their array, or in a different language (I'm new and only learning java at the moment), so here I am.
I want to set up an array that uses the user's input for their names.
What I have so far is this, I'm assuming this is the declaration line, where later I use an input line to define a value within the array (which I also am unsure how to do)
String[] array = {"name"};
But I don't know how to for example print.out the object or keep up with which name will be what value. I appreciate your time taken to teach me!
EDIT for further clarification. I'm trying to write up a small app that asks the user for numerous names, addresses, and phone numbers (Type name -> Type name's address -> type name's phone number, ask if they want to add another person, if yes then go back to asking for another name)
I am unsure how to set up a String array or how to use it throughout. However, thanks to your input and coming back after some fresh air, I have a better idea how to word it for google. Thank you guys for your help, even if it was just to gesture a better articulated question.
An array is a sequence of values. You have created an array of Strings that is one String long. To access the value at a specific of an array, use array subscript notation: the name of the array followed by a pair of square brackets ([]) with the index in between them.
String[] anArrayOfStrings = {"string0", "string1", "string2"};
anArrayOfStrings[0]; //the first element
System.out.println(anArrayOfStrings[1]); //print the second element
anArrayOfStrings[2] = "new string value"; //assign the third element to a new value
if (anArrayOfStrings[0].equals("string0") //evaluate the first element and call a method
{
//this block will execute anArrayOfStrings[0] is "string0"
}
anArrayOfStrings[3]; //error, index out of bounds
Simply declaring the array would be
String[] names;
In your code you both declare and assign it in the same line by using an initializer list.
To assign individual elements, use the [] notation. Note that once you initialized you list to be only one String long, it cannot become longer than without be re-assigned. To declare an array of any size, you can use:
String[] arrayWithInitialSize = new String[5]; //holds five strings, each null to begin with
I'm very new to Android development and Java. This is probably a simple answer but I cant seem to find the info anywhere. I know I can pull data from my XML by using:
Double.parseDouble(example.getText().toString());
but what I need to to is then extract the data and add it together.
Example: I pull the data and its 1234 then I need to add 1+2+3+4. Thanks for the help.
I'm not sure I understand the question, but if you want to add all the digits in a string then you would do this:
(pseudocode)
length = number of characters in string
runningtotal = 0;
loop through all charcaters in string, 0 to length-1
extract character at each loop position
runningtotal += Integer.getString(character);
But I suspect this is not actually what you're trying to do. More info?