Arrays showing vertically instead of horizontally - java

I am sort of new to programming and I am working on a school assignments on arrays
I am suppose to write a program that stores statistics using arrays.
import java.io.*;
public class HockeyLeague {
static final int Rows = 7;
static final int Cols = 8;
static double HockeyChart [][] = new double[Rows][Cols];
static HockeyLeague HL = new HockeyLeague();
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws IOException {
while(true){
System.out.println("Welcome to the NHL statistic program");
System.out.println("Would you like to proceed to the program? <y for yes, n for no>");
final String menuDecision = br.readLine();
if (menuDecision.equals("n")) {
System.out.println("Goodbye");
System.exit(0);
}
if (menuDecision.equals("y")) {
while(true) {
System.out.println("The 8 teams are Toronto Maple Leafs, Montreal Canadiens, Ottawa Senators, Detroit Red Wings, Boston Bruins,"+
" Chicago Blackhawks, New York Islanders, and Pitsburg Penguins");
System.out.println("To input statistics for Toronto, enter '0' ");
System.out.println("To input statistics for Montreal, enter '1' ");
System.out.println("To input statistics for Ottawa, enter '2' ");
System.out.println("To input statistics for Detroit, enter '3' ");
System.out.println("To input statistics for Boston, enter '4' ");
System.out.println("To input statistics for Chicago, enter '5' ");
System.out.println("To input statistics for New York, enter '6' ");
System.out.println("To input statistics for Pitsburg, enter '7' ");
int numString = Integer.parseInt(br.readLine());
Info (numString);
}
}
}
}
public static double[][] Info(int teamInput)throws IOException{
System.out.println("Enter the amount of games played");
int games = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][1] = games;
System.out.println("Enter the amount of wins");
int wins = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][2] = wins;
System.out.println("Enter the amount of ties");
int ties = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][3] = ties;
System.out.println("Enter the amount of losses");
int losses = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][4] = losses;
System.out.println("Enter the amount of goals scored");
int goals = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][5] = goals;
for (int i = 0; i < Rows; i ++) {
for (int j = 0; j < Cols;j ++) {
System.out.println(HockeyChart[i][j] + " ");
}
System.out.println(" ");
}
return HockeyChart;
}
}
This is the program I came up with. I dont understand why I get an output that is a long vertical row of numbers instead of row of numbers side by side.
Any help would be appreciated! thanks

In your Info method, while iterating over arrays, you are using System.out.println(), instead of that you will need to use System.out.print() method.
for (int i = 0; i < Rows; i ++) {
for (int j = 0; j < Cols;j ++) {
System.out.print(HockeyChart[i][j] + " ");
}
System.out.println();
}
Second println statement will help you to move to next line after one row is printed.

Take a look at your loop:
for (int j = 0; j < Cols;j ++) {
System.out.println(HockeyChart[i][j] + " ");
}
println is short for Print Line - it prints the given string, and then moves on to the next line. If you want to print all the contents of the array in a single line, you could use System.out.print instead:
for (int j = 0; j < Cols;j ++) {
System.out.print(HockeyChart[i][j] + " ");
}

You are using System.out.println which means print in new line.
You can use System.out.print for same line i.e horizontal row.
Code will be like this
for (int i = 0; i < Rows; i ++)
{
for (int j = 0; j < Cols;j ++) {
System.out.print(HockeyChart[i][j] + " ");
}
System.out.println(" ");
}
return HockeyChart;

Related

i wrote a code using array and methods to display numbers from the smallest o the largest numbers

