You can have a scanner that simply reads however many numbers you need (.nextInt() or .nextDouble() depending on the precision you want) in sequence, so...
However, I don't know how many degrees you want to catch, so I think you should go for a more robust solution.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//the class this program will be ran from
public class Test{
//a list of Pair objects
private List<Pair> funcPairs;
public Test() {
//list is an interface, so must store the value of a concrete object that implements it
this.funcPairs = new ArrayList<Pair>();
}
//function to read the polynomial using regex
public void readPolynomial(String input) {
Pattern p = Pattern.compile( "((-?\\d+(?=x))?(-?[xX])(\\^(-?\\d+))?)|((-?)[xX])|(-?\\d+)");
Matcher m = p.matcher(input);
int x = 0, y = 0;
//while matches exist for input string
while (m.find()) {
//if variable exists and coefficient exists
if(m.group(3) != null && m.group(2) != null) {
//store the exponent if exists else store 1
y = (m.group(5) != null ? Integer.parseInt(m.group(5)) : 1);
//store coefficient
x = Integer.parseInt(m.group(2));
} else {
//store coefficient with no exponent or variable
x = Integer.parseInt(m.group());
}
//add new pair to funcPairs list
this.funcPairs.add(new Pair(x, y));
//reset values to 0 for next iteration through while loop
x = 0;
y = 0;
}
}
//getter for list
public List<Pair> getCoefPairs() {
return this.funcPairs;
}
//private inner class (only accessible within Test)
private class Pair {
private int coef, degree;
public Pair(int coef, int degree) {
this.coef = coef;
this.degree = degree;
}
public int getCoef() {
return coef;
}
public int getDegree() {
return degree;
}
public String toString() {
return String.format("%dx^%d", coef, degree);
}
}
//main method that runs program
public static void main(String[]args) {
Test test = new Test();
//scanner object
Scanner scan = new Scanner(System.in);
// example 4x^4+3x^2+7x^0-20x^1
//read input from user
String function = scan.nextLine();
//populate list
test.readPolynomial(function);
//initialize return string
String ret = "";
//assemble output by looping through each pair in the list
for(Pair p: test.getCoefPairs()) {
ret += p.toString() + "+";
}
//remove last "+" because lazy about assembling output
System.out.println(ret.substring(0, ret.length() - 1));
}
}
Assuming variable is always x and coefficients and exponents are always integers. If you want more precision, I provided links to resources you can use to add the functionality!
This is a process using a regular expression to catch anything that looks like a polynomial and break it into a group. Those groups are defined at the link below. This expression stores coefficient in matching group 2, the variable (assumed to be x in this case) in group 3, and the exponent in group 5. I build in some checks for if any of those things existed to deduce if it had no exponent/ variable provided and establish default values in those cases. I store each of the matches in a List of Pairs and iterate through them with a foreach statement. This sets the stage for you to do anything you want with those coefficients and exponents, and if the original expression matters you can store the scan.nextLine() as a String first, or you can iterate through the List of Pairs and print them each out in turn with a "+" between each member to get the original function back.
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285