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();
}
}
}
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:
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.
Assuming that the input is from a file, what you want to do is this:
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
}
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With