The following codeparts are exactly the same, however, it prints a different result and I am unable to understand why.
//First Part
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.println("*");
System.out.println();
}
System.out.println();
//Second Part
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.print ("*");
System.out.println();
}
Since you are using print() in the second part of your code you should insert \n to wrap text, Because println() will do it by default.
Your code to work should be like :
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++){
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}
System.out.println();
for (int row = 1; row <= MAX_ROWS; row++){
for (int star = 1; star <= row; star++) {
System.out.print("*\n");
}
System.out.println();
}
Or like this:
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++) {
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}
System.out.println();
for (int row = 1; row <= MAX_ROWS; row++) {
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}
Related
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)));
}
The first piece of code results in only one column and bunch of rows. The second piece of code results in 5x5 board. What is wrong with the first piece of code. It's probably something stupid and simple, but I couldn't find it.
int size = 5; // size of the board
for (int row = 1; row <= size; ++row) {
for (int col = 1; col <= size; ++col) {
System.out.println("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print("# ");
}
System.out.println();
}
int size = 5;
for(int row=1;row<=size;++row)
{
for (int col = 1; col <= size; ++col)
{
System.out.print("# ");
}
System.out.println();
}
int height = 5;
int width = 5;
for(int i=0;i<height;i++)
{
for (int j = 0; j < width; j++)
{
System.out.print("# ");
}
System.out.println();
}
I need to make a multiplication table that shows 1 * 1 up to 12 * 12. I have this working but it needs to be in 13 columns in a format that looks like the diagram below, really appreciate any help.
1 2 3 4 5 6 7 8 9 10 11 12
1 1 2 3 4 5 ...
2 2 4 6 8 10 ....
3
4
5
6
...
Code so far:
public class timetable {
public static void main(String[] args) {
int[][] table = new int[12][12];
for (int row=0; row<12; row++){
for (int col=0; col<12; col++){
table[row][col] = (row+1) * (col+1);
}
}
for (int row = 0; row < table.length; row++) {
for (int col = 0; col < table[row].length; col++) {
System.out.printf("%6d", table[row][col]);
}
System.out.println();
}
}
}
Print column headings before printing the table, and print row headings at the start of each row. You can use the code below.
int[][] table = new int[12][12];
for (int row=0; row<12; row++){
for (int col=0; col<12; col++){
table[row][col] = (row+1) * (col+1);
}
}
// Print column headings
System.out.printf("%6s", "");
for (int col = 0; col < table[0].length; col++) {
System.out.printf("%6d", col+1);
}
System.out.println();
for (int row = 0; row < table.length; row++) {
// Print row headings
System.out.printf("%6d", row+1);
for (int col = 0; col < table[row].length; col++) {
System.out.printf("%6d", table[row][col]);
}
System.out.println();
}
This only prints 9x9 timetable, if you need to change it 12x12, then just change the numbers in the code from "9" to "12", and add more "----" lines in the system output to match it
This includes " * |" and "----" ...
Thought this might be helpful for anyone else
Output:
9x9 Timetable
public class timetable2DArray
{
public static void main(String[] args)
{
int[][] table = new int[9][9];
for (int row=0; row<9; row++)
{
for (int col=0; col<9; col++)
{
table[row][col] = (row+1) * (col+1);
}
}
// Print column headings
System.out.print(" * |");
for (int col = 0; col < table[0].length; col++)
{
System.out.printf("%4d", col+1);
}
System.out.println("");
System.out.println("------------------------------------------");
for (int row = 0; row < table.length; row++)
{
// Print row headings
System.out.printf("%4d |", row+1);
for (int col = 0; col < table[row].length; col++)
{
System.out.printf("%4d", table[row][col]);
}
System.out.println();
}
}
}
int [][] A = new int[5][5];
int [][] B = new int[5][5];
for (int row = 0; row < A.length; row++) {
System.out.println();
for (int col = 0; col < A.length; col++) {
B[row][col] = (row+1)*(col+1);
System.out.print("\t");
System.out.printf("%2d", B[row][col]);
}
}
You could implement a timesTable() method. Here's my code, modify it anyway you would like.
//main driver
public static void main(String[] args) {
int[][] data; //declaration
data = timesTable(4,6); //initialisation
for (int row = 0; row < data.length ; row++)
{
for (int column = 0; column < data[row].length; column++)
{
System.out.printf("%2d ",data[row][column]);
}
System.out.println();
}
}
//method
public static int[][] timesTable(int r, int c)
{
int [][] arr = new int[r][c];
for (int row = 0; row < arr.length ; row++)
{
for (int column = 0; column < arr[row].length; column++)
{
arr[row][column] = (row+1)*(column+1);
}
}
return arr;
}
This is what I written:
import javax.swing.JOptionPane;
public class JavaTest{
public static void main(String[] args){
String numberString = JOptionPane.showInputDialog(null, "Enter number here: ",
null, JOptionPane.INFORMATION_MESSAGE);
int number = Integer.parseInt(numberString);
printMatrix(number);
}
public static void printMatrix(int n){
int[][] myList = new int[n][n];
String output = "";
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
myList[row][col] = (int) (Math.random() * 2);
}
}
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
if (col == n){
output += "\n" + myList[row][col];
}
else{
output += myList[row][col] + " ";
}
}
}
if (n < 0){
JOptionPane.showMessageDialog(null,
"Invalid input!");
}
else{
JOptionPane.showMessageDialog(null,
output);
}
}
}
I run it and enter 3 in a dialog box, and the eclipse IDE shows that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at JavaTest.printMatrix(JavaTest.java:17)
at JavaTest.main(JavaTest.java:8)
I guess at line 17 and 8 the program goes wrong but I don't know how to improve it.
What can I do to improve my code? Thanks!
You're looping from 1 to n:
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
Indexes begin at 0, not at 1. The loops should be from 0 to n-1:
for (int row = 0; row < n; row++){
for (int col = 0; col < n; col++){
(This same error may likely be in other places than just the first line that threw the exception.)
OK I'm embarrassed I have to ask this but I'm stuck so here we go, please nudge me in the right direction.
We need to create this using a nested loop:
*
**
***
****
*****
Here's what I came up with.
int row,col;
for(row = 5; row >= 1; row--)
{
for (col = 0; col < 5 - row; col++)
println("");
for(col = 5; col >= row; col--)
print("*");
}
It ALMOST works but it prints spaces between each row and I cannot for the life of me figure out why.
Why not just one println(""); at the end of the loop instead of looping that statement? You only need the one new line per row of stars.
I think you only want to use one inner loop, to print each row of stars. Then print a newline at the end of each row:
int row, col;
for(row = 1; row <= 5; row++) {
for(col = 0; col < row; col++) {
print("*");
}
println("");
}
Why don't you do this:
for (int row = 0; row < 5; row++) {
for (int col = 0; col <= row; col++) {
System.out.print("*");
}
System.out.print("\n");
}
Why don't you simplify it?
int row, col;
for(row=0; row<5; row++) {
for(col=0;col<=row;col++) {
System.out.print("*");
}
System.out.println();
}
Print one row at a time, and then print the line-end
For the sake of only one I/O operation, a function may be a suitable approach:
public String starStarcase(int rows) {
int row, col;
String str="";
for(row = 0; row < rows; row++) {
for(col = 0; col <= row; col++) {
str += "*";
}
str += "\n";
}
return str;
}
To print the result:
System.out.println(starsStaircase(5));
You can try this, it should work:
for(int j = 0; j < row; j++){
for(int i = 1; i <= row; i++){
System.out.print(i < row-j ? " " : "#");
}
System.out.println("");
}