i wrote a code using array and method that allow the user to enter any number of numbers and
display the numbers sorted from the smallest number to the largest number however the program works but it doesn't show the numbers here is the code that i wrote
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("How many numbers you want to enter? ");
int size = s.nextInt();
int i;
double[] numbers1 = new double[size];
System.out.println("Enter " + numbers1.length + " numbers: ");
getNumbers(numbers1);
double[] numbers2 = new double[numbers1.length];
for (i = 0; i < numbers1.length; i++) {
numbers2[i] = numbers1[i];
}
displayNumbers(numbers1);
System.out.println("The numbers after sorting are: ");
sortNumbers(numbers2);
displayNumbers(numbers2);
}
public static void getNumbers(double[] numbers) {
Scanner s = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = s.nextDouble();
}
}
public static void sortNumbers(double[] numbers) {
double temp;
double pass;
for (pass = 0; pass < numbers.length; pass++) {
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] > numbers[i + 1]) {
temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
}
}
}
}
public static void displayNumbers(double[] numbers) {
Scanner s = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = s.nextDouble();
System.out.print(numbers + " ");
}
System.out.println();
}
}
Your displayNumbers() method is wrong. In the loop you wrote:
numbers[i] = s.nextDouble();
System.out.print(numbers + " " );
You're trying to read again 4 doubles (everytime you call that method) and you're printing the whole array (which doesn't do what you'd expect). What you probably want is this:
System.out.print(numbers[i] + " " );
Your code is inefficient and unclear.
You don't need to create a new Scanner everytime and also why instead of println the array with Arrays.toString(double[]).
You should also make more cleaner instructions and variable names.
Here is an example of how the code should be
public static void main(String[] args)
{
//create a new Scanner with a name that defines it
Scanner scanner = new Scanner(System.in);
//print your instructions
System.out.println("How many numbers would you like to sort?");
//print "> " to let the user to know he should be entering values
System.out.print("> ");
//read the number the user has entered which we will define as how many numbers he would enter next
int totalNumbers = scanner.nextInt();
//create a new array the size of the total numbers
double[] unsortedNumbers = new double[totalNumbers];
//tell the user to enter his unsorted numbers
System.out.println("Enter your unsorted numbers: ");
//loop totalNumbers times until the whole unsortedNumbers is full
for(int index = 0; index < totalNumbers; index++)
{
System.out.print("> ");
unsortedNumbers[index] = scanner.nextDouble();
}
//Print the numbers he entered
System.out.println("You entered: ");
//Arrays.toString prints the array in format [number, number, ...]
System.out.println(Arrays.toString(unsortedNumbers));
//sort the arrays with Arrays.sort which sorts in ascending numerical order
Arrays.sort(unsortedNumbers);
//Print the final result - sorted numbers
System.out.println("Sorted: " + Arrays.toString(unsortedNumbers));
}

Where am I doing wrong in getting the outputs?

import java.util.Scanner;
public class Qa2QeArray2 {
public static void main(String[] args) {
int numOfAircrafts;
Scanner sc = new Scanner(System.in);
System.out.print("Enter num Of Aircrafts : ");
numOfAircrafts = sc.nextInt();
//To Determine the length of the Array
String[] stringArray = new String[numOfAircrafts];
System.out.println("Please enter the names of the Aircrafts : ");
for(int i = 0; i < numOfAircrafts; i++)
{
stringArray[i] = sc.next();
}
//To insert the string values to the Array
double[] doubleArray = new double[numOfAircrafts];
System.out.println("Please enter the shipment rate through each aircraft carrier : ");
for(int i = 0; i < numOfAircrafts; i++)
{
doubleArray[i] = sc.nextDouble();
}
//To insert the double values to the Array
String[] dubArr = new String [stringArray.length * doubleArray.length];
for (int x = 0; x < stringArray.length ; x++)
for (int y = 0; y < doubleArray.length ; y++) {
System.out.println(stringArray[x] + " \t " + doubleArray[y]);
}
}
}
I am looking for o/p as below
Emirates 20.89
Indigo 10.34
But I am getting o/p as
Emirates 20.89
Emirates 10.34
Indigo 20.89
Indigo 10.34
You have two nested loops that are running.
The x loop and y loop.
When x=0 prints the values and again when x=1
it prints values.
That is why you are getting output two times.
Remove one loop and you are good to go.

Java program keeps exiting program prematurely

I need to build a simple automaton for my Automata class. I am using Java and I cannot figure out for the life of me why my program keeps exiting prematurely. I've tried debugging it, having print statements everywhere to figure out where it's stopping, and although I know where it stops, I do not see anything that would make the program stop working. The stoppage happens on line 27 (Right where I SOP "Enter a string of digits...".
Knowing me it's probably something simple, but I cannot figure this one out.
import java.util.*;
public class hw1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please indicate the number of states");
int numState = input.nextInt();
int[] state = new int[numState];
boolean[] accept = new boolean[numState];
for (int i = 0; i < numState; i++) {
System.out.println("Is the state q" + (i + 1) + " a final state? (Answer 1 for yes; 0 for no)");
int finalState = input.nextInt();
if (finalState == 1)
accept[i] = true;
} // for
System.out.println("Enter the number of symbols s: ");
int numSym = input.nextInt();
int[][] next = new int[numState][numSym];
for (int i = 0; i < numState; i++) {
for (int j = 0; j < numSym; j++) {
System.out.println("What is the number for the next state for q" + i + " when it gets symbol " + j);
next[i][j] = input.nextInt();
}//nested for
}//for
String digits = input.nextLine();
System.out.print("Enter a string of digits (0-9) without spaces to test:");
int[] digitArray = new int[digits.length()];
for (int i = 0; i < digits.length(); i++) {
digitArray[i] = digits.charAt(i);
}
for (int i = 0; i < digits.length(); i++) {
System.out.print(digitArray[i] + " ,");
}
System.out.println("end of program");
}// main;
}// class
Change your code to :
input.nextLine();
System.out.print("Enter a string of digits (0-9) without spaces to test:");
String digits = input.nextLine();
This will get and ignore the newline character left in the stream after call to nextInt()

