Home > Blockchain >  Align user input (array and ints) in Java
Align user input (array and ints) in Java

Time:10-04

I am a beginner in Java. I am trying to make this as for my schoolwork to input their courses and units. So far, this is my progress. I was told to use System.out.format for aligning the inputs but I seem to get an error every time time I try new ones. So far, this is the best output I've done. Requesting for help/improvement!

package com.mycompany.studentinfo;

import java.util.Arrays;
import java.util.Scanner;

public class StudentInfo {   
    static int unit;
    static String[] subj = new String[8];

    public static void main(String[] args){
        academicinfo();
        academicunit();
        finaloutput();

    static void academicinfo() {
        Scanner sc = new Scanner(System.in);
        System.out.print("Program: ");
        program = sc.nextLine();
        System.out.println("Courses: ");
        int i = 0;
        while (i < subj.length) {
            subj[i] = sc.nextLine();
            i  ;
        }
    }

    static void academicunit() {
        Scanner sc = new Scanner(System.in);
        int[] unit = new int[8];
        System.out.println("Units: ");
        int i = 0;
        while (i < unit.length) {
            unit[i] = sc.nextInt();
            i  ;
        }
    }

    static void finaloutput() {
        String courses_text = "Courses:";
        String units_text = "Units:";
        System.out.format(courses_text,1);
        System.out.format("]" units_text,1);
        System.out.println();
        System.out.format(Arrays.toString(subj), 1);
        System.out.format("]" unit, 1); 
    }     
}

This code shows as:

Courses: 
asd //user input
asd
asd
asd
asd
asd
as
dasd
Units: 
3 //user input
3
3
3
3
3
3
3
ACADEMIC INFORMATION
Program: asd
Courses:    1Units:
[asd, asd, asd, asd, asd, asd, asd, asd]    10

I am trying to make this as

ACADEMIC INFORMATION
Program: asd
Courses:        Unit:
asd             3
asd             3
asd             3
asd             3
asd             3
asd             3
asd             3
asd             3

CodePudding user response:

First of all I recommend you search about formatting specifiers, you are looking for something like "%-10s", where % mark where the specifier begin, - align the text to the left, 10 specifies the resulting string desired length and s indicates that the parameter will be a String.

Your code should look like:

static void finaloutput() {
    String courses_text = "Courses:";
    String units_text = "Units:";
    System.out.format("%-10s",courses_text);
    System.out.format(units_text);
    System.out.println();
    
    for (int i = 0; i < subj.length; i  ) {
        System.out.format("%-10s",subj[i]);
        System.out.format(""   unit[i]);
        System.out.println(); 
    }
    
} 

CodePudding user response:

Here is my solution.

    package com.src;

import java.util.*;

public class Solution {
    static Map<String, List<String>> programAndCourses = new TreeMap<>();
    static Map<String, Integer> coursesAndUnits = new TreeMap<>();
    static Scanner sc;
    static Integer coursesCount = 3;
    static Integer unitCount = 3;
    public static void main(String[] args) {
        enterProgramAndCourses();
        addUnitToCourses();
        finalOutput();
    }

    static void enterProgramAndCourses() {
        sc = new Scanner(System.in);
        System.out.println("Enter program ");
        String program = " ";
        if (sc.hasNext()) {
            program = sc.nextLine();
        }
        System.out.println("Enter courses");
        List<String> courses = new ArrayList<>();
        for (int i = 0; i < coursesCount; i  ) {
        if (sc.hasNext()) {
            courses.add(sc.nextLine());
        }
        }
        programAndCourses.put(program, courses);

    }

    static void addUnitToCourses() {
        System.out.println("Add units to courses");
        for (int i = 0; i < unitCount; i  ) {
            if (sc.hasNext()) {
                int unitsCount = sc.nextInt();
                if (unitsCount >= 0){
                for (Map.Entry<String, List<String>> programAndCourses : programAndCourses.entrySet()) {
                    for (String course : programAndCourses.getValue()) {
                        coursesAndUnits.put(course, unitsCount);
                    }
                }
                }
            }
        }
        sc.close();
    }

    static void finalOutput() {
        for (Map.Entry<String, List<String>> entry : programAndCourses.entrySet()) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key   ":"   value);
        }
        for (Map.Entry<String, Integer> entry: coursesAndUnits.entrySet()) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key   "        "   value);
        }
    }
}

But I also recommend to review this topic to make command line application https://www.baeldung.com/spring-boot-console-app

OUTPUT:

Enter program 
programming
Enter courses
java
python
c  
Add units to the courses
    3
    2
    1
    programming:        [java, python, c  ]
    c          1
    java        1
    python        1
  • Related