Hi guys! I needed help deciphering a section of my code. I found it online and got a little help from my friends, but I don't fully understand what it means. My code basically takes in an input from the user in the form mx+b, and then asks for x. The program then solves for x. I need help deciphering section 3 of my code.
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //1 String xExpression; System.out.println("Write an expression in mx+b form (eg. 2x+10)"); xExpression = sc.nextLine(); //2 int m = Integer.valueOf(xExpression.substring(0,xExpression.indexOf('x'))); //System.out.println(m); //3 int index=0, num=0; char sign='+'; for (int i=xExpression.indexOf('x');i<xExpression.length();i++) { if (xExpression.charAt(i)=='+' || xExpression.charAt(i) == '-') { sign = xExpression.charAt(i); index=i; num = Integer.valueOf(xExpression.substring(index)); //System.out.println(num); } } System.out.println(num); //4 System.out.println("Enter x"); int x = sc.nextInt(); if (sign=='+') { System.out.println(m*x+num); } else if (sign=='-') { System.out.println(m*x-num); } } }