Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with java code

    I'm working on a Java code that needs to take two inputted 7 digit numbers with commas, read them as a string variable, convert them into integers. And then output the sum. Then output the sum a second time with commas included. I don't see what I'm doing wrong but it keeps failing.


    import java.util.*;

    public class Assignment2
    {
    public static void main (String[] args)
    {
    Scanner scan = new Scanner(System.in);

    System.out.println("Assignment 2: Add integers with commas");

    System.out.print("Enter a 7 digit integer with commas:");
    String var1 = scan.nextLine();
    System.out.print("Enter a 7 digit integer with commas:");
    String valb = scan.nextLine();

    String digit1, digit2, digit3, digit4, digit5, digit6;


    digit1 = var1.substring(0);
    int valaa =Integer.parseInt("digit1");
    digit2 = var1.substring(2,5);
    int valab =Integer.parseInt("digit2");
    digit3 = var1.substring(7,10);
    int valac =Integer.parseInt("digit3");

    int variablea = (valaa*10^6 + valab*10^3 + valac);

    digit4 = valb.substring(0);
    int valba =Integer.parseInt("digit4");
    digit5 = valb.substring(2,5);
    int valbb =Integer.parseInt("digit5");
    digit6 = valb.substring(7,10);
    int valbc =Integer.parseInt("digit6");

    int variableb = (valba*10^6 + valbb*10^3 + valbc);

    int ans = ((int)variablea + variableb);

    int num1 = (valaa + valba);
    int num2 = (valab + valbb);
    int num3 = (valac + valbc);

    System.out.print(variablea);
    System.out.print("+");
    System.out.print(variableb);
    System.out.print("=");
    System.out.print(ans);

    System.out.print(valaa);
    System.out.print(",");
    System.out.print(valab);
    System.out.print(",");
    System.out.print(valac);
    System.out.print("+");
    System.out.print(valba);
    System.out.print(",");
    System.out.print(valbb);
    System.out.print(",");
    System.out.print(valbc);
    System.out.print("=");
    System.out.print(num1);
    System.out.print(",");
    System.out.print(num2);
    System.out.print(",");
    System.out.print(num3);



    System.out.println("Assignment 2 completed");
    }
    }

  • #2
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I've written this code for you:


    Code:
    import java.util.*;
    
    public class Main {
    
        public static int digit(boolean comma, char digit) {
            if (comma) {
                if (digit == ',') {
                    return 10; // Code for comma
                }
            } else {
                switch (digit) {
                    case '0':
                        return 0;
                    case '1':
                        return 1;
                    case '2':
                        return 2;
                    case '3':
                        return 3;
                    case '4':
                        return 4;
                    case '5':
                        return 5;
                    case '6':
                        return 6;
                    case '7':
                        return 7;
                    case '8':
                        return 8;
                    case '9':
                        return 9;
                }
            }
            return -1;
        }
    
        public static int convertToInt(String string) {
            boolean comma = false;
            int i = 0;
            int output = 0;
            while (i < string.length()) {
                int temp = digit(comma, string.charAt(i));
                if (temp == -1) {
                    return -1;
                }
                if (!(comma)) {
                    output = output * 10 + temp;
                }
                comma = !(comma);
                i++;
            }
            return output;
        }
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
    
            System.out.println("Assignment 2: Add integers with commas");
    
            System.out.print("Enter a 7 digit integer with commas:");
            String var1 = scan.nextLine();
            System.out.print("Enter a 7 digit integer with commas:");
            String var2 = scan.nextLine();
    
            int result1 = convertToInt(var1);
            int result2 = convertToInt(var2);
    
            if ((result1 < 0) || (result2 < 0)) {
                System.out.println("Invalid input");
                System.exit(0);
            }
    
            System.out.println((result1 + result2) + "");
    
            System.out.println("Assignment 2 completed");
        }
    }
    This code was also tested. I didn't test whether this is a seven digit number, but that can be done easily, based on the value of the variable called i.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •