Jagged array in java - java

My final output should be like:
How many rows are in the jagged array? 4
Enter a row, separated by spaces: 9 2 14 5 8
Enter a row, separated by spaces: 3
Enter a row, separated by spaces: 15 23
Enter a row, separated by spaces: 9 8 7 6 5 4 3
After the funky operation, the resulting array is:
9 4 42 20 40
6
45 92
36 40 42 42 40 36 30
But I keep getting errors:
Exception in thread "main" java.util.NoSuchElementException: No line found
at ScannerHacked.nextLine(ScannerHacked.java:1525)
at jagged.main(jagged.java:14)
Here is my code:
import java.util.Scanner;
public class jagged {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many rows are in the jagged array? ");
int row = sc.nextInt();
int[][] jaggedArray = new int[row][];
for(int i = 0; i < row; i++)
{ Scanner rows = new Scanner(System.in);
System.out.print("Enter a row, separated by spaces: ");
String arraystring = rows.nextLine();
String []a = arraystring.split(" ");
jaggedArray[i] = new int[a.length];
for(int j = 0; j < jaggedArray[i].length; j++)
{
int y = Integer.parseInt(a[j]);
jaggedArray[i][j] = y;
}
}
System.out.println("After the funky operation, the resulting array is:");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < jaggedArray[i].length; j++)
{
if(jaggedArray[i][j] > 9)
System.out.print(" "+(jaggedArray[i][j]*(i+j+1)) + "");
else
System.out.print(" "+(jaggedArray[i][j]*(i+j+1)) + "");
}
System.out.print("\n");
}
}
}

As i can see, you are using two Scanner with the same source (System.in) in main() method...
Scanner sc = new Scanner(System.in);
Scanner rows = new Scanner(System.in);
it might throw an Exception, So you should close first Scanner i.e. sc.close(); when going to use another Scanner with same source.

Related

Trying to add using a multiple in a for statement

I'm trying to change the x into a pyramid of multiples. Ex. if the multiple was 2 it would add by 2s going down. I kept getting multiples that would mess up and swap around when I tried and was wondering if anyone would be kind enough to help!
This is what I have so far:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int rows=0;
int multiple=0;
int x=0;
Scanner scan = new Scanner(System.in);
System.out.println("Input Rows to Generate: ");
rows = scan.nextInt();
System.out.println("Input Multiple to Count by: ");
multiple = scan.nextInt();
System.out.println();
for (int i = 1; i<=rows; i++)
{
for (int j = 1; j<=i; j++)
{
System.out.print("x");
}
System.out.println();
}
}
}
ex.
Enter the number of rows:
5
Enter the multiple to count by:
1
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int rows=0;
int multiple=0;
int x=0;
Scanner scan = new Scanner(System.in);
System.out.println("Input Rows to Generate: ");
rows = scan.nextInt();
System.out.println("Input Multiple to Count by: ");
multiple = scan.nextInt();
System.out.println();
int n = 0;
for (int i = 1; i <= rows; i++){
for (int j = 1; j <= i; j++){
System.out.print(multiple * n + " ");
n++;
}
System.out.println();
}
}
}
For rows = 5 and multiple = 1 the output will be:
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
Also, for rows = 5 and multiple = 2 the output will be:
0
2 4
6 8 10
12 14 16 18
20 22 24 26 28

I am only able to print up to "red: 1" only, after which my program ends due to usage of break statement

Please see this code:
import java.util.Scanner;
public class Array
{
public static void main(String args[])
{
String[] names=new String[5];
Scanner scan=new Scanner(System.in);
System.out.println("Enter 5 colour:");
for(int i=0; i<names.length;i++)
{
names[i]=scan.nextLine();
}
String[] numbers=new String[5];
Scanner scan2=new Scanner(System.in);
System.out.println("Enter 5 numbers:");
for(int j=0; j<numbers.length;j++)
{
numbers[j]=scan.nextLine();
}
OUTER:
for (int k = 0; k < names.length; k++)
{
System.out.println(names[ k ] + ":");
break OUTER;
}
INNER:
for (int l = 0; l < numbers.length; l++)
{
System.out.println(numbers[ l ]);
break INNER;
}
}
}
I am a newbie, learning Array as of now, in Java. I want to print the outcome of the code above as follows:
Enter 5 numbers:
//Say:
RED
GREEN
BLUE
PINK
YELLOW
Enter 5 numbers:
1
2
3
4
5
//Output of the code should be:
RED: 1
GREEN: 2
BLUE: 3
PINK: 4
YELLOW: 5
How can I print the array? I am only able to print up to "RED: 1" only, after which my program ends due to break statement.
Your last for does not a have any purpose. Just try :
import java.util.Scanner;
public class Array {
public static void main(String args[]) {
String[] names=new String[5];
Scanner scan=new Scanner(System.in);
System.out.println("Enter 5 colour:");
for(int i=0; i<names.length;i++){
names[i]=scan.nextLine();
}
String[] numbers=new String[5];
System.out.println("Enter 5 numbers:");
for(int j=0; j<numbers.length;j++) {
numbers[j]=scan.nextLine();
}
for (int k = 0; k < names.length; k++){
System.out.println(names[ k ] + ":"+ numbers[k]);
}
}
}
ANd it's stop at the first because you break your for
scan.nextLine() reads the whole line of input that is it reads until you don't enter \n new line charachter, so use nextInt() instead of that. Also each System.out.println() is used to print a whole line so only one loop can be used. If names and numbers have same length. And change numbers[] from String to int, that would be more type safe.
int numbers[] = new int[5];
for(int j=0; j<numbers.length;j++)
{
numbers[j]=scan.nextInt();
}
for (int k = 0; k < names.length; k++)
{
System.out.println(names[ k ] + ":" + numbers[k]);
}
Since the number of elements be it color or numericals that you're trying to get from the user is fixed here i.e 5, why complicate things? Use a single for loop to print all the contents of both the arrays:
for(int k=0;k<names.length;k++){
System.out.println(names[k] + ":"+numbers[k]);
}

