public class MethodExemple {
public static void main(String[] args) {
MethodExemple methodExemple = new MethodExemple();
methodExemple.StrCombine( x: "hello", y:"hongdroid");
System.out.println(methodExemple.StrHongdroid( hong:"hollo"));
}
public void StrCombine (String x, String y) {
String result = x + y;
System.out.println(result);
}
public String StrHongdroid (String hong, String droid){
String result = hong +droid;
return result;
}
}
I coded as I learned in class. However, an error occurred only in my code. The contents of the error are as follows.
cannot resolve symbol 'x' and 'y'
When calling a method in Java, you shouldn't specify the argument names, just provide them in order:
methodExemple.StrCombine("hello", "hongdroid");
You don't need to include x and y when calling the method StrCombine(). Here is the correct way methodExemple.StrCombine( "hello", "hongdroid"); either passing string literals like this or string variables
Related
I am a beginner java coder on an assignment. Please don't report as too vague/duplicate etc, I really need help tailored to me as I have looked at many other answers and just dont understand whats going on!
This bit of code is where my string - e.g. 5x^3 + 2x^2 will be input
String P = Polynomial;
Expression poly = new Variable(P);
Expression diffpoly = poly.derive();
Here is my code where I need the help:
public class Variable implements Expression
{
private double coeff1, power1,newcoeff,newpow;
private String stringpoly;
//public Variable(String[] t)
public Variable(String p)
{
stringpoly = p;
}
public Expression derive()
{
String[] parts;
parts = stringpoly.split("x");
coeff1 = double.parseDouble(parts[0]);
power1 = double.parseDouble(parts[1]);
newcoeff = coeff*power1;
newpower = power1-1;
String newcoeffstr = Double.toString(newcoeff);
String newpowerstr = Double.toString(newpower);
String differentiatedterm = newcoeffstr + "x^" + newpowerstr;
return new Variable(differentiatedterm);
}
public double evaluate(double a)
{
return stringpoly;
}
public String toString()
{
return Double.toString(stringpoly);
}
}
So my issue, is that I am getting errors saying
errors
Can anyone explain these errors for me?
double is a primitive datatype in java. It does not represent an object or a class.
To call methods an object or at least a class is required.
Check the documentations for primitive types here
Check the different wrapper classes and how to use them here
You will have to use the double's wrapper class Double to be able to call the function parseDouble(...)
double parsedNumber = Double.parseDouble("1083407810329");
I am new to java and have been asked to write a program that males three different types of calculations using three different methods. For the purpose of this question I will provide two examples of comparison. For the method that I am stuck on, the following rules must apply:
Must test that x("12 [ 3") returns null because [ is not a valid operator.
Must be written using the following parameters:
Double x;
/*
* Chops up input on ' ' then decides whether to add or multiply.
* If the string does not contain a valid format returns null.
*/
public Double x(String x){
return new Double(0);
}
Here is what I have so far along with an example of another calculation which works fine:
TestCalculator class
public class TestCalculator {
Double x;
String string = "b";
Double doubleObject = 1.0;
double doublePrimitive = 2;
public void testParsing() {
if (multiplyx(12.0) == 60) {
System.out.println("Multiplying Success");}
else {
System.out.println("Multiplying Fail");
}
if (x("12") == null) {
System.out.println("Ovalid operator Success");}
else {
System.out.println("Invalid operator Fail");
}
}
/*
* Chops up input on ' ' then decides whether to add or multiply.
* If the string does not contain a valid format returns null.
*/
public Double x(String x){
return new Double(x) + ("[ 3");
}
/*
* Multiplies the parameter x by instance variable x and return the value as a Double.
*/
public Double multiplyx(double x){
System.out.println("== Multiplying ==");
this.x = x;
return new Double(x * 5);
}
}
Main class
public class Main {
public static void main(String[] args) {
TestCalculator call = new TestCalculator();
call.testParsing();
}
}
My main queries are:
How do I make my method which uses a String as a parameter return a new Double value?
Since anything can be put into Strings as long as they are within the quotation marks, I would I make java recognize "[" as an invalid operator?
Any help on these issues would be greatly appreciated, thanks.
You get your errors because you try to add Double to String.
Please look at this example:
public static void main(String[] args) {
System.out.println(x("12 + 3"));
System.out.println(x("12 * 3"));
System.out.println(x("12 [ 3"));
}
// Static for example purposes
public static Double x(String x){
String[] parsed;
if (x.contains("*")) {
// * Is a special character in regex
parsed = x.split("\\*");
return Double.parseDouble(parsed[0]) * Double.parseDouble(parsed[1]);
}
else if (x.contains("+")) {
// + is again a special character in regex
parsed = x.split("\\+");
return Double.parseDouble(parsed[0]) + Double.parseDouble(parsed[1]);
}
return null;
}
Your method x takes a String and returns a Double. Your task is to convert the string to a value by interpreting the string as an expression. Without supplying the answer here are some hints:
Split the string up (String.split)
Convert the arguments to values (Double.parseDouble)
Use a switch statement on the operator to determine what to return
As the default of the switch, return a null
That should give you enough to get you started
Identifier error either has ** or is in bold. I also do not know if the rest of the program will work.
import javax.swing.JOptionPane;
public class Multi
{
public static void main (String args[]);
**public static String dataIn (Stringinfo)**
{
String words = "Your hypotenuse is?";
String word1 = "Your second side is?";
String word2 = "Your thrid side is?";
int a = Integer.parseInt (words);
int b = Integer.parseInt (word1);
int c = Integer.parseInt (word2);
if ((a*a+b*b)== (c*c))
{
System.out.println ("Right triangle") ;
}
}
public static String dataIn (String info)
{
String answer = JOptionPane.showInputDialog(info); return answer;
}
}
You are declaring a method main like you would in an interface without a body. However, you can't do this outside an interface so main must have a body
public static void main (String args[]) { }
or be removed.
In addition to what #clcto said about your main method not having a body, there is another problem. You need to specify a data type while adding parameters just like when you create variables.
public static String dataIn (String Stringinfo)
Here String is the data type, just like in your other variables. Change String to be whatever fits your needs best.
I'm having an issue with a java program trying to get a string input from a joptionpane menu with a prompt box. With returning a string input.
I don't know if im going about it all wrong by trying to use
String.parseString(input)
Im very much a beginner with this so any help would have to be as simple as possible or a correction outright.
private static String getStringInput (String prompt) {
String input = EZJ.getUserInput(prompt);
return String.parseString(input);
}
UseCalls.java:27: error: cannot find symbol
return String.parseString(input);
^
symbol: method parseString(String)
location: class String
1 error
Here is a sample of the menu Im trying to use it with
do {
userInput = mainMenu();
if (userInput.equals("1")) {
String name = getStringInput("Name?");
String address = getStringInput("Address?");
call[numCalls++] = new Call();
}
} while (!userInput.equals("0"));
}
Here is the EZJ mini method
public class EZJ {
public static String getUserInput (String prompt) {
return JOptionPane.showInputDialog(prompt);
}
public static void dialog(String inputValue) {
JOptionPane.showMessageDialog ( null, inputValue );
}
}
You don't need to parse the string, it's defined as a string already.
Just do:
private static String getStringInput (String prompt) {
String input = EZJ.getUserInput(prompt);
return input;
}
As you see in an error UseCalls.java:27: error: cannot find symbol
return String.parseString(input); there is no method parseString in String class. There is no need to parse it as long as JOptionPane.showInputDialog(prompt); already returns a string.
If you're really bent upon converting Integer to String value, I suggest use String.valueOf(YourIntegerVariable). More details can be found at: http://www.tutorialspoint.com/java/java_string_valueof.htm
I was told in my class that I have to write and test my code in the main method, I wrote it, but I dont know how to test it. How I am supposed to test my methods? I am supposed to take user input, and then get the get the first letter, last letter, etc.
import java.util.Scanner;
public class Word
{
public static void main(String[] args)
{
}
public String word;
public void Word()
{
String word = "";
}
public void Word(String word1)
{
String word = word1;
}
public String getWord()
{
return word;
}
public void setWord(String newWord)
{
String word = newWord;
}
public void getFirstLetter()
{
String firstLetter = word.substring(0, 1);
}
public void getLastLetter()
{
String lastLetter = word.substring(word.length() - 1, word.length());
}
public void removeFirstLetter()
{
String noFirstLetter = word.substring(1, word.length());
}
public void removeLastLetter()
{
String noLastLetter = word.substring(0, word.length() - 1);
}
public int findLetter (String parameter)
{
word.indexOf(parameter);
return 1;
}
}
You test your methods by calling them with some defined input and compare the results with your expected output.
Example:
Suppose you have a method like this:
public static int add(int a, int b) {
return a + b;
}
You'd test it like this:
int result = add( 3, 5);
if( result != 8 ) {
//method is wrong
}
So basically you define a "contract" of what input the method gets and what the result should be (in terms of return value or other changed state). Then you check whether you get that result for your input and if so you can assume the method works correctly.
In order to be quite sure (you often can't be perfectly sure) you'd test the method several times with different types of input (as many as reasonable, to test different cases, e.g. short words, long words).
You often also test how your method handles wrong input, e.g. by passing null or empty strings.
You should have a look at tools like junit.
You can create a simple Test class and test your class and its behavior.
imports ...;
public class MyTest{
#Test
public void testMyClass(){
Word w= new Word();
w.setWord("test");
Assert.assertEquals(w.getFirstLetter(), "t");
}
}
With tools like Eclipse you could nicely run such a test.
Just a hint: you're very close you need an instance of Word, than you can call your methods
public static void main(String[] args) {
Word test = new Word();
test.setWord("something");
// here you might read javadoc of the String class on how to compare strings
}
EDIT:
I overlooked this:
public void setWord(String newWord)
{
String word = newWord;
}
The code you've written creates a variable word and newWord is assigned to it and then disappears.
If you (obviously) want to set a member of a class you should use this wich references the instance (you created in main()).
public void setWord(String newWord) {
this.word = newWord;
}
Since I would say this is homework, I will try not to explicitly give the answer. In the main method, you should set your word, then call each method and print the output to verify it is correct.
Agree with Jason. If you wanna test something, simply System.out.println() it. In your methods though, your return type is not a String but a void, so you could change that, and print it out on the main program run.
If not, just put the System.out.println() in your void methods. Shouldn't have much of a problem!