Need assistance with creating a table for java project - java

So, I'm super new to programming in general. I've been learning Java through a course and one of our projects is to make a program that reads a text file and prints the number of vowels in a little table. (See below example.)
A|##
E|##
I|####
O|#
U|
I've gotten some work done. For instance getting the total number of vowels. (See below.)
String words = input.nextLine();
System.out.println(words);
for (int i=0;i<words.length(); i++)
{
char car = words.charAt(i);
if (car =='a'|| car == 'i' || car == 'e' || car=='o' || car == 'u')
{
count ++ ;
}
This prints the total number ^
Output: 9
After that I worked out a way to print the table. (See below.)
//Process for creating the chart (NOT FINAL)
System.out.println(count);
System.out.print("A|");
final int MAX_ROWS = 1 ;
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int a =5; a >= row; a--)
System.out.print("#");
}
System.out.println("");
System.out.print("E|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int e =5; e >= row; e--)
System.out.print("#");
}
System.out.println("");
System.out.print("I|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int ii =5; ii >= row; ii--)
System.out.print("#");
}
System.out.println("");
System.out.print("O|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int o =5; o >= row; o--)
System.out.print("#");
}
System.out.println("");
System.out.print("U|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int u =5; u >= row; u--)
System.out.print("#");
}
Output:
A|#####
E|#####
I|#####
O|#####
U|#####
However, I can't figure out how to get the number of each vowel into the table. I've provided the whole thing below to look through for broader context. I apologize for how rough it is, I'm just a bit lost. And I couldn't find anything with my specific table requirement on the Web. I hope one of you can help. Thanks for any answers.
P.S. Since this is a class were I'm trying to learn I want to figure this out on my own. Pointers with explanations would be great. A push in the right direction if you will.
Edit: I want to clarify something that I explained poorly. The ints a-u are placeholders and the goal was to use vowels from a text doc. The example from the test doc is "orange tree apple hope". Hope that clarifies.
Full Code:
import java.util.Scanner ;
import java.io.File ;
public class VowelCounter
{
public static void main(String[] args) throws Exception
{
File text = new File ("Doc.txt") ;
Scanner input = new Scanner(text) ;
//File Doc.txt is scanned and is used
int count = 0 ;
while (input.hasNext())
{
String words = input.nextLine();
System.out.println(words);
for (int i=0;i<words.length(); i++)
{
char car = words.charAt(i);
if (car =='a'|| car == 'i' || car == 'e' || car=='o' || car == 'u')
{
count ++ ;
}
//Test Values (NOT FINAL)
int a = 5 ;
int e = 5;
int ii = 5;
int o = 5;
int u = 5;
}
//Process for creating the chart (NOT FINAL)
System.out.println(count);
System.out.print("A|");
final int MAX_ROWS = 1 ;
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int a =5; a >= row; a--)
System.out.print("#");
}
System.out.println("");
System.out.print("E|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int e =5; e >= row; e--)
System.out.print("#");
}
System.out.println("");
System.out.print("I|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int ii =5; ii >= row; ii--)
System.out.print("#");
}
System.out.println("");
System.out.print("O|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int o =5; o >= row; o--)
System.out.print("#");
}
System.out.println("");
System.out.print("U|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int u =5; u >= row; u--)
System.out.print("#");
}
}
//(ERROR) prints 15 # instead of 4
// a= 2 e=5 i=0 o=2 u=0 (FOR CHECK)
// 9 toatl vowels
}
}