How can I fix my code? 2D Array

I want to create a 2D array using the user's input and the creating random numbers in the second line.
E.g:
Output should be:
If the user enters "7" then:
1 2 3 4 5 6 7 (User's input)
0 2 4 8 9 8 5 (Random numbers)
but instead I only get one random number.
My code is working but I can't see to create the array correctly.
My code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of exits: ");
int n = input.nextInt();
int [][] A = new int[2][n];
for(int i=0; i<A.length; i++){
for (int j = 0; j<n; j++) {
A[i][j] = (int) (Math.random()*10);
}
}
System.out.println(A[1][n-1]);
System.out.print("Distance between exit i and exit j is: " + distance());
}
public static int distance(){
Scanner input = new Scanner(System.in);
System.out.print("Please enter exit i: ");
int i = input.nextInt();
System.out.print("Please enter exit j: ");
int j = input.nextInt();
return i + j;
}
}
How can I fix it?
Would this help?
int n = input.nextInt();
Random rand = new Random();
int [][] A = new int[2][n];
for (int i = 0; i<n; i++) {
A[0][i] = i+1;
A[1][i] = rand.nextInt(10);
}
Am not too certain about what you mean by:
1 2 3 4 5 6 7 (User's input)
0 2 4 8 9 8 5 (Random numbers)
Do you want users to manually enter "1 2 3 4 5 6 7"?
Eitherway, here's something to help with the printing aspect:
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter number of exits: ");
int n = input.nextInt();
int [][] A = new int[2][n];
for(int i=0; i<A.length; i++){
for (int j = 0; j<n; j++) {
A[i][j] = (int) (Math.random()*10);
}
}
for(int[] b: A)
{
for(int k: b)
{
System.out.print(k + " ");
}
System.out.println();
}
System.out.print("Distance between exit i and exit j is: " + distance());
}

How to create a empty set of array and fill it with user input and print it out?

Here is my code to fill an empty array :
package duplicate.terminator;
import java.util.Arrays;
import java.util.Scanner;
public class DuplicateTerminator {
public static void main(String args []){
Scanner number = new Scanner(System.in);
int a, num;
int[] integerset = null;
System.out.println("Enter Number: ");
num = number.nextInt();
Arrays.fill(integerset, num);
}
}
That is my code because I want to have this output.
I need to stack input numbers in array and print it out like this.
Sample Input/Output:
Enter number: 5
5
Enter number: 9
5 9
Enter number: 2
5 9 2
Enter number: 9
9 has already been entered
5 9 2
Enter number: 1
5 9 2 1
I would suggest using LinkedHashSet
because
you don't know the number of elements in your data container initially
you don't want duplicates and you don't want to go serially to check if there is a duplicate
you want to preserve order
Here is an example:
Set<Integer> numbers = new LinkedHashSet<>();
while(/*some logic to exit on special input*/) {
if(!numbers.add(userInputNum){
// number was already present
}
}
If you don't want to use LinkedHashSet or fill method in array this is an alternate way to do it.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int i, j;
int[] list = new int[10];
for (i = 0; i <= 9; i++)
{
System.out.println("Enter a number :");
list[i] = scan.nextInt();
for (j = 0; j < i; j++)
{
if (list[j] == list[i])
{
System.out.println(list[j] + " has already been entered");
i--;
}
}
for(j = 0; j <= i; j++)
{
System.out.print(list[j] + " ");
}
System.out.println();
}
}

Scanner input issue

How do I take in input from a user from a scanner, then put that input into a 2D Array. This is what I have but I dont think it is right:
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int [][] a = new int[row][col];
Scanner in = new Scanner(System.in);
System.out.println("Enter a sequence of integers: ");
while (in.hasNextInt())
{
int a[][] = in.nextInt();
a [row][col] = temp;
temp = scan.nextInt();
}
Square.check(temp);
}
What I am trying to do is create a 2D array and create a magic Square. I have the boolean part figured out, I just need help with inputting users sequence of numbers into the array so the boolean methods can test the numbers. All help greatly appreciated
I don't believe your code will work how you want it to. If I'm understanding your question correctly, here's what I would do:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int [][] a = new int[row][col];
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.print("Enter integer for row " + i + " col " + j + ": ");
a[i][j] = in.nextInt();
}
}
// Create your square here with the array
}
In the loops, i is the current row number and j is the current column number. It will ask the user for every row/column combination.
You can use that in order to enter all number at the same time :
int [][] a = new int[3][3];
Scanner in = new Scanner(System.in);
System.out.println("Enter a sequence of integers: ");
int row=0,col=0;
while (in.hasNextInt())
{
a [row][col++] = in.nextInt();
if(col>=3){
col=0;
row++;
}
if(row>=3)break;
}
Then you can enter :
1 2 3 4 5 6 7 8 9
to fill your array.

Categories