I have this code in eclipse:
String A = String.valueOf(a);
String B = String.valueOf(b);
String C = String.valueOf(c);
String D = String.valueOf(d);
String E = String.valueOf(e);
String F = String.valueOf(f);
String G = String.valueOf(g);
String H = String.valueOf(h);
String I = String.valueOf(i);
String J = String.valueOf(j);
String K = String.valueOf(k);
String rawpassword = A+B+C+D+E+F+G+H+I+J+K;
int password = Integer.parseInt(rawpassword);
System.out.println(password);
And it gives me this error
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
at com.jakibah.codegenerator.Main.Generate(Main.java:65)
at com.jakibah.codegenerator.Main.run(Main.java:24)
at java.lang.Thread.run(Thread.java:745)
But I do not understand why.
can someone help me?
String A = String.valueOf(10);
String B = String.valueOf(10);
String C = String.valueOf(10);
String D = String.valueOf(10);
String E = String.valueOf(10);
String F = String.valueOf(10);
String G = String.valueOf(10);
String H = String.valueOf(10);
String I = String.valueOf(10);
String J = String.valueOf(10);
String K = String.valueOf(10);
String codestring = A+B+C+D+E+F+G+H+I+J+K;
BigInteger bigInteger = new BigInteger(codestring);
System.out.println(bigInteger.max(bigInteger));
The parseInt(String s) method throws a NumberFormatException if the argument is not a parseable Integer.
Make sure the String you pass to the method is a Number and is between -2^31 and 2^31 - 1
Number Format exception arise when you trying to convert a string type into a integer but that does not fit as a integer.
From your code I am not able to understand what value is a,b,c,d,..
From my experience I have uploaded two image to show you may be this kind of error you done
here number format exception arise as codestring is 10.320 and it is a string type so when compiler try to convert it as a string it unable to convert it due to .
But in this scenario codestring is 1020 , so easily it convert to a int.
Related
I am trying to remove the non-number characters from my string.
I have tried using the .replace() method but this returns with the error:
The method replace(char, char) in the type String is not applicable for the arguments (String, String)
Code:
Properties p = new Properties();
File f = new File("coords.txt");
if (f.exists()) {
FileInputStream in;
try {
in = new FileInputStream(f);
p.load(in);
in.close();
} catch (Exception e) {
System.err.println("Failed to load coordinates");
System.err.println(e.getMessage());
Button.waitForAnyPress();
System.exit(0);
}
} else {
System.out.println("No coordinates found");
while (!Button.ESCAPE.isPressed()) {
Thread.yield();
}
System.exit(0);
}
When I print out the string gg, initialized like:
String gg = p.toString();
I get the output: Object020f458.
My computer highlights the error on the replace:
gg = gg.replace("{", "");
gg = gg.replace("=", "");
gg = gg.replace("}", "");
int commaLoc = gg.indexOf(",");
int x = Integer.parseInt(gg.substring(0,commaLoc));
int y = Integer.parseInt(gg.substring(commaLoc + 1));
So I was having a look at NXT and its APIs (JavaDocs)
Btw, I am by no mean an expert on NXT and leJOS, I'm guessing.
You can see by the JavaDoc, that the replace(CharSequence, CharSequence) method is not present.
While I wouldn't solve the problem in such a way, you can try using a StringBuilder to remove the unwanted chars.
See for example a Jon Skeet answer for ideas
https://stackoverflow.com/a/3472705/1392277
You can extract a method such as:
private String removeChars(
final String originalString,
final String charsToRemove) {
final StringBuilder stringBuilder = new StringBuilder(originalString);
int i = stringBuilder.indexOf(charsToRemove);
while (i != -1) {
stringBuilder.replace(i, i + charsToRemove.length(), "");
i = stringBuilder.indexOf(charsToRemove, i);
}
return stringBuilder.toString();
}
problem:
gg = gg.replace("{", "");
gg = gg.replace("=", "");
gg = gg.replace("}", "");
error message:
The method replace(char, char) in the type String is not applicable for the arguments (String, String)
Please try to replace with Character.MIN_VALUE:
Replace " with ', then replace all '' (, since java "doesn't like the empty character literal") with Character.MIN_VALUE):
gg = gg.replace('{', Character.MIN_VALUE);
gg = gg.replace('=', Character.MIN_VALUE);
gg = gg.replace('}', Character.MIN_VALUE);
Character.MIN_VALUE is not empty character, but closest to it :), and converts (with a String.replcae(char,char) test):
{foo=bar}
to:
\u0000foo\u0000bar\u0000
...which appears hard to copy&paste, but "looks like blanks" :)
Since the String I was using was actually using was a property I had to retrieve it like a property which will format it itself
int Count = Integer.parseInt(p.getProperty("Number_of_properties"));
for(int i = 1; i < Count; i++)
{
int x = Integer.parseInt(p.getProperty("x"+i));
int y = Integer.parseInt(p.getProperty("y"+i));
}
I also had to change my text file to match the property format:
Number_of_properties = 4
x1 = 150
y1 = 70
x2 = 55
y2 = 77
You can use
public String replaceAll(String regex, String replacement)
This will replace all matching expressions with the replacements.
gg = gg.replaceAll("{", ""); // replace all "{" with "".
gg = gg.replaceAll("=", "");
gg = gg.replaceAll("}", "");
The error must have been highlighted on below two line and not for replace function
int x = Integer.parseInt(gg.substring(0,commaLoc));
int y = Integer.parseInt(gg.substring(commaLoc + 1));
you forgot to put comma in second line. It should be
int y = Integer.parseInt(gg.substring(commaLoc, + 1));
Still it wont work because you are trying substring function with invalid range (as value of commaLoc is -1).
Try replacing those last two lines as below just to make it error free.
int x = Integer.parseInt(gg.substring(6, 7));
int y = Integer.parseInt(gg.substring(7, 8));
want to split the string
String vv = "class="fcg">1.6M";
String[] breakvv = vv.split(">");
String breakvv0 = parts[0];
String breakvv1 = parts[1];
System.out.println(breakvv0);
System.out.println(breakvv1);
Output which i am getting is breakvv0 = an and breakvv1 = class="fcg">1.6M
String vv = "class=\"fcg\">1.6M";
String[] breakvv = vv.split(">");
String breakvv0 = breakvv[0];
String breakvv1 = breakvv[1];
System.out.println(breakvv0);
System.out.println(breakvv1);
public static void main(String args[]) {
String vv = "class=\"fcg\">1.6M";
String[] breakvv = vv.split(">");
String breakvv0 = breakvv[0];
String breakvv1 = breakvv[1];
System.out.println(breakvv0);
System.out.println(breakvv1);
}
}
For the line data[] = {nam, addres, adminStaf, phoneNumbe, typ}; NetBeans gives me this error
"; Expected Not a statement"
I don't know why this is not working, help will be appreciated.
public Object[] getObjects() {
Object[] data = {};
for (int i = 0; i < Storage.getAgencies().size(); i++)
{
String nam = Storage.getAgencies().get(i).getName();
String addres = Storage.getAgencies().get(i).getAddress();
String adminStaf = Storage.getAgencies().get(i).getAdminstaff();
String phoneNumbe = Storage.getAgencies().get(i).getPhonenumber();
String typ = Storage.getAgencies().get(i).getType();
data[] = {nam, addres, adminStaf, phoneNumbe, typ};
}
return data;
}
The syntax you're using is only allowed when initializing an array. You should do the following instead:
data = new String[] {nam, addres, adminStaf, phoneNumbe, typ};
I have an object declared in this format
StringTokenizer star = new StringTokenizer(st , SEPARATOR); //separator is '/'
Session s1 = new Session(int SessionID, String [] cineName, String[] cineType, String movieName, String date, int time);
int SessionID = Integer.parseInt(star.nextToken().trim()); //for integer
String movieName = star.nextToken().trim(); //for string
String [] cineName = ? //how about string array?
Thus, I wish to ask how I can implement the parsing of a cineName array.
You can store the tokens of a String which delimits tokens with "/" in cineArray as follows:
int tokens = star.countTokens();
for (int i=0;i<tokens;i++)
{
cineName[i] = star.nextToken().trim();
}
String [] cineName = star.nextToken().trim().split(CINE_NAME_ARRAY_SEPARATOR);
this method runs an exception and i didn't find why.
private void loadTrace () {
BufferedReader reader = new BufferedReader(
new StringReader(logTextArea.getText()));
String str;
try {
while(reader != null)
{
str =reader.readLine();
String [] splitted = str.split("\\|");
String b = splitted[1].trim();
String c = splitted[2].trim();
String d = splitted[3].trim();
String Chemin;
String Type = "action" ;
String Description;
if (d!=null) {
Description=d;
}
else Description ="Afficher onglet";
if (c!= null) {
Chemin= b+"."+c;
}
else Chemin =b;
String trace =Type+" "+Description +" "+Chemin ;
ArrayList<String> p = new ArrayList<String>();
p.add(trace);
System.out.println(p);
}
}
catch(IOException e) {
e.printStackTrace();
}
}
Without knowing the exception I can guess one of the potential issue is in these lines:-
String [] splitted = str.split("\\|");
String b = splitted[1].trim();
String c = splitted[2].trim();
String d = splitted[3].trim();
You are accessing splitted without checking if it is null or size so you may run into ArrayIndexOutOfBound exception if splitted length is less than 3. So modify the code this way-
String [] splitted = str.split("\\|");
if(splitted!=null && splitted.length==3){
String b = splitted[0].trim();
String c = splitted[1].trim();
String d = splitted[2].trim();
}
The NullPointerException you get now that you've fixed the ArrayIndexOutOfBound is because of the test you use in your while loop:
while(reader != null)
{
...
}
reader will always be non-null so this loop will never end. You need to test if
reader.readLine()
returns null (indicating EOF).
I guess you get an ArrayIndexOutOfBoundException, right?
(You need to tell us, what Exception you receive)
The problem could be in the following lines. You should check the size of the array and not just "hope" that it has three parts.
String b = splitted[1].trim();
String c = splitted[2].trim();
String d = splitted[3].trim();