Console bar graph using loop, without graphics JAVA

Currently working on example from a book loop chapter. Basically, there are 4 car sellers and each should have input how many cars they sold. Below inputs, their names should be printed out and in same line number of cars they sold(from input), but not in numbers, using X or star *. x or star represents that number input.
Here is my code so far. I am stuck on printing that X or star next to a name. It prints out next to wrong name. Any way using loops only without array?
import java.io.*;
public class BarGraphCarSold {
public static void main(String[] args)throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no of cars Bob sold >> ");
int bobSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars Pam sold >> ");
int pamSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars John sold >> ");
int johnSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars Kim sold >> ");
int kimSold = Integer.parseInt(buf.readLine());
System.out.println();
System.out.println("Car Sales for Month");
System.out.print("Bob \n");
System.out.print("Pam \n");
System.out.print("John \n");
System.out.print("Kim ");
for(int i = 1; i<=bobSold;i++){
System.out.print("X");
}
}
}
OUTPUT
Enter no of cars Bob sold >> 5
Enter no of cars Pam sold >> 7
Enter no of cars John sold >> 4
Enter no of cars Kim sold >> 6
Car Sales for Month
Bob
Pam
John
Kim XXXXX
You are close.
try something like this.
System.out.print("Bob ");
for(int i = 0; i < bobSold; i++){
System.out.print("X");
}
System.out.print("\n");
You are printing out each of the names then have newline (\n) then you try to print the X's. So you will need to print their name then the X's then the new line. You will need a loop like that for each of the names you want to print out with their X's.
There are many different ways to do this. You can have a String with their name then loop through and add the X's to the string then print it all out at one time.
String dealer = "Bob ";
for(int i = 0; i < bobSold; i++){
dealer += "X";
}
dealer += "\n";
System.out.print(dealer);
With Arrays
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] dealers = {"Bob","Pam","John","Kim"};
int[] sales = new int[4];
try {
for(int i=0; i < dealers.length; i++){
System.out.print("Enter number of cars "+dealers[i]+" sold: ");
sales[i] = Integer.parseInt(buf.readLine());
}
}catch(IOException e){
System.err.println("Error Reading input");
System.err.println(e.getMessage());
}
System.out.println();
System.out.println("Car Sales for Month");
for(int i = 0; i < dealers.length; i++) {
System.out.print(dealers[i]+" ");
for(int j = 0; j < sales[i]; j++) {
if(j == (sales[i]-1)) {
System.out.println("X");
}else {
System.out.print("X");
}
}
}
}
}
Have fun. Code On.
You need to print the X's for each person.
System.out.print("Bob - ");
for(int i = 1; i<=bobSold;i++){
System.out.print("X");
}
System.out.print("\nPam - ");
for(int i = 1; i<=pamSold;i++){
System.out.print("X");
}
System.out.print("\nSame - ");
for(int i = 1; i<=samSold;i++){
System.out.print("X");
}
System.out.print("\nKim - ");
for(int i = 1; i<=kimSold;i++){
System.out.print("X");
}

Error when looping on keyboard input

I have a loop that is supposed to store information into an array of objects, but for some reason, it always skips the first input.
public class GerbilData {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("How many different food items do the gerbils eat?");
int n1 = keyboard.nextInt();
Food[] gerbilFood = new Food[n1];
String temp;
int temp2;
int count = 1;
for (int a = 0; a < n1; a++){
gerbilFood[a] = new Food();
}
int j = 0;
while (j < n1){
System.out.println("Name of food item " + count + ":");
temp = keyboard.nextLine();
gerbilFood[j].setName(temp);
count++;
j++;
}
keyboard.nextInt() is only reading an integer from the keyboard, not reading the return character. So, when you first call keyboard.nextLine() you get the \n of the getInt().
Try this instead :
int n1 = keyboard.nextInt();
keyboard.nextLine();

Categories

Resources