Changing String To Int - Returning Unwanted 0
Oct 17, 2014
public class Lab07 {
public static void main (String[] args) {
System.out.println(convertToInt("123"));
} public static int convertToInt(String str) {
int conversion = 0;
int i;
[Code] ....
The only methods I am aloud to use is length and charAt, which I have done. I see why it is returning a 0 at the end of the value returned, its because I set my int conversion = 0. If I just declare it I get a compiler error that it must be initialized.
View Replies
ADVERTISEMENT
Mar 7, 2014
I'am trying to converting string data into xml data using xml beans and StringEscapeUtils.This is work fine but in one case it if the data contains special characters.
Code snippet
--------------------
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
public class ParseXMLData {
public static XmlObject parseXML(String stringXML) {
XmlObject xmlObject = null;
[Code] ....
success case
---------------------------
i/p------<aaa><bbb>This converts string to xml</bbb></aaa>
o/p---<aaa><bbb>This converts string to xml</bbb></aaa>
Failer case
-----------------
i/p----<aaa><bbb>This fails if it contains < symbols</bbb></aaa>
expected o/p----<aaa><bbb>This fails if it contains < symbols</bbb></aaa>
Observed that it converts < to '<' but as per xml rules, if data contains '< ' it fails. I want to convert staring and end tags to xml format and if data in b/w middle of starting and ending tags do'n need to convert it. How to do it.
View Replies
View Related
Oct 12, 2014
I am writing a code where in the first method the question will ask whats your favorite website. for example www.javaprogrammingforums.com...when it outputs it will read just "javaprogrammingforums" without the www. and the .com.
Because the program will ask a series of questions in the main, I would like website question to be returned to the main. Here is my code, and what can I do?
import java.util.Scanner;
public class chapter3 {
public static String website(Scanner kb) {
String website;
System.out.println("What is your favorite website?");
website = kb.next();
[Code] ......
View Replies
View Related
May 13, 2014
Write a method that accepts an array of Strings and a character c. The method must return the count of all cs in the strings of the array.
Here's what I have written:
[
public int countC(String[] strings, char c) {
int count = 0;
for(int i = 0; i < strings.length; i++) {
for(int j = 0; j < strings[i].length(); j++) {
if(strings[i].charAt(j) == c) {
count++;
}
}
}
return count;
}]
Does this look correct?
View Replies
View Related
Feb 19, 2015
I run a bash command and save the output as a string object
String str1 = "" +getLastOutput();
str1 would look like this: abcd efgh lkmn xsds
In this string ( str1) i need to check three separate words are there.
If i have an if statement with str1.contains("xyz") && str1.contains("lkmn") &&str1.contains("zzzzzzzzzzzz")
How can i get the one that is not there returned to the screen? so i can use elsewhere?
View Replies
View Related
Jul 31, 2014
I'm training myself with the EJB 3 technology. I would like to create a stateless bean that instead of returning a String, it returns an object. I tried in the same way I did with the first exercise, but I'm getting several errors.
View Replies
View Related
Feb 12, 2015
I want to make a private method that will change the value of a instance variable like this:
String bla = "bla bla bla";
private void changeString (String x) { x = "eee";}
changeString(bla);
Now value of the variable bla should be "eee" but its not, it is stil "bla bla bla".
View Replies
View Related
Mar 7, 2015
import java.io.InputStreamReader;
import java.util.Scanner;
public class FinalCombinations {
public static void main(String[] args){
//Read number of values
System.out.println("Enter the no of values");
[Code] .....
I am getting output like this
Enter the no of values
4
Enter the values
1
2
3
4
Enter the number for combination
2
C(4,2)=6
1
1
2
1
1
3
1
1
4
2
2
3
2
2
4
3
3
4
where as I want output like this..wit one array
1
2
1
3
1
4
2
3
2
4
3
4
View Replies
View Related
Nov 22, 2014
I can't come up with a simple scenario so I'll use my actual code to explain the problem, and it's going to be related to java.awt.Color, but my problem is general, not awt dependent. Although I will shorten my code to show only what I think is relevant to the problem.
Java Code:
public class GameColor {
public abstract class Effect {
public int strength;
public Effect(int strength) {
this.strength = strength;
}
public abstract Color get(Color color);
[Code] ....
I have my reasons for having a SetHue inner class and a setHue method that practically do the same thing. The inner abstract class Effect and inner class SetHue was static but I changed it when I testing what could cause this problem, later I will need them to be static, but since they don't affect the problem I don't think it matters if they're static or not.
So down to the problem, I create three new GameColor instances which all are 255, 0, 0, basically red. Then I do setHue(0) method on the first one, setHue(20) on the second one, and setHue(40) on the last one. And for some reason all of them have a hue of 40, whereas the last one should've had this, so basically the last time I call setHue on the instances of GameColor, changes the hue of all of these colours to that hue. I want it to be individually affected, but all of them are affected. This is where I apply these changes:
Java Code:
colo1.setHue(0);
colo2.setHue(20);
colo3.setHue(40); mh_sh_highlight_all('java');
And how I initialise the colors:
Java Code:
public GameColor colo1 = GameColor.RED;
public GameColor colo2 = GameColor.RED;
public GameColor colo3 = GameColor.RED; mh_sh_highlight_all('java');
View Replies
View Related
Mar 20, 2014
public class werek4d {
public static void main(String[] args) {
int counter = 1;
int[] anArray = new int[101] ;
for (int i = 0; i <= 99; i++){
anArray[i] = i + 1;
System.out.println(i + ": " + anArray[i] + " ");
[Code] ....
My aim is to generate a lists containing 1 to 100. I will then count the number of integers divisible by 3. After doing so, I want to delete the integers that are NOT divisible by 3 in the lists. I tried doing it, but I seem to keep on getting the same lists.
View Replies
View Related
Oct 29, 2014
I recently began writing a much larger program than I will show. I have boiled the problem down to this SSCCE.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
class TextButton extends JRadioButton
[Code] ....
What i wanted was a JRadioButton with no label carrying around a JTextField. When I added a restore configuration feature in the real program, i found that the text destined for the JTextField was being echoed into the JRadioButton.
Perhaps it is obvious but i sure don't see what I am doing wrong. I am using Java 1.7.0 for development if that makes a difference.
View Replies
View Related
Apr 29, 2014
I'm new to Java and I have a problem with a method, I can't see the code of the method, I just have a jar, but it should return a boolean, something like this:
boolean band = false;
band = TestClass.testMethod("blabla");
// band = false
The problem is that the method seems that is returning nothing (band remain false), and if I initialize band to true:
boolean band = false;
band = TestClass.testMethod("blabla");
// band = true
band remain true, in other words, the value of band is never modified, the question is, how is this possible? because it should return the same value on both calls, true or false, no matter the initial value of the variable that is receiving the returning value of the method.
View Replies
View Related
Apr 6, 2015
This simple program should build a new table with random values and on the getValue method (perhaps called from othe TestClass) return back content of particular cell. why my getValue method doesn't work. Nay! It returns error while compilation.
import java.util.Random;
class TablicaIntowa{
public int getValue(int a){
return tabl[a];
} //getValue method end
[Code]....
View Replies
View Related
Nov 22, 2014
I have created a class and a matrix of doubles (or at least, I think I have, that's partly what I want to verify).I need to return the values of the array,Here is my class:
public class example{
double[][] Position=new double[2][11];
double calculate(){
for (int time=0;time<=10;time=time+1){
Position[1][time]=time;
Position[2][time]=time+1;
double A=Position[2][time];
return A;
}
}
}
I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A).
View Replies
View Related
Jan 4, 2015
I've made a class called Car with a method which will tell me a category for the engine size regarding the actual size (which I've included in the main just so I could see if it works) but everytime I test it I get an error.
public class Car {
public String b;
public String c;
public double es;
public double cs;
public String getCategory() {
if (es < 1.3)
[Code] ....
Figured it out. Was missing parenthesis on audiCar.getCategory();
View Replies
View Related
May 14, 2014
i am trying to run a command in terminal the code is below if i run the command in terminal it works fine however when i run it from netbeans with code below nothing gets printed. however if i run a different command such as (ip addr) it works fine?
public static void a() throws IOException{
ArrayList lister=new ArrayList();
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ps -ef | grep firefox");// the command i am trying to run to get pid of application
InputStream stderr = proc.getInputStream();
[Code] ....
View Replies
View Related
Sep 1, 2014
I'm having an issue returning data from a constructor. This is an assignment, and the specifications were that two classes are to be used, one for the variables and assigning methods, and one for the main method and the printing. This app compiles, but returns "0" for the isbn number, and I'm sure it's because I'm not doing something right with my constructors. My code is below
public class Book {
/* Declare Variables */
public static int isbn;
/* Constructor */
public Book (int isbn) {
isbn = 454545;
[code]....
There are other variables to add, but if I can get one working, I can get the rest working.
View Replies
View Related
Sep 24, 2014
The code is below. The program runs a series of calculations based on data input by the user. My problem is that for the most important thing I'm looking for, total kg CO2 emissions, I continually get an answer of 0.0. What I need is a sum of the individual total emissions as calculated in each method, i.e. the values which are printed with the following: System.out.println(trans); System.out.println(elec); and System.out.println(food);
The total should be something like 25040 or whatever, depending on the value of the inputs provided by the user, but I'm constantly getting a total of 0.0., which is obviously false. Could have something to do with the way I've initialized my variables, or something to do with the limitations of returning values from methods.
import java.util.Scanner;
public class CarbonCalc {
public static void main(String[] args) {
double trans = 0;
double elec = 0;
double food = 0;
giveIntro();
determineTransportationEmission(null);
[code]....
View Replies
View Related
Mar 27, 2014
I want to return multiple values in my EJB query. The query goes something like this:
SELECT SUM(s.sales) SUM(s.cancels) FROM accounts.
To implement this if I write as below:
Query query = _em.createNamedQuery("Accounts.findAccountData");
Now if I do query.getSingleResult(); how do i retrieve both the values since the ejb finder returns either a single object or a collection?.
View Replies
View Related
Jul 15, 2014
For some reason my code returns the memory address of the array when its a print statement with a string, but it works fine when its in a separate print statement all by itself. Why? Do I need to create a toString method that converts a char array to a String to something? The reason why I ask that is becuase on Eclipse line 10 has a warning stating "Must explicitly convert char[] to a String".
public class Ex {
private String word;
public Ex(String word) {
this.word = word;
}
public char[] Display(){
char[] wordChars = this.word.toCharArray();
return wordChars;
[Code] .....
Result:
Hello world
The word is: [C@1db9742
I also tried this, knowing that it's a long shot, but that didnt do anything...
public String toString(){
Ex ex = new Ex(this.word);
char[] word = ex.Display();
String updated = word.toString();//counter intuitive?
return updated;
}
View Replies
View Related
Jan 7, 2014
I have a for each loop that outputs the Mechanics salary for the first team in the arraylist, how can I add these figures together instead of having separate value for each?The code in my testing class to get the salary for mechanics:
Java Code: for (Mechanic str1: formula1.get(0)){
System.out.println(str1.getSalary());
} mh_sh_highlight_all('java');
How can I also get the salary for the driver 1 + 2 in the same team and add their salary to this? I have attaached an image showing the classes/fields created
View Replies
View Related
Apr 10, 2014
public int[] allIndicesOf(E itemSought) {
ArrayList<Integer> toUse = new ArrayList<>();
for (E anArray : container) {
if (anArray.equals(itemSought)) {
toUse.add(container.indexOf(itemSought));
[Code] ....
I have an array list of strings. I want to be able to return an array of integers telling me which indexes in the string array list contain the itemSought object.
View Replies
View Related
Sep 25, 2014
I am working on a project which manages an airport's airplanes and flights based on user input. The method printFlights() - lines 133-134 - is returning null and I can't figure out why. The method is supposed to print information about each flight. The logic is identical to the printPlanes() method which is working successfully.
View Replies
View Related
Jun 16, 2014
When I try to getNextMailItem,it keeps returning null.Why?
public class MailServer
{
// Storage for the arbitrary number of mail items to be stored
// on the server.
private HashMap<String,ArrayList<MailItem>> mailbox;
/**
* Construct a mail server.
*/
public MailServer()
[Code] .....
View Replies
View Related
Jan 12, 2014
I'm following a tutorial and I can't work out why the version that I have written is returning null when reading the text file...?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class AppReadFileFileReader {
public static void main(String[] args) {
File file = new File("test.txt");
BufferedReader br = null;
[Code] ...
I can't see any difference in the code though, and the files being found in order for the null to appear... :?
the text file is within the Java project, and just has three lines :
line one
line two
line three
That's all it say..... But all my program returns is the work 'null'
View Replies
View Related
Jan 23, 2015
I'm writing a program that involves the use of set/get methods. I will submit a sample code of my issue. The format appears to be alright however the program is not returning any data. This is the same format as my actual code. Why this might be happening?
Java Code:
public static void main(String [] args) {
SecondClass object = new SecondClass();
object.setName("Name");
object.getName();
} mh_sh_highlight_all('java');
Java Code:
public class SecondClass {
private String name;
public SecondClass(){
name = " ";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
} mh_sh_highlight_all('java');
View Replies
View Related