0

Here's a small bit of code I've been working on. I'm on the beginner's side but I've hit a small roadblock that I can't seem to work around. Here is my code so far:


import java.util.Scanner;
public class Question5
{
    public static void main(String args[])
    {
        int m, n, i, j;
        Scanner sc=new Scanner(System.in);
        m = 100;
        n = 6;
        int array[][] = new int[m][n];

        System.out.println("Enter the elements of the array, beginning with the first two lines of a song's lyrics, musical key for the song, beats per minute, the artist and the year realeased: ");

        for (i = 0; i < m; i++)
            for (j = 0; j < n; j++)
                array[i][j] = sc.nextInt();

        System.out.println("Yours track inputs were: ");
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
                System.out.print(array[i][j] + " ");

            System.out.println();
        }
    }
}

When executing my code, it seems that it only takes inputs for integers, but as my title suggests I'm seeking for each input to be represented as a sentence followed by a comma, then the following info, but like the information that System.out.println("Enter the elements of the array, beginning with the first two lines of a song's lyrics, musical key for the song, beats per minute, the artist and the year released: "); suggests, considering each row would consist of these values.

Granted, as you can see, the array would be quite large, so I tested it in shorter terms, such as 10x2 or 5x5, where it functioned as I imagined, yet I would only need to be able to input the sentences. Thus, I'm practicing the principle of the code working on a larger scale on theory, as I believe it should be unless I am corrected here.

Thank you in advance. Quite the journey I'm on.

p.s: I understand this is probably a newbish question, although I can't seem to find the answer anywhere in regards to my code. Apologies in advance for that as well.

Edit: As requested, here would be an example with changing the value of m to 1 and n to 6

What I envision is that I would input, say: the information as listed in bold in a sequence:


Please enter lyrical line one, line two, Artist, Release Year, Beats, Key:

This is lyrical line one

This is lyrical line two

Artist

2021

10

Eb

Your track inputs were:

This is lyrical line one, This is lyrical line two, Artist, 2021, 10, Eb


And thus that would end the program. Thank you.

The main intention of my code is to have an array hold 100 tracks, store information

64102
  • 3
  • 2
  • Does this answer your question? [How to input a sentence in Java](https://stackoverflow.com/questions/38028039/how-to-input-a-sentence-in-java) – Joe Apr 17 '21 at 06:30

3 Answers3

0

Try using sc.nextLine(); instead. There are lots of different methods you can use with the Scanner class depending on what you need. Here is a list of them with examples.

Wellerman
  • 649
  • 2
  • 12
0

In your code, variable array is a two-dimensional array of int but part of your sample input is String. In java, a String is not an int. If you want array to contain elements that are either String or int then you need to change the type of array to Object.

Then you need to call the appropriate method of class Scanner. For example, for reading a line of text, there is method nextLine.

Here is my rewrite of class Question5. Notes after the code.

import java.util.Scanner;

public class Question5 {
    public static void main(String args[]) {
        int m, n, i, j;
        Scanner sc = new Scanner(System.in);
        m = 1;
        n = 6;
        Object array[][] = new Object[m][n];
        System.out.println("Enter the elements of the array, beginning with the first two lines " +
                           "of a song's lyrics, musical key for the song, beats per minute, the " +
                           "artist and the year realeased:");
        for (i = 0; i < m; i++) {
            array[i][0] = sc.nextLine();
            array[i][1] = sc.nextLine();
            array[i][2] = sc.nextLine();
            array[i][3] = sc.nextInt();
            array[i][4] = sc.nextInt();
            sc.nextLine();
            array[i][5] = sc.nextLine();
        }
        System.out.println("Yours track inputs were: ");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}

A two-dimensional array can be thought of as a table containing rows and columns, so according to the above code, array contains one row and six columns where each column contains a predefined value. The first column contains the first line of a song. The second column contains the second line of a song and so on – according to your requirements as stated in your question. Hence no need for an inner for loop when accepting data from the user.

Note that after a call to method nextInt, if you call method nextLine you won't get the correct input due to the way class Scanner works. Refer to this SO question for more details.

Scanner is skipping nextLine() after using next() or nextFoo()?

Also note that the above code is using autoboxing to convert an int to an Integer.

Here is a sample run of the above code:

Enter the elements of the array, beginning with the first two lines of a song's lyrics, musical key for the song, beats per minute, the artist and the year realeased:
This is lyrical line one
This is lyrical line two
Artist
2021
10
Eb
Yours track inputs were: 
This is lyrical line one This is lyrical line two Artist 2021 10 Eb
Abra
  • 11,631
  • 5
  • 25
  • 33
  • This is fantastic, I thank you for that, my issue now is how to duplicate this 99 more times to take different elements and repeat them back in the same manner. – 64102 Apr 17 '21 at 06:39
  • @64102 start by changing the value of `m` to 2 and enter two "rows" of data. I have shown you how to enter one "row" of data. – Abra Apr 17 '21 at 06:45
  • Exactly what I needed! Thank you so very much! – 64102 Apr 17 '21 at 07:02
0

You are trying to take String input in integer array and that will not work. As you have multiple String inputs and Integer inputs, one thing you can do is use object array.

class Question5{

String lineone, linetwo, Artist,Key; 
int ReleaseYear, Beats;
public Question5(String lineone, String linetwo, String Artist,String Key,int 
ReleaseYear,int Beats){
    this.lineone=lineone;
    this.linetwo=linetwo;
    this.Artist=Artist;
    this.Key=Key; 
    this.ReleaseYear=ReleaseYear; 
    this.Beats=Beats;
}

After creating constructor you can take inputs by using for loop, but to avoid confusion take one input at a time.

public static void main (String[] args) 
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter size");
    int size = sc.nextInt();
    Question5[] c = new Question5[size];
    for(int i=0;i<size;i++){
    /*Write your code here to take inputs from user and save in variables*/
    
    c[i]= new Question5(/*Pass the variables here according to order mentioned in constructor*/);
}

For example it will look like this but I have mentioned input directly here. For user input which will be different for each loop, you need to first save them in variables and pass the variables while creating object.

c[0] = new Question5("One","Two","Artist","Key",2017,15);