Goldy86 February 2016

Calling substring Method?

I am fairly new to Java. Over the past few weeks I have been trying to teach myself java. This has been primarily based on tutorials i find online and forums I can find. So keep this in mind and any additional critique you can share is greatly appreciated! I am currently trying to create a calculator that runs off of if-else loops. I'm working on a method that allows the user to derive a function based on the principle that if

f(x)=ax^n+bx^o+cx^p... then f'(x)=anx^n-1+box^o-1+cpx^p-1...

I'm trying to use .split() to separate the parts of the function, perform the changes to the individual parts, and then print them together. I could get most of the way through this but I couldn't convert a string with a negative sign to an integer so I am trying to call a method that uses .substring and then replaceAll to get rid of the negative sign then convert to integer. However, I keep getting a compiling error stating the "actual and formal argument lists differ in length". Can anyone explain why this might be happening?

import java.util.Scanner;
import java.util.Arrays;
import java.lang.String;
public class InputInteger
{
public String changeSign(String second) {
    String negative = second.substring(0,1);
    return negative;
}
public static void splitFunction() {
  Scanner o = new Scanner(System.in);
  String function = o.next();
  String[] parts = function.split("(?=\\+|\\-)");
  for (int i = 0; i < parts.length;) { 
        String[] second = parts[i].split("(?=[0-9]+|[a-z]+|[A-Z]+\\^)");
        InputInteger.changeSign();
        if (negative = ("-")) {
            second = second.replace("-","");
      int x = Integer.parseInt(second[0]);
      int y = Integer.parseInt(second[2]);
      int w = x*y;
      int z = y-1;
      System.out.println(w + "x^" + z);
      i++;
    }
  }
}

Answers


radoh February 2016

  • changeSign(String second) should be defined as static
  • negative variable is not defined
  • you should compare strings with equals() method
  • you call .replace(...) on an array, which doesn't have this method

And these are only compile errors, I see at least one runtime problem:

  • you increase i only in the if which may result in an infinite loop...

I suggest you use some good IDE like Eclipse or IntelliJ IDEA, which will help you with warnings/errors/etc.


SarthAk February 2016

Problem that you are talking about is the method not working . You have to pass argument in the function like

InputInteger.changeSign(function);

or

InputInteger.changeSign(second[i]);

according to requirement


Michael Gantman February 2016

First of all in your code if (negative = ("-")) you have a single "=" and I think you meant to use "==" for comparison. Second, method parseInt() as well as valueOf() (which I prefer to parseInt()) should handle negative numbers just fine. there is no need to remove "-". Yout method changeSign() takes a String argument, also your method ChangeSign() returns String value and you must assign a result to some String: String negative = InputInteger.changeSign(str);. Plus also String class has a method startsWith(String prefix) that fits your better then substring(). Hope this helps. If anything there is an Open source library that provides a Utility for parsing String into Integer (among other things). in That util there is a method

parseStringToInt(String numStr, int defaultValue, String nullOrEmptyStringErrorMessage, String numberFormatErrorMessage)

That tries to parse a String to Integer and if it does not succeed it returns a default value and never throws an Exception. That method definitely works fine with negative integers. Here is the link to the article about the library.https://www.linkedin.com/pulse/open-source-java-library-some-useful-utilities-michael-gantman?trk=pulse_spock-articles. There you will find the instructions on where to get the library including javadoc and source code.

Post Status

Asked in February 2016
Viewed 2,678 times
Voted 12
Answered 3 times

Search




Leave an answer