Based on my comments above I remove the outer for loops and replaced your inner for loops with occurrences of each vowel shows up
//Test Values (NOT FINAL)
int aCount = 5 ;
int eCount = 5;
int iCount = 5;
int oCount = 5;
int uCount = 5;
//Process for creating the chart (NOT FINAL)
System.out.println(count);
System.out.print("A|");
final int MAX_ROWS = 1 ;
for(int i =0; i < aCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("E|");
for(int i =0; i < eCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("I|");
for(int i =0; i < iCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("O|");
for(int i =0; i < oCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("U|");
for(int i =0; i < uCount; i++)
System.out.print("#");
}
and this is what #Just another Java programmer suggested
public static void main(String[] args){
Map<Character, Integer> vowelMap = new HashMap<Character, Integer>() {{
put('A', 0);
put('E', 0);
put('I', 0);
put('O', 0);
put('U', 0);
}};
String words = "oiwjroiajfdojasoijfalkdfoiejrf";
for(int i=0;i<words.length();i++){
char c = Character.toUpperCase(words.charAt(i));
if(vowelMap.containsKey(c))
vowelMap.put(c, vowelMap.get(c)+1);;
}
vowelMap.forEach((k,v) -> System.out.println( k+ "|"+ "#".repeat(v)));
}

Related

Count and print even flowers

We have n number of flowers that can be black or white. We have m number of months. At the end of each month, if the number of white flowers is even, we print B for the number of roses that are even, and print F for the rest of the characters.For example:(W=white,B=black)
input:3(n) 2(m)
WBW
BBW
output:FBB
My code just work well just for this example and dont give true answer for other examples.
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of flowers: ");
int flower = input.nextInt();
System.out.println("Enter number of months : ");
int month = input.nextInt();
String[] arr = new String[month];
char ch = ' ';
int count = 0;
for (int j = 0; j < month; j++)
arr[j] = input.next();
for (int i = 0; i < month; i++) {
char[] s = arr[i].toCharArray();
for (int j = 0; j < flower; j++) {
if (s[j] == 'W') {
count++;
}
}
if (count % 2 == 0) {
for (int k = 0; k < flower - count; k++) {
System.out.print('F');
}
for (int b = 1; b <= count; b++) {
System.out.print('B');
}
}
}
}
}
In your for loop
for (int j = 0; j < flower; j++) {
if (s[j] == 'W') {
count++;
}
}
We must remember what s is referring to, char[] s = arr[i].toCharArray(); which means that s will not have a length of flower but will have a length of arr[i].length() or s.length.
So if you change the for loop to use that as its control, it should solve the index out of bounds exception that you get.
The fix would look like:
for (int j = 0; j < s.length; j++) {
if (s[j] == 'W') {
count++;
}
}

How to print a 2d array horizontally multiple times

I am using java and I would like to print a 2d array horizontally multiple time based on user input. However, my array prints vertically, can anyone help?
n=3; //user input
char[][] board = new char[2][3];
char[][] f = new char[board.length][n * board[0].length];
for (int i = 1; i < n + 1; i++) {
int Start = (i * board[0].length) - board[0].length;
int End = i * board[0].length;
for (int row = 0; row < f.length; row++) {
for (int col = nStart; col < nEnd; col++) {
f[row][col] = board[row][col - nStart];
System.out.print(f[row][col]);
}
System.out.println();
}
}
For example board array =
xx
xx
I would like
xxxxxx
xxxxxx
If you want to print 2d array horizontally, you have to repeat printing row n times before next row:
int n = 3; // user input
char[][] board = new char[][] { { 'x', 'x', 'x' }, { '0', '0', '0' } }; //example board
for (int row = 0; row < board.length; row++)
{
for (int i = 0; i < n; i++)
{
for (int col = 0; col < board[row].length; col++)
{
System.out.print(board[row][col]);
}
System.out.print("\t"); //arrays separated by tab
}
System.out.println();
}
Output:
xxx xxx xxx
000 000 000
I hope this help.
Your solution works fine except that you made a little mistake in the last lines of your code. I think you want to print a space between every entry by using System.out.println() but println prints a line-break at the end. So your code should look like this:
n=3; //user input
char[][] board = new char[2][3];
char[][] f = new char[board.length][n * board[0].length];
for (int i = 1; i < n + 1; i++) {
int Start = (i * board[0].length) - board[0].length;
int End = i * board[0].length;
for (int row = 0; row < f.length; row++) {
for (int col = nStart; col < nEnd; col++) {
f[row][col] = board[row][col - nStart];
System.out.print(f[row][col]);
}
System.out.print(" "); // Print a space between every single output
}
}
Or if you dont wan't a space at all remove the line completely. Or change the space to a comma, point or whatever you need.
Removing the copied array f.
What you need is exchanging row,col in loop.
n=3; //user input
char[][] board = new char[2][3];
for (int j = 0; j < board[0].length; j++) {
for (int i = 0; i < n ; i++) {
System.out.print(board[i][j]);
System.out.print(" ");
}
System.out.println();
}

Inputs for column length is returning unintended length

While working on a code for making a irregular 2D-array, I discovered a weird error while messing with different inputted values for the column. While the row works, the column length input returns either the wrong amount or a null pointer error occurs. I'm not sure what might be causing this since inputs such as ( 1 , 2 , 3) returns the correct table but (2 , 1, 3) will not. Also in a row of 4 with column inputs of (2, 3, 4, 5) returns "index out of bounds exception: 5" when there shouldn't be able to be out of bounds because of the while loop that should keep it with in reasonable range. Neither the main nor the display method seems to be saving the intended column length correctly and I can't seem to spot why.
It seems the array is set to 3 rows with column length of 1 , 2 , 3.
The output for row(3) and column(2,3,1) gives:
A:2.0
B:2.0 2.0
C:2.0 2.0 2.0
when I want is:
A:2.0 2.0
B:2.0 2.0 2.0
C:2.0
The code:
import java.util.Scanner;
//================================================================
public class ArrayIrreg {
//----------------------------------------------------------------
private static Scanner Keyboard = new Scanner(System.in);
public static void main(String[] args) {
//----------------------------------------------------------------
char group, rLetter,letter;
String choice;
int sum = 0;
int num = 10; // for test
int rows = 10;
int columns = 8;
//greetings
System.out.println("");
System.out.println("Welcome to the Band of the Hour");
System.out.println("-------------------------------");
// creating 2d array
System.out.print("Please enter number of rows : ");
rows = Keyboard.nextInt();
Keyboard.nextLine();
while (rows < 0 || rows >= 10) {
System.out.print("ERROR:Out of range, try again : ");
rows = Keyboard.nextInt();
Keyboard.nextLine();
}
double[][] figures = new double[rows + 1][num];
for(int t = 0; t < rows; t++) {
rLetter = (char)((t)+(int)'A');
System.out.print("Please enter number of positions in row " + rLetter + " : ");
columns = Keyboard.nextInt();
Keyboard.nextLine();
while(columns < 0 || columns >= 8) {
System.out.print("ERROR:Out of range, try again : ");
columns = Keyboard.nextInt();
Keyboard.nextLine();
}
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
}
// filling the array
for(int row = 0; row < figures.length; ++row) {
for(int col = 0; col < figures[row].length; ++col) {
figures[row][col] = 2.0;
}
}
// printing the array
for(int row = 1; row < figures.length; ++row) {
// printing data row
group = (char)((row-1)+(int)'A');
System.out.print(group + " : ");
for(int col = 0; col < figures[row].length; ++col) {
System.out.print(figures[row][col] + " ");
System.out.print(" ");
}
System.out.print("["+","+avg(figures)+"]");
System.out.println();
}
//----------MENU
System.out.print("(A)dd, (R)emove, (P)rint, e(X)it : ");
choice = Keyboard.next();
letter = choice.charAt(0);
letter = Character.toUpperCase(letter);
if(letter == 'P') {
display(figures);
}
}
public static void display(double x[][]) {
int average, total;
char group;
System.out.println(" ");
for(int row=1;row<x.length;row++) {
group = (char)((row-1)+(int)'A');
System.out.print(group+" : ");
for(int column=0;column<x[row].length;column++){
System.out.print(x[row][column]+" ");
}
System.out.print("["+","+avg(x)+"]");
System.out.println();
}
}
public static int avg(double[][] temp) {
int sum = 0;
int avg = 0;
for (int col = 0; col < temp[0].length; col++) {
sum = 0;
for (int row = 0; row < temp.length; row++)
sum += temp[row][col];
System.out.println(sum);
}
avg = sum / temp.length;
return avg;
}
}
In Creating 2D Array,
Change this
double[][] figures = new double[rows + 1][num];
to
double[][] figures = new double[rows][num];
and
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
to
figures[t] = new double[columns] ;
In Printing Array
Change this
for(int row = 1; row < figures.length; ++row) {
group = (char)((row-1)+(int)'A');
to
for(int row = 0; row < figures.length; ++row) {
group = (char)((row)+(int)'A');
In display function
Change this
for(int row = 1; row < x.length; ++row) {
group = (char)((row-1)+(int)'A');
to
for(int row = 0; row < x.length; ++row) {
group = (char)((row)+(int)'A');
It's this
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
I think you meant actually
figures[t] = new double[columns];

Adding Elements of an Array (sum of rows and columns)

I'm looking for a way to add up the elements of the rows of an array and get that sum. I have to get the column's sum as well.
The array looks something like this:
{{45.24, 54.67, 32.55, 25.61},
{65.29, 49.75, 32.08, 26.11},
{25.24, 54.33, 34.55, 28.16}};
For example, I would add 45.24, 65.29, and 25.24 to get the sum of that part of the columns. I would then have to add the other 3 columns up as well.
Same goes for the rows.
I keep getting errors concerning the variable types. Is there a way to do this? Thanks.
The logic Would be --->
for(i = 0; i < columns; i++)
{
for(j=0; j<rows; j++)
{
sum+=arr[j][i];
}
}
Opposite for Columns
I think you should define the type of numbers your array will handle, if I use float numbers I can have some code like the class bellow to do the type of operations you are asked for. You can also add some decimal formatting.
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
float myarray[][]= {
{45.24f, 54.67f, 32.55f, 25.61f},
{65.29f, 49.75f, 32.08f, 26.11f},
{25.24f, 54.33f, 34.55f, 28.16f}
};
float row[] = new float[3];
float column[] = new float[4];
for (int i=0; i < 3; i++) {
float rowvalue = 0f;
for (int j=0; j < 4; j++) {
System.out.print(myarray[i][j]+" ");
rowvalue+=myarray[i][j];
}
row[i]=rowvalue;
System.out.println("");
}
System.out.println("");
for (int i=0; i < 4; i++) {
float colvalue = 0f;
for (int j=0; j < 3; j++) {
System.out.print(myarray[j][i]+" ");
colvalue+=myarray[j][i];
}
column[i]=colvalue;
System.out.println("");
}
System.out.println("Rows answer:");
for (int i=0; i < 3; i++) {
System.out.println(row[i]);
}
System.out.println("Columns answer:");
for (int i=0; i < 4; i++) {
System.out.println(column[i]);
}
}
}
Suppose you have nxn matrix. The idea is to identify the pattern.
Row values
i j
0 0
0 1
0 2
Column values
i j
0 0
1 0
2 0
The position of i and j values is reversed.
Let's assume we have an array a[][]. The logic would be:
for (int i=0; i<n; i++) {
int row = 0, col = 0;
for (int j=0; j<n; j++) {
row += a[i][j];
col += a[j][i];
}
System.out.println("row" + i + " = " + row);
System.out.println("col" + i + " = " + col);
}
I assumed you wanted the sum of each row and column separately. You can modify it accordingly.

Why is my array printing out differently on different computers

For our assignment. We have to create an Absolon game. But first we have to get the display board right. I programmed the display using a 2D array, 11 by 21. Using this code. And i got the following answer and it printed out correctly. (IDE Neatbeans)
char [][] board = new char [11][21];
for(int column =6; column <= 14; column=column+2 ) {
int row = 0;
board[row][column] = '=';
}
for(int column =6; column <= 14; column=column+2 ) {
int row = 1;
board[row][column] = 'o';
}
for(int column =6; column <= 14; column=column+2 ) {
int row = 10;
board[row][column] = '=';
}
for(int column =6; column <= 14; column=column+2 ) {
int row = 9;
board[row][column] = 'x';
}
for(int column =5; column <= 15; column=column+2 ) {
int row = 2;
board[row][column] = 'o';
}
for(int column =5; column <= 15; column=column+2 ) {
int row = 8;
board[row][column] = 'x';
}
for(int column =5; column <= 15; column=column+2 ) {
int row = 8;
board[row][column] = 'x';
}
for(int column =8; column <= 12; column=column+2 ) {
int row = 3;
board[row][column] = 'o';
}
for(int column =8; column <= 12; column=column+2 ) {
int row = 7;
board[row][column] = 'x';
}
int j=1;
for(int column =4; column >= 0; column-- ) {
board[j][column] = '"';
j = j+1;
}
int l=1;
for(int column =16; column <= 20; column++ ) {
board[l][column] = '"';
l = l+1;
}
int m=6;
for(int column =1; column <= 4; column++ ) {
board[m][column] = '"';
m = m+1;
}
int n=6;
for(int column =19; column >= 16; column-- ) {
board[n][column] = '"';
n = n+1;
}
//Print Out Board
for(int row =0; row < board.length; row++) {
for(int column =0; column < board[row].length; column++) {
System.out.print(board[row][column] + "");
}
System.out.println();
}
}
}
This Is The Link to my sample output.
http://postimg.org/image/dx656twrf/
But when i went to campus working on the same problem also using neatbeans. It gave me a completely new output.
=====
"ooooo"
"oooooo"
"ooo"
""
""
""
"xxx"
"xxxxxx"
"xxxxx"
=====
Whats wrong. Is it the IDE or my code. please help.
You have not initialized the characters in board to anything so they default to zero which prints differently on various systems. Initialize board with blanks and it will work.
The memory of your multi-dimensional char array is initialized with null characters and different systems/fonts may render it differently.
Why does \0 print different output in different system in java

Categories