Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Want to decipher a bit of code (pretty new to java)

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Want to decipher a bit of code (pretty new to java)

    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);
        }
      }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Want to decipher a bit of code (pretty new to java)

    Can you ask some specific questions about the source statements in section 3 that you are having problems with?

    What did the print statements print before they were commented out?

    If you have questions about what any of the expressions do, add a print statement that prints out the results of when that expression is executed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: December 18th, 2013, 02:09 PM
  2. Can someone send me codes of Java games? Pretty Please!? =)
    By daryll_1210 in forum Project Collaboration
    Replies: 2
    Last Post: August 10th, 2013, 11:21 PM
  3. Pretty basic java program..need some help!
    By LAerving in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 26th, 2012, 12:09 AM
  4. Pretty new to Java can someone help with an addObject() problem?
    By alan120184 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2009, 12:48 PM
  5. Ambiguity and non-static variable reference error in java
    By jenseits in forum AWT / Java Swing
    Replies: 5
    Last Post: December 8th, 2008, 07:04 PM