Okay, so im trying to get my java code to print this out... Ages: 5,7,18,30,5,78
ID: 3805992 • Letter: O
Question
Okay, so im trying to get my java code to print this out...
Ages: 5,7,18,30,5,78,8,22,6,90
//Seperate
Ages: 5,5,6,7,8,18,22,30, 78, 90
//Seperate
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
//Seperate
1 7 13 19 20
5 9 13 17 21
------But im getting this-------
I have a txt document as well, so the code works, it just prints out this mess...
How can i fix it?
Here's my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class myArrayLab {
public static void printArray(int arr[]){
for(int i=0;i<arr.length;++i)
System.out.print(arr[i] + " ");
}
public static void printArray2(int arr[][]){
for(int i=0;i<arr.length;++i){
for(int j=0;j<arr.length;++j)
System.out.print(arr[i][j] + " ");
}
}
public static void printArray3(int arr[][]){
for(int i=0;i<arr.length;++i){
for(int j=0;j<arr.length;++j){
if(i==j)
System.out.print(arr[i][j] + " ");
}
}
}
public static void Sort1(int arr[]){
Arrays.sort(arr);
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("input.txt"));
int arr[] = new int[10];
int i= 0;
while(sc.hasNextLine()){
arr[i++] = Integer.parseInt(sc.nextLine());
}
System.out.print("Ages:");
printArray(arr);
System.out.println("Ages:");
Sort1(arr);
printArray(arr);
System.out.println();
int array2d[][] = new int[5][5];
printArray2(array2d);
System.out.println();
printArray3(array2d);
}
}
Ages 5 7 18 30 5 78 8 22 6 90 Ages: 5 56 7 8 18 22 30 78 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C:UsersFabia DesktopExplanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class myArrayLab {
public static void printArray(int arr[]){
for(int i=0;i<arr.length;++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void printArray2(int arr[][]){
for(int i=0;i<arr.length;++i){
for(int j=0;j<arr.length;++j)
System.out.print(arr[i][j] + " ");
System.out.println();
}
System.out.println();
}
public static void printArray3(int arr[][]){
for(int i=0;i<arr.length;++i){
System.out.print(arr[i][i] + " ");
}
System.out.println();
for(int i=0, j=arr.length-1;i<arr.length && j>=0;++i, j--){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
public static void Sort1(int arr[]){
Arrays.sort(arr);
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("input.txt"));
int arr[] = new int[10];
int i= 0;
while(sc.hasNextLine() && i < 10){
arr[i++] = Integer.parseInt(sc.nextLine().trim());
}
System.out.print("Ages:");
printArray(arr);
System.out.print("Ages:");
Sort1(arr);
printArray(arr);
System.out.println();
int array2d[][] = new int[5][5];
int num = 1;
for(int k=0; k<5; k++)
for(int j=0; j<5; j++){
array2d[k][j] = num;
num++;
}
printArray2(array2d);
System.out.println();
printArray3(array2d);
}
}
/*
Sample run:
Ages:5 7 18 30 5 78 8 22 6 90
Ages:5 5 6 7 8 18 22 30 78 90
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
1 7 13 19 25
5 9 13 17 21
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.