package assignment2;
import java.util.Scanner;
import java.util.Arrays;
public class assign3 {
public static void main(String[] args) {
int n;
int m;
Scanner scan = new Scanner(System.in);
System.out.println("Write 2 numbers below: ");
n = scan.nextInt();
m = scan.nextInt();
int numArr[] = new int[n*m];
for (int i=0; i<numArr.length; i++) {
numArr[i] = i+1;
}
String str = Arrays.toString(numArr);
System.out.println(str);
System.out.println(str.substring(3,6));
System.out.println(str.substring(0,m));
System.out.println(str.substring(5));
/*
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
System.out.println(i + j + "\t");
}
}
*/
}
}
Write these codes and print out the results
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28] 2, [1, , , 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]
These are the results. From what I've been looking for, str.In the case of substring(3,6), you have to cut from fourth to seventh, and str.I think you should cut from the first to the m+1 when you have the substring(0,m), so I'd appreciate it if you could tell me which part is the problem.
substring java string
String.If you look at jabadok in substring(int, int)
:
Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
That's what it says. They say they include the start index, but not the end index.
For some examples:
String str = "a234567890b234567890c234567890d234";
Assert.assertEquals("a", str.substring(0, 1));
Assert.assertEquals("a23456", str.substring(0, 6));
Assert.assertEquals("567", str.substring(4, 7));
That's what happens. Let's look at the results of indexes 0 to 6. Index 0 is a
and index 6 is 7
but not. In other words, substring(x, y)
means x and less than y.
For example, the question is:
int numArr[] = new int[5];
for (int i = 0; i < numArr.length; i++) {
numArr[i] = i + 1;
}
String str = Arrays.toString(numArr);
System.out.println(str); // [1, 2, 3, 4, 5]
If you give an index to the string [1, 2, 3, 4, 5]
which is the value of
str
, it looks like this:
If you understood it up to here, the next one would be easy, right?
System.out.println(str.substring(3, 6)); // 2,
System.out.println(str.substring(0, 4)); // [1,
© 2024 OneMinuteCode. All rights reserved.