How to take user inputted number and assign to byte - java

I've got a user input dialog box which I'm using to update a value.
byte valScoreAway = 0;
The value of valScoreAway is displayed on the screen with:
tvScoreAway.setText( valScoreAway );
This works perfectly.
During the program the score will increment when the TextView tvScoreAway is clicked.
This works perfectly.
If there is an error, I have it so that a onLongClickListerner() will inflate a dialog box with an edit field. The user will enter the correct value into the EditView and then click OK. When the OK button is click, I am trying to assign the user inputted value to valScoreAway but it is failing because valScoreAway is a byte type and userInput.getText() is returning a string.
Basically, I need to convert the value of userInput.getText() to a byte type.
Can someone please help me with this?

Byte.parseByte(userInput.getText().toString());

You can convert a edit text input to byte type. By using the following way :
String example = userInput.getText().toString();
example.getBytes();

userInput.getText().toString().getBytes();
//convert string to byte

Why don't you use Integer.parseInt(userInput.getText())? See http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html

Related

How Can I Check , If A User Input (Double) Contain More Than One Point (.)?

First Of All , I Am New To Android Development 😉
Whats Working Fine :-
I Have Created A Calculator, Where User Enter Numbers Using EditText & Select Operator Using Radio button .
On Clicking Of A Button :-
It Creates Variable Double With Value Of EditText
It Returns Respected Answer (In Double) .
Here's Problem Arrive :-
If User Enter More Than 1 Point (.) Ex - [ 5.5.2 ] in Any of the EditText & And Click Button
What Happens Next is a Think Which You Already know 😉
Exactly ! Crash !
So , Is There Any Way To Deal With It ? 😅
Any Suggestion is Appreciated 😌
this not an android problem it's a java problem any way you have to get the text from the EditText as String and then check for the count of the .(Point) in the string
String line = EditText.getText.toString()
int count = line.length() - line.replace(".", "").length();
now if count is more than 1 it will make crash so give the user messae and stop the process else do what you want you are save
It may not be the best solution but its one of the solutions. What you can do is; when user enters the double value, you can convert it into a String using Double.toString() method. and then you can check if it contains more than one '.' (dots).
Pseudo would look like this.
value = get double value from EditText()
str = value.toString()
checkFlag = check(str) //Checks if there are more then one dot
then you cna take decision based on checkFlag value

Getting NumberFormatException for TextView.getText()

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);

How to change the text in showConfirmDialog without using 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?");

Question about the cancel button on a standard JOptionPane

So I am working on a program for school, and part of the assignment is to have a bunch of prompts for input pop up. I am using the JOptionPane, which inherently has an OK button and a Cancel button. Now, to make the program exit when they press cancel when the prompt is asking for a string, I have something like this:
firstName = JOptionPane.showInputDialog("Please enter your first name:");
if(firstName == null)System.exit(0);
But I also have to do the same thing for numbers I get as input, both Doubles and Ints. If I try the same thing, it throws an error saying The operator == is undefined for the argument type(s) double, null. So, what is the best way for me to check if they click Cancel when being prompted for a numerical value? Thanks for your help!
Edit #1
Here is the code for the JOptionPane getting a numerical value:
startDateMonth = Integer.parseInt(JOptionPane.showInputDialog("Please enter the start date month (1-12):"));
JOptionPane.showInputDialog() returns always a string which is the user input. If the user has clicked the Cancel button it will return null. If you want to convert the user input to another type just parse the string. I mean the code you have pasted should remain the same.
If you are asking for a different thing, please clarify.
Petar

J2ME TextField Exception

When I instantiate a textField, I have a number in it that I want to be the default text. The problem is, I can't seem to be able to place that value into the textfield without getting an error. The strange thing about it is that the same TextField is what I use to set the value of the variable containing the number.
TextField myTF = new TextField("Number", value, 10, TextField.NUMERIC);
When I run this code, I receive an exception stating that value doesn't match the constraints of TextField.NUMERIC. However, when I check the vale of the value of the variable, I get the following output:
value = 1234567890
value.length() = 10
The value is set by the same TextField, saved to the phone that I am working on, and when loaded from the phone's settings, throws an exception.
This is definitely a JVM bug. If a TextField returned a string, it must be able to accept it. The only thing I can advice is to play a bit with the size of the field or the constraints. You haven't specified the device you are using, there could be some new firmwares for it with bugfixes.
a potential workaround to your problem could be to instantiate the field with a null value and then set the text afterwards.
TextField myTF = new TextField("Number", null, 10, TextField.NUMERIC);
myTF.setString(value);
I have the same problem. The cellphone is trying to store the field value as an int, and the maximum int value is (2^31) - 1 = 2,147,483,647, which is one digit short of what you (and me) need. Workaround, make your field of type text and set a charset of IS_LATIN_DIGITS. Cheers.
My idea is you try it
String str=""+value;
TextField myTF = new TextField("Number",str,10,TextField.NUMERIC);

Categories