Problem with parsing String convertion to int in java - java

Please help me for conversion my line of codes are mention below:
String deptName = "IT";
String dept_test = request.getParameter("deptName").trim();
System.out.println("Dept name vlue is"+dept_test);
// problem here for casting...
int dept_id = Integer.parseInt(dept_test);

I don't see any casting problems. If your text doesn't contain a parseable number you will get a NumberFormatException which you may need to catch with a try/catch block. What exactly is the problem you are having?

From looking at your code the most you get will be an error parsing the input (including if it is none. What may be an issue is that the variable deptName is never used. Did you mean to make the second line
String dept_test = request.getParameter(deptName).trim();
(notice no quotes) instead?

Related

Replace String with characters with substring

I tried to make security to display email data by replacing some words with symbol (*) but not as expected there might be an error in making the example script as below.
String email = "thismyemail#myhost.com";
String get_text = email.get_text(3, 6);
String hasil = email.replace(get_text,"*");
email_string = (EditText) findViewById(R.id.emailT);
email_string.setText(hasil);
But the result is like this
thi*email#myhost.com
Which I expect
thi***email#myhost.com
String hasil = email.replace(get_text,"***");
But please note that if that text appears anywhere else in the string it will be replaced as well.
Also, if the email is like jf#mymailserver.com you won't be replacing a part of their user id with *.
So you can probably find a better way to select the characters, taking into account email length and also not "replacing" text but rather putting those chars at the specific position you want to.
See this related question for some ideas on how to improve this:
masking of email address in java
Your code seems right. If ur expected output is like the one mentioned above, you can just add 2 more "*" to the code.
String hasil = email.replace(get_text,"***");
I hope it helps

java substring returning empty

I'm attempting to take a substring of a string txt and then set another portion of that string to a value, however whenever I try to set voltage[t] with the double value of the substring, I'm getting an empty string error. Here's the part of the code where I'm getting the error:
if(txt.substring(0,1).equals("1")) {
//Voltage button pressed(S3)
//=====================================================================
text3.setText(txt.substring(1));
voltage[t] = (Double.parseDouble(text3.getText()));
}
Anyone know why this error would be occurring? Any help would be appreciated. Thanks!
Edit:
Here is the exact exception I'm receiving:
Caused by: java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at UartApp$11.run(UartApp.java:728)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
The error occurs on line 728
Also, here's the code I'm using to convert the int and adding the "1". The microcontroller is programmed in C:
char *p, text[32];
int i = readADC(POT);
sprintf(text,"1%d", i);
p = text;
UartTxString(p); //Sends string out
If the code you provided is complete and representable, then Double.parseDouble(...) would throw an exception indicating an empty string if and only if txt is the string "1" (and nothing else).
Assuming you want to get the double value of "1", then the way you would fix this is by changing text3.setText(txt.substring(1)); to text3.setText(txt.substring(0,1));.
If instead what you want to do is to get the double value of anything in the string after the leading "1", then I think you may have a problem elsewhere in your code--wherever it is that you set the value of txt. If so, could you post that code as well so that we can take a look?
(I think what may be happening is that you intended to prepend "1" to txt, but instead accidentally replaced the value of txt with "1" entirely)
EDIT:
The problem may be with text3 instead.
Try this. Replace
voltage[t] = (Double.parseDouble(text3.getText()));
with
voltage[t] = (Double.parseDouble(txt.substring(1)));
EDIT #2:
If the above replacement did not fix the problem, then it must be true that txt is exactly the string "1" (and nothing else) within this scope. The problem may lie in how you are prepending the '1' to txt.

how to fix a java.lang.NumberFormatException error where the desire input is string?

i'm using Jframe as my front-end for an inventory system i have developed. I'm getting a "java.lang.NumberFormatException: For input string:"6seater"" but the variable is declared as a string so i'm a bit confused as to why this error is coming up
private String Eng_num, Chasis_num, make, model, year_of_car,capacity,description;
private Integer status,Sup_id;
public void actionPerformed(ActionEvent e)
{
Insert I = new Insert();
try
{
Chasis_num = textField_1.getText();
Eng_num = textField_9.getText();
year_of_car = textField_10.getText();
model = textField_11.getText();
make = textField_12.getText();
capacity = textField_14.getText();//error is at this line
description = textField_16.getText();
Sup_id = Integer.parseInt(""+textField_13.getText().toString());
status = Integer.parseInt(""+textField_15.getText().toString());
I.insertVehicle(Eng_num, Chasis_num, make, model, year_of_car, capacity, Sup_id, status, description);
}
I even try to put .toString and still getting the same error
capacity = textField_14.getText();
I don't think this is the cause of your exception.
java.lang.NumberFormatExceptionOnly occur when you try to parse String into any kind of Number.
So, i'm guessing, this exception was thrown somewhere you try to convert 6seater to Int or some other number format.
I'm getting a "java.lang.NumberFormatException: For input string:"6seater"" but the variable is declared as a string so i'm a bit confused as to why this error is coming up.
The error is happening because you have tried to parse the characters 6seater as an integer. It isn't an integer. An integer consists of the characters 0 through 9, possibly with a - character at the front. Any other character, and the value will be rejected ...
(The problem is nothing to do with the type that getText() returns. The problem is the value that you are giving to the parseInt method. It is not clear where the parseInt call is. A stacktrace would answer that ... but you didn't provide one.)
Also, you say:
capacity = textField_14.getText();//error is at this line
Actually, it isn't. That line cannot possibly throw a NumberFormatException. In reality, the error could be happening at one of these lines:
Sup_id = Integer.parseInt(""+textField_13.getText().toString());
status = Integer.parseInt(""+textField_15.getText().toString());
or it could be happening within the the insertVehicle method that you are calling here:
I.insertVehicle(Eng_num, Chasis_num, make, model,
year_of_car, capacity, Sup_id, status, description);
I should also point out that you have made some egregious Java style errors in your code:
Java class, method or variable names should never contain _ as a separator. Use "camel case".
A Java variable name should never start with an uppercase letter.
(If you instructor doesn't deduct "style" marks for this, he/she should. If your code reviewers don't pick this up, they are not doing their job properly. If this code was intended to be delivered to a paying customer, they would have reason to complain about the code quality ...)

Java: reading a string in a particular format

I am not posting any code I am struck with. I am trying this in Java:
Issue:
I have words like:
,xxxx-1223
yyyyy,xxdd-345
$,xxxxr-7
sdsdsdd-18
so what ever format I have I should be able to read the last one:
xxxx-1223
xxdd-345
xxxxr-7
sdsdsdd-18
what so may be the words, all I need to to get the words as shown.
Use String#lastIndexOf(int) to find where the last comma occurs, and use String#substring(int) to get the rest of the string that follows.
String input = /* whatever */;
int lastComma = input.lastIndexOf(',');
String output = input.substring(lastComma + 1);
String[] str=yourWord.split(",");
String output=str[str.length-1];
You can use this Regex: -
(\\w+-\\d+)$
Or this specific problem can simply be solved using String.split() or String.substring(int) methods

Trimming string removes more than needed

The below code is meant to trim my string. Unfortunately it's removing more than the 'conf' value.
Did I do this correctly?
What this is doing is adding a double html response address, so i'm removing the first url which is 'conf'
String pageIdUrl = response.getRedirectUrl();
if(pageIdUrl.startsWith(conf.toString()));
{
pageIdUrl = pageIdUrl.substring(conf.toString().length());
}
It would appear that there is a misplaced semicolon that is stopping your if statement from working as you expect.
String pageIdUrl = response.getRedirectUrl();
if(pageIdUrl.startsWith(conf.toString()))
{
pageIdUrl = pageIdUrl.substring(conf.toString().length());
}
Console.WriteLine(conf.toString());
Console.WriteLine(pageIdUrl);
if(pageIdUrl.startsWith(conf.toString()));
^
Note the semicolon here. The then-action is empty: if the condition is met, nothing is done.
{
pageIdUrl = pageIdUrl.substring(conf.toString().length());
}
This part happens whether or not the condition is met, because it's just another follow-on statement. Yes, you're allowed to use braces like that. They create an extra scope for local variable names.
What's happening is that your string doesn't actually start with the conf string, and is getting chomped anyway.
You kept one semicolon after if statement.
String pageIdUrl = "conf:xxx";
String conf = "conf";
if (pageIdUrl.startsWith(conf.toString())){
pageIdUrl = pageIdUrl.substring(conf.toString().length());
System.out.println(pageIdUrl);
}
As others have pointed out, the problem is with the extra ";". Tip: If you're and Eclipse user, I'd recommend setting the Compile/Error Warnings for "Empty Statement" to Warning or Error. Eclipse will then flag this problem in your editor. Other IDE's may well offer something similar.

Categories

Resources