0

I am EXTREMELY new to programming. I am trying to write a program that allows users to enter in their college / university transcripts course by course. I would like each course to be separated in their own array and separated by spaces.

For example: ENG 105 A 3 (array 1) MAT 102 A 4 (array 2) etc...

It seems as if the input is being stored into one single array.

It would be awesome if I did not have to use a counter and the program could move on when users are done entering their courses.

import java.util.Scanner;

public class Tester{

public static void main(String[] args) {

 int length;
 Scanner input = new Scanner(System.in);

 System.out.println("How many courses did you complete at your college / university?: ");
 length = input.nextInt();  

 String[] courses = new String[length];

 System.out.println("Follow this model when entering your courses: ENG 105 3 A");

 for(int counter = 0; counter < length; counter++){
  System.out.println("Course "+(counter+1));
  courses[counter] = input.next();   
 }

 input.close();

}

}
3
  • 1
    It seems like there are two issues here. 1. you would like three arrays. 2. you would like a better way to end user input. I'm not sure what the benefit of #1 is. Do you want String[] courses to be String[][] courses? To end user input you could have the user enter done and check for that string to exit the loop. Commented Apr 10, 2019 at 19:10
  • I was thinking I could create a student object and then have an arraylist inside of that object that holds the courses... And maybe when I implement a GUI I could have a “complete” button? Commented Apr 10, 2019 at 19:34
  • Other suggestion would be to use a list of object: List<Course> ... where Course is a class with 4 attributes (e.g.. ENG. 105, A and 3). Commented Apr 10, 2019 at 21:40

2 Answers 2

1

There is 2 points to fix: Handle data (ENG-105-3-A) and buffer.

String[][] courses = new String[length][4];

System.out.println("Follow this model when entering your courses: ENG-105-3-A");

for(int counter = 0; counter < length; counter++){
    System.out.println("Course "+(counter+1));

    //Solution
    courses[counter] = input.next().split("-");  //data are separated by "-"

    input.nextLine(); //Cleanning buffer

}
Sign up to request clarification or add additional context in comments.

2 Comments

This is working. I am trying to figure out how to print single courses. For example, the third course you entered is (print 3rd entered course here).
@AddiFamer, try this to print courses: System.out.println("\nEntered Stuffs.!"); int nbr=0; for(String[] cours:courses){ System.out.println("Course "+nbr++); for(String s:cours) System.out.print(s+" "); System.out.println(); } (so lat but try it, if you are not jet got it)
0

To achieve what you want something like this would work:

String[][] courses = new String[length][];

System.out.println("Follow this model when entering your courses: ENG 105 3 A");

for (int counter = 0; counter < length; counter++){
    System.out.println("Course "+(counter+1));
    courses[counter] = input.nextLine().split("\\s+");   
}

Since this is splitting the course, it generates an array of arrays, that looks like this:

[["ENG","105","A","3"], ["MAT", "102", "A", "4"]] 

On the other hand, if you want to stop the input when the user enters a keyword, you need something like this:

List<String[]> courses = new ArrayList<String[]>;

System.out.println("Follow this model when entering your courses: ENG 105 3 A");
String course = input.next();
while (!course.equals("end")){
    courses.add(course.split("\\s+"));  
    String course = input.nextLine(); 
}

2 Comments

After input.next(), buffer should be cleanned
To get this to work i needed to add the array size of 4 in the bracket next to length. I also had to add input.HasNextLine. Thank you! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.