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?");
Related
I'm a beginner who's been desperately trying to understand how to achieve the following thing: I have 3 TextView displaying 3 numeric values (always integers) on screen as text, such as: 50, 100, 200. Then, after some event, I want these 3 strings to be put as values in my PieEntries
in the ArrayList. I can't just get the text from the TextView and put it as a value for the entry, so I tried to use Integer.parseInt to read the Strings as an Ints (since they are actually made of all numbers), or also Integer.valueOf but I only get java.lang.NumberFormatException: For input string: "First" error after the crash. This is the part I'm talking about:
ArrayList<PieEntry> myData(){
ArrayList<PieEntry> myArray = new ArrayList<PieEntry>();
arrayValue.add(new PieEntry(Integer.parseInt(TextView1.getText().toString()), "First"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView2.getText().toString()), "Second"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView3.getText().toString()), "Third"));
return graphValues;
}
I won't paste the whole code here since the graph actually works fine if I manually put values instead of trying to parse the integers from the TextViews.
Also: when I first start to type "new PieEntry", the hint clearly says that it's gonna require a "float value" (then a drawable or a string and so on). Is this where the troubles begin? Should I convert Strings into Integers (or into Floats?) in some way before? I guess there's just like some basic Java rule I'm ignoring, so I'd like to understand what is happening here and how to face this. Many thanks!
As you Explained, you are not getting the TextView text, it is because maybe you have written it somewhere you should not, i.e. provide a Button, when the user types and is done he will hit the button say submit and do this above code on button click.
Solved: I used SharedPreferences to store the new typed data as Integers and retrieve them whenever I need them:
Setting up my Preferences:
SharedPreferences myPreferences;
savedData = getSharedPreferences("myStoredData", Context.MODE_PRIVATE);
Saving an Integer for later:
SharedPreferences.Editor myEditor = savedData.edit();
myEditor.putInt("Label", int);
myEditor.apply();
myEditor.commit();
Retrieving and using the Integer:
savedData.getInt("Label", Default);
Lets see the below scenario.
On the registration form, there is field called "Hobbies" which has three check boxes "Reading", "Dance" and "Cricket".
I have to select two check boxes "Reading" and "Cricket". I have to pass one string value (e.g. "Reading,Cricket") to one method say "selectMultipleCheckboxes". So, based on passing value, it should split the string and it should select two check boxes.
Note: I don't want to select single check box or all check boxes.
Could you please help me to write test script?
I'm not really an expert on Java, but since someone just answered my question, I thought I would at least try to help you out.
You would do something like this:
String originalText = "Reading,Cricket";
String[] parts = originalText.split(",");
String readingPart = parts[0];
String cricketPart = parts[1];
Then you would pass those strings to Selenium:
driver.findElement(By.id(cricketPart)).click();
That would of course be assuming your checkbox's DOM ID was called "Cricket"
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
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 need to get some input from user in my application, and then use it in Java. But, it is quite more complicated than get some value from GUI and assign it to variable. The value should be processed according some rules.
For example:
input from user is string "2 + 3", then he clicks "RUN" button, and when the button is clicked I need to assign "2" to one variable, "3" to next variable, and then make SUM of it.
I suggest you use http://www.beanshell.org/ This tool is used in a number of IDEs with the debugger to evaluate expressions.
Use the ScriptEngine. E.G. here.
=
If all you needed is to make some simple math calculation like these, I would use 2 Stacks to maintain the syntax. You can tokenise the input Strings and then use one Stack as the operators Stack and the other as the value Stack. And then you know that for every one pop from the operator Stack, the next pop from the value Stack must be an integer. If it isn't, you know the rules is broken and you can throw an error to your user.
Here is some code for a four-function calculator.