Move all elements of an array to the right
So ive got an array of size 22, that reads in a number from the console and adds the number to the array. so if 123456 is read in the array will print out
12345600000000... something along those lines.
I need to right justify this number to perform arithmetic operations, but i cant seem to get the loop structure right to output the number!
开发者_如何学Cint[] newlong = new int[22];
String inputline = "";
public void readNewLong()
{
Scanner in = new Scanner(System.in);
inputline = in.nextLine();
try
{
for (int i = 0; i < inputline.length(); i ++)
{
char a = inputline.charAt(i);
String b = "" + a;
newlong[i] = Integer.parseInt(b);
}
}
catch (NumberFormatException nfe)
{
System.out.println("NumberFormatException, you must enter a string of digits. " + nfe.getMessage());
}
}
This is what reads in the number, inputline and store it in the array newLong.
The method below is what is not working...
public void rightJustify()
{
printLong();
System.out.println(inputline.length());
for(int i = 0; i< inputline.length(); i++)
{
for(int j = 0; j < newlong.length; j++)
{
newlong[j] = (newlong[j] - (inputline.length() -i));
}
}
printLong();
}
Why you want justify to right actually you can enter in correct order?
If newlong[] is global then has 0 in all positions
for (int i = inputline.length()-1; i>=0; i--)
{
char a = inputline.charAt(i);
String b = "" + a;
newlong[22 - inputline.length() + i] = Integer.parseInt(b);
}
edited: If you prefer your justify method I recommend you:
public void rightJustify() {
System.out.println(inputline.length());
for (int i = inputline.length()-1; i>=0; i--) {
newlong[22 - inputline.length() + i] = newlong[i];
newlong[i] = 0;
}
}
Why you don't align to the right when you read your data ?
Scanner in = new Scanner(System.in);
String inputline = in.nextLine();
StringBuilder inputData = new StringBuilder();
try {
for (int i = 0; i < inputline.length(); i++) {
char a = inputline.charAt(i);
inputData.append(a);
}
for (int i = 0; i < inputData.length(); i++) {
newlong[newlong.length - inputData.length() + i] = Integer
.parseInt("" + inputData.charAt(i));
}
printLong(newlong);
} catch (NumberFormatException nfe) {
System.out
.println("NumberFormatException, you must enter a string of digits. "
+ nfe.getMessage());
}
This an output
123456
0000000000000000123456
This is a strange way to parse an integer. Be that as it may, you don't need a nested loop to right justify the array:
// len is the number of digits of input.
public void rightJustify(int[] digits, int len)
{
int off = digits.length() - len;
for (int i = len; i-- > 0; ) digits[i + off] = digits[j];
for (int i = off; i-- > 0; ) digits[i] = 0;
}
Ok it's obviously homework which is why we're using for loops, but in practice I'd just use:
// len is the number of digits of input.
public void rightJustify(int[] digits, int len) {
System.arraycopy(digits, 0, digits, len, digits.length() - len);
System.fill(digits, 0, len, 0);
}
No reason to reinvent the wheel, even if the function is trivial enough for it imho.
精彩评论