Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modular calculator getting data inside a String array

Tags:

java

I'm currently stuck on a problem from CodeAbbey. I don't want an answer to the entire thing.

This is the meat of the question:

Input data will have:

  • initial integer number in the first line;
  • one or more lines describing operations, in form sign value where sign is either + or * and value is an integer;
  • last line in the same form, but with sign % instead and number by which the result should be divided to get the remainder.

Answer should give remainder of the result of all operations applied sequentially (starting with initial number) divided by the last number.

My problem is with the logic. So far I realize there is an operator, a space and then a number. To get the number I thought of using

char c = src.charAt(2);

and to get the operator I'd use the same thing. Then I'd just use a bunch of if-else statements to figure out the rest. But how do I do this in the large scale? How do I read all of those numbers in and do it for every one of them? A nudge on the right direction would help.


import java.util.Scanner;

public class Challenge14 {

    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("Enter Your first number: ");
        int x = in.nextInt();

        for (int i = 0; i < 7; i++) {
            String[] s = new String[i];

            s = in.nextLine();
        }
    }
}
like image 368
JustForLong Avatar asked Jan 05 '16 20:01

JustForLong


3 Answers

One of the more helpful classes for this problem is the Scanner.

The Scanner documentation is located at: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

The Scanner can be set to read from a String, File, or Input Stream.

The Scanner has several useful methods, including:

  • hasNext(), which lets you know if there are any other operators or numbers to pick up
  • next(), which will read the next token as a String
  • nextInt(), which will read the next token as an int

You can use the scanner to pick up the first number, then use some sort of loop to perform operations on that number until there are no more operations to perform.

like image 144
Zachary Myers Avatar answered Nov 03 '22 21:11

Zachary Myers


Assuming that the input is from a file, what you want to do is this:

  • Read the first line in as a number
  • Split the input on each other line on whitespace, so you're left with something that can be interpreted as an operator, and something that can be interpreted as a number
  • Perform the operations and accumulate your result

The file portion is likely what you're struggling with. The answer below addresses that. I leave the rest as an exercise for the reader.

public class Accumulator {
    private int result;

    public void readFile(String filename) {
        try(Scanner scan = new Scanner(new File(filename)) {
            // Unconditionally read the first line as a number
            result += Integer.parseInt(scan.nextLine());
            //Now, we need to read in each other line remaining.
            while(scan.hasNextLine()) {
                String[] contents = scan.nextLine().split();
                int number = Integer.parseInt(contents[1]);
                // The operator is in contents[0]
                performArithmeticWith(contents[0], number);
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
   }

   public void performArithmeticWith(String operator, int number) {
       switch(operator) {
           case "+":
           break;
           // etc
       }
   }

} 
like image 45
Makoto Avatar answered Nov 03 '22 22:11

Makoto


My problem is with the logic. So far I realize there is an operator, a space and then a number.

There are several options in Java to separate the operator and the operand.

You can use the String.split() method:

String[] token input.split(" ");
//token[0] will be your operator
//token[1] will be your number

You can also use the indexOf() method:

int idx = input.indexOf(" ");
String opr = input.substring(0, idx);    //your operator
String num = input.substring(idx+1);     //your number
like image 40
user3437460 Avatar answered Nov 03 '22 21:11

user3437460