My program compiles and runs but my spacing for "Enter a size..." is incorrect. Would like some help with spacing, and System.out.println formatting in the loop for my program to have proper spacing as intended.
import java.util.Scanner;
public class PrintCustomizedDiamond {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a letter:");
char ch = s.next().charAt(0);
int a = 0, b, c, d;
d = a - 1;
while (a < 6 || a % 2 == 1) {
System.out.print("Enter a size (even number no less than 6): ");
a = s.nextInt();
if (a % 2 == 0) {
System.out.print("");
}
else {
System.out.println("");
}
}
d = (a / 2) - 1;
for (b = 2; b <= a; b = b + 2) {
for (c = 0; c < d; c++) {
System.out.print(" ");
}
for (c = 0; c < b; c++) {
System.out.print(ch);
}
d = d - 1;
System.out.println();
}
d = 0;
for (b = a; b >= 2; b = b -2) {
for (c = d; c > 0; c--)
{
System.out.print(" ");
}
for (c = 0; c < b; c++)
{
System.out.print(ch);
}
d = d + 1;
System.out.println();
}
}
}
Related
I've been trying to write a program to print the min and max value of 5 integer type variables, using no arrays and only swapping using this technique
if (x > y) {
int tmp = x;
x = y;
y = tmp;
}
now this is what I came up with ( although I know it's possible with only 6 swaps however I can't figure it out):
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
//storing max value in a all the way, and min value in b
if (a < b) {
int tmp = a;
a = b;
b = tmp;
}
if (a < c) {
int tmp = a;
a = c;
c = tmp;
}
if (a < d) {
int tmp = a;
a = d;
d = tmp;
}
if (a < e) {
int tmp = a;
a = e;
e = tmp;
}
if (c < b) {
int tmp = b;
b = c;
c = tmp;
}
if (d < b) {
int tmp = b;
b = d;
d = tmp;
}
if (e < b) {
int tmp = b;
b = e;
e = tmp;
}
System.out.println(b +"\n" + a);
Now this should work, but I also needed to write a code to make sure the program works fine, so I could check all the combinations with 0's and 1's in them.
i.e 2^5=32 combinations. and if one of them fails print it.
I've tried to do this :
for (int a = 0; a <= 1; a++) {
for(int b = 0; b <= 1; b++) {
for(int c = 0; c <= 1; c++) {
for(int d = 0; d <= 1; d++) {
for(int e = 0; e <= 1; e++) {
if (a < b){
int tmp = a;
a = b;
b = tmp;
}
if (a < c) {
int tmp = a;
a = c;
c = tmp;
}
if (a < d) {
int tmp = a;
a = d;
d = tmp;
}
if (a < e) {
int tmp = a;
a = e;
e = tmp;
}
if (c < b) {
int tmp = b;
b = c;
c = tmp;
}
if (d < b) {
int tmp = b;
b = d;
d = tmp;
}
if (e < b) {
int tmp = b;
b = e;
e = tmp;
}
System.out.println(b + "\n" + a);
}
}
}
}
}
but this doesn't work because once there is a "1" in any variable a gets it and then I'm not gonna be able to run the combinations properly..
****basically this is a tester for the code within the loop, so it should test 0000-11111 and print something like verified if all works well, or print the combination for which it got the wrong result.. however I can't see how it's gonna do that .****
any suggestions? no arrays , swapping only...
thanks ahead
I would use Math.min(int, int) and Math.max(int, int) and a long chain of invocations. Like,
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Math.min(Math.min(Math.min(Math.min(a, b), c), d), e);
int max = Math.max(Math.max(Math.max(Math.max(a, b), c), d), e);
Your "swapping piece of code" makes no sense. Here is another implementation
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
min = (a < min) ? a : min;
min = (b < min) ? b : min;
min = (c < min) ? c : min;
min = (d < min) ? d : min;
min = (e < min) ? e : min;
max = (a > max) ? a : max;
max = (b > max) ? b : max;
max = (c > max) ? c : max;
max = (d > max) ? d : max;
max = (e > max) ? e : max;
Why don't you store the a,b,c,d,e values in the innermost loop and reassign it back to the variables again after your swapping piece of code? Like so,
public static void main(String args[]) {
for (int a=0 ;a<=1 ;a++ ){
for(int b=0 ;b<=1 ;b++ ){
for(int c=0 ;c<=1 ;c++ ){
for(int d=0 ;d<=1 ;d++ ){
for(int e=0 ; e<=1 ;e++ ){
// Store the variable values
int ap = a;
int bp = b;
int cp =c;
int dp = d;
int ep = e;
// Swapping logic for finding min and max
// Due to swapping, the values of a,b,c,d,e may change
if ( a < b){
int tmp = a;
a=b;
b=tmp;
}
if ( a < c){
int tmp = a;
a=c;
c=tmp;
}
if ( a < d){
int tmp = a;
a=d;
d=tmp;
}
if ( a < e){
int tmp = a;
a=e;
e=tmp;
}
if ( c < b){
int tmp = b;
b=c;
c=tmp;
}
if ( d < b){
int tmp = b;
b=d;
d=tmp;
}
if ( e < b){
int tmp = b;
b=e;
e=tmp;
}
System.out.print(a+""+b+""+c+""+d+""+e+" ");
System.out.println(b + " " + a);
// Reassign the variable values
a = ap;
b = bp;
c=cp;
d=dp;
e=ep;
}
}
}
}
}
}
The purpose of this program is for the user to decide the length (rows) of a triangle, and also decide if it should be facing up or down. And the triangle is made of letters, so it is supposed to look like this:
How many rows would you like? (finish with -1): 4
Do you want the triangle to face up (1) or down (2)? 1
A
A B
A B C
A B C D
How many rows would you like? (finish with -1): 6
Do you want the triangle to face up (1) or down (2)? 2
A B C D E F
A B C D E
A B C D
A B C
A B
A
I have two problems when I try to get the triangle to print facing down, first the letters look like this (it should begin with an A)
F E D C B A
F E D C B
F E D C
F E D
F E
F
And the letters are followed by loads of different characters that I don't want. I've tried so many things and nothing seems to be working. I could really use some advice.
This is my code so far:
import java.util.Scanner;
public class Triangle {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int a = 0;
int b = 0;
while (a != -1) {
System.out.println("How many rows would you like? (finish with -1):");
a = scan.nextInt();
if (a != -1) {
b = a - 1;
int j = 'A';
char alphabet = (char) (j + 'A');
System.out.println("Do you want the triangle to face up (1) or down (2)?");
int c = scan.nextInt();
if (c == 1) {
for (int i = 1; i <= b + 'A'; i++) {
for (j = 'A'; j <= i; j++)
System.out.print((char) j + " ");
System.out.println(alphabet);
}
} else {
for (int i = 1; i <= b + 'A'; i++) {
for (j = b + 'A'; j >= i; j--)
System.out.print((char) j + " ");
System.out.println(alphabet);
}
}
}
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = 0;
int b = 0;
while (a != -1) {
System.out.println("How many rows would you like? (finish with -1):");
a = scan.nextInt();
if (a != -1) {
System.out.println("Do you want the triangle to face up (1) or down (2)?");
int c = scan.nextInt();
if (c == 1) {
for (int i = 0; i < a; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char) (j + 'A'));
}
System.out.println();
}
} else {
for (int i = 0; i < a; i++) {
for (int j = a; j > i; j--) {
System.out.print((char) (a - j + 'A'));
}
System.out.println();
}
}
}
}
}
Adopt a more modular solution given below.
It prints both triangles composed of 4 rows, one facing up and the other facing down.
public static void main(String args[]) {
int a = 4; // # of rows
// Triangle facing up
for (int i = 1; i <= a; i++) // i - How many letters in this row (also row No)
printRow(i);
System.out.println("--------"); // Separator
// Triangle facing down - Start from the longest row, then decrease its length
for (int i = a; i > 0; i--)
printRow(i);
}
static void printRow(int length) {
for (int j = 0; j < length; j++) // j - char code shift
System.out.printf("%c ", j + 'A');
System.out.println();
}
This solution is more elegant, as the code to print a row is not repeated.
Note also a more natural way to express the length of consecutive rows: For the triangle facing down the loop decreases the row length.
The program needs to execute with no arguments and with arguments. The program does different things depending on the inputs at the command line. Here is the code, line 94 is causing the problem where I have parsed int a (int a = Integer.parseInt(args[0]);)
But I have to parse that int in order to execute the program with five or more command line arguments.
Here is the code, it's long, but the problem is at line 94:
class CommandArgsOrNot{
public static void main(String[] args) {
final int clargs = args.length;
if (clargs==0) {
System.out.print(" ");
System.out.println("Hello");
for (int i = 0; i < 3; i++) {
System.out.print(" ");
}
System.out.println("World !!");
}
if (clargs==1) {
String s = args[0];
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
System.out.println(s);
}
}
if (clargs==2) {
System.out.println("ARGUMENT 1:" + " " + (args[0]));
System.out.println("ARGUMENT 2:" + " " + (args[1]));
}
if (clargs==3) {
final int a = Integer.parseInt(args[0]);
final int b = Integer.parseInt(args[1]);
final int c = Integer.parseInt(args[2]);
if (a * b == c) {
System.out.println("1*2=3");
}
else if (a * c == b) {
System.out.println("1*3=2");
}
else if (b * a == c) {
System.out.println("2*1=3");
}
else if (b * c == a) {
System.out.println("2*3=1");
}
else if (c * a == b) {
System.out.println("3*1=2");
}
else if (c * b == a) {
System.out.println("3*2=1");
}
else {
System.out.println("None");
}
System.out.println();
}
if (args.length == 4) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = Integer.parseInt(args[3]);
if (a == b && a == c && a == d) {
System.out.println("1");
}
else if (a != b && b != c && c != d) {
System.out.println("4");
}
else if (a == b) {
System.out.println("2");
}
else if (c == d && a != b) {
System.out.println("3");
}
else if (b == c) {
System.out.println("2");
}
else if (a == b && a == c) {
System.out.println("3");
}
else if (d == c && d == b) {
System.out.println("3");
}
else if (a == d) {
System.out.println("2");
}
else if (a == c && b == d) {
System.out.println("2");
}
else if (a == d && b != c) {
System.out.println("3");
}
}
int a = Integer.parseInt(args[0]);
long answer = 1;
long result = 0;
if (a < 0) {
for(int i = 1; i < args.length; i++) {
answer *= Integer.parseInt(args[i]);
}
System.out.println(answer);
}
else if (a == 0) {
for(int i = 0; i < args.length; i++) {
result += Integer.parseInt(args[i]);
}
System.out.println(result);
}
else if (a == 1) {
for(int i = 0; i < args.length; i++) {
result += Integer.parseInt(args[i]);
}
System.out.println(result);
}
else if (a == 2) {
for(int i = 2; i < args.length; i++) {
if (i % 2 != 0){
answer += Integer.parseInt(args[i]);
}
}
System.out.println(answer);
}
else if (a == 3) {
for (int i = 2; i < args.length; i++) {
if (i % 3 != 0) {
answer += Integer.parseInt(args[i]);
}
}
System.out.println(answer);
}
}
}
Any help would be greatly appreciated, thanks!
You should stop the program by using return keywords if you didn't pass any parameter to your Java program.
eg:
if (clargs==0) {
System.out.print(" ");
System.out.println("Hello");
for (int i = 0; i < 3; i++) {
System.out.print(" ");
}
System.out.println("World !!");
return;
} //please note that there is also many alternative ways to control your program.
But, the main idea is that you should not parse any other args[..] value to int if your agrs[..] value is equal 0.
I believe that your output is-
Hello
World !!
and after this you got an error.
This is because you have accessed int a = Integer.parseInt(args[0]) 0th argument without checking its existence and if no argument is provided to it then it throws the error.
Check existence before accessing.
Your line 94 is called without any checks on the size of args. Try to check if the size is ok before trying to access the array cell.
if(args.length > 1){
int a = Integer.parseInt(args[0]);
//What you want to do with you value.
}
While accessing the array element its index ensure the following.
The array is not null
The index you are trying to access is within the array size.
In your code add the condition before accessing the args[0] like shown below:
if(clargs!=0) int a = Integer.parseInt(args[0]);
als try to declare the int a before this condition and define here to avoid further errors.
Full code:
class VariousCases
{
public static void main(String[] args)
{
final int clargs = args.length;
if (clargs==0)
{
System.out.print(" ");
System.out.println("Hello");
for (int i = 0; i < 3; i++)
{
System.out.print(" ");
}
System.out.println("World !!");
}
if (clargs==1)
{
String s = args[0];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(" ");
}
System.out.println(s);
}
}
if (clargs==2)
{
System.out.println("ARGUMENT 1:" + " " + (args[0]));
System.out.println("ARGUMENT 2:" + " " + (args[1]));
}
if (clargs==3)
{
final int a = Integer.parseInt(args[0]);
final int b = Integer.parseInt(args[1]);
final int c = Integer.parseInt(args[2]);
if (a * b == c) {
System.out.println("1*2=3");
}
else if (a * c == b) {
System.out.println("1*3=2");
}
else if (b * a == c) {
System.out.println("2*1=3");
}
else if (b * c == a) {
System.out.println("2*3=1");
}
else if (c * a == b) {
System.out.println("3*1=2");
}
else if (c * b == a) {
System.out.println("3*2=1");
}
else
{
System.out.println("None");
}
System.out.println();
}
if (args.length == 4) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = Integer.parseInt(args[3]);
if (a == b && a == c && a == d) {
System.out.println("1");
}
else if (a != b && b != c && c != d) {
System.out.println("4");
}
else if (a == b) {
System.out.println("2");
}
else if (c == d && a != b) {
System.out.println("3");
}
else if (b == c) {
System.out.println("2");
}
else if (a == b && a == c) {
System.out.println("3");
}
else if (d == c && d == b) {
System.out.println("3");
}
else if (a == d) {
System.out.println("2");
}
else if (a == c && b == d) {
System.out.println("2");
}
else if (a == d && b != c) {
System.out.println("3");
}
}
// Add the condition to check the arguments length is greater and the index you are trying to access is within the range of the array length
if(clargs!=0)
int a = Integer.parseInt(args[0]);
long answer = 1;
long result = 0;
if (a < 0) {
for(int i = 1; i < args.length; i++) {
answer *= Integer.parseInt(args[i]);
}
System.out.println(answer);
}
else if (a == 0) {
for(int i = 0; i < args.length; i++) {
result += Integer.parseInt(args[i]);
}
System.out.println(result);
}
else if (a == 1) {
for(int i = 0; i < args.length; i++) {
result += Integer.parseInt(args[i]);
}
System.out.println(result);
}
else if (a == 2) {
for(int i = 2; i < args.length; i++) {
if (i % 2 != 0){
answer += Integer.parseInt(args[i]);
}
}
System.out.println(answer);
}
else if (a == 3) {
for (int i = 2; i < args.length; i++) {
if (i % 3 != 0) {
answer += Integer.parseInt(args[i]);
}
}
System.out.println(answer);
}
}
}
I am trying to design a method in java which receive 2 D array and manipulate each column in it. then, save this manipulation whereas each column will be represented as a one value in the return array. My problem is that, I do not know, how can I read every column individuall. Could you suggest me please.
int[] ibraMethod(int[][] InputArray){
int[] Array_After_ibraMethod = new int[24];
int APoints = 0;
int BPoints = 0;
int CPoints = 0;
for(int h = 1; h < 24 ; h++){
System.out.println("\n Hour"+ h);
for(int i = 0; i<InputArray.length; i++){
for(int j = 0; j<InputArray[h].length; j++){
if(InputArray[i][j]==1){
APoints = APoints +3;
}
else if(InputArray[i][j]==-1){
BPoints = BPoints +2;
}
else if(InputArray[i][j]==0){
CPoints = CPoints +1;
}
}
}
Array_After_ibraMethod[h] = Compare(APoints, BPoints, CPoints);
}
/// to print////
System.out.println("\n After ibraMethod");
for (Integer i :Array_After_ibraMethod) {
System.out.print(i.intValue() + " ");
}
return Array_After_ibraMethod;
}
and this is the compare function code
Compare(int a, int b, int c) {
int r;
if ((a > b) && (a > c)) {
r = 1;
System.out.println("\n A ");
} else if ((b > a) && (b > c)) {
r = -1;
System.out.println("\n B ");
} else {
r = 0;
System.out.println("\n C");
}
return r;
}
Use j<InputArray[i].length; instead of j<InputArray[h].length; as j<InputArray[h].length; may give you java.lang.ArrayIndexOutOfBoundsException
I am trying to extend the following code to sort the array if I added a third value 'C'. Would this be possible to do while retaining only one loop. The following code will sort an array with two possible values 'A' and 'B'.
public class TestSort
{
public static void main(String args[])
{
char f[] = {'A','B','B','A','B','B','A','B','A','A','B'};
int k = 0, t = f.length-1;
while(k < t)
{
if(f[k] == 'A')
k = k + 1;
else if(f[k] == 'B')
{
char m = f[t];
f[t] = f[k];
f[k] = m;
t = t - 1;
}
}
System.out.print("\nSorted List\n");
for(char i : f)
System.out.print(i + ", ");
System.out.println();
}
}
Here is an attempt. I don't know if I'm on the right track.
public class TestSort
{
static char f[] = {'C','A','B','A','C','B','A','B','C','C','B','A','B'};
//static char f[] = {'A','A','A','A','A','C','A','C','A','A','C','A','C'};
//static char f[] = {'C','B','B','B','C','B','B','B','C','C','B','C','B'};
//static char f[] = {'A','B','B','B','A','B','C','B','A','A','B','A','B'};
public static void main(String args[])
{
int j = 0, k = 0, t = f.length-1, l = f.length-1;
while(t >= 0)
{
if(f[k] == 'A')
k = k + 1;
else if(f[k] == 'B')
{
char m = f[j];
f[j] = f[k];
f[k] = m;
j = j + 1;
}
else if(f[k] == 'C')
{
char m = f[l];
f[l] = f[k];
f[k] = m;
l = l - 1;
}
for(char i : f)
System.out.print(i + ", ");
System.out.println();
}
}
}
Maybe something like:
public sort(char[] array) {
int[] frequencies = new int[3];
for(char c : array) {
if (c == 'A')
frequencies[0]++;
if (c == 'B')
frequencies[1]++;
if (c == 'C')
frequencies[2]++;
}
int index = 0;
for (int i = 0; i < frequencies[0]; i++) {
array[index++] = 'A';
}
for (int i = 0; i < frequencies[1]; i++) {
array[index++] = 'B';
}
for (int i = 0; i < frequencies[2]; i++) {
array[index++] = 'C';
}
}
Is the requirement "keep it O(n)", or "keep one loop" ?
Adding a second (non-nested) loop wouldn't change the O(n) quality. then you could do it in two steps: first push all the 'A's to the start and a second one to push all the 'C's to the end.