Why is null appearing in my output? - Java - java

I am writing this program and all of a sudden null appears in the output:
1-10 |null**********
11-20 |null**********
21-30 |null
31-40 |null
41-50 |null
it should be like this:
1-10 |**********
11-20 |**********
21-30 |
31-40 |
41-50 |
This is my code:
import java.util.Arrays;
public class Ex4Method {
public void Average(int[] a) {
int total = 0;
for (int i = 0; i < a.length; i++) {
total = total + a[i];
}
int average = total / a.length;
System.out.println(" ");
System.out.println("Average: " + average);
}
public void MaxAndRange(int[] b) {
int min = b[0];
int max = b[0];
for (int i = 0; i <= 19; i++) {
//System.out.println(i);
if (b[i] < min) {
min = b[i];
}
if (b[i] > max) {
max = b[i];
}
}
int range = max - min;
System.out.println("Maximum Number: " + max);
System.out.println("Range: " + range);
}
public void Median(int[] c) {
Arrays.sort(c);
double median = 0;
System.out.println("Median: " + median);
}
public void findMedian(int a[]) {
// First we sort the array
Arrays.sort(a);
double median;
if (a.length % 2 == 0) {
median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
} else {
median = (double) a[a.length / 2];
}
System.out.println("Median: " + median);
}
public void Mode(int[] d) {
int maxValue = 0, maxCount = 0;
for (int i = 0; i < d.length; i++) {
int count = 0;
for (int j = 0; j < d.length; j++) {
if (d[j] == d[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = d[i];
}
}
System.out.println("Mode: " + maxValue);
}
public void Histogram(int[] f) {
String[] asterisk = new String[6];
System.out.println("Histogram: ");
System.out.println(" ");
for (int i = 0; i <= f.length - 1; i++) {
if (f[i] >= 1 && f[i] <= 10) {
asterisk[1] += "*";
}
if (f[i] >= 11 && f[i] <= 20) {
asterisk[2] += "*";
}
if (f[i] >= 21 && f[i] <= 30) {
asterisk[3] += "*";
}
if (f[i] >= 31 && f[i] <= 40) {
asterisk[4] += "*";
}
if (f[i] >= 41 && f[i] <= 50) {
asterisk[5] += "*";
}
}
System.out.println(" 1-10 |" + asterisk[1]);
System.out.println("11-20 |" + asterisk[2]);
System.out.println("21-30 |" + asterisk[3]);
System.out.println("31-40 |" + asterisk[4]);
System.out.println("41-50 |" + asterisk[5]);
}
}
MAIN CLASS-
import TurtleGraphics.KeyboardReader;
public class ArrayEx4 {
public static void main(String[] args) {
KeyboardReader reader = new KeyboardReader();
Ex4Method object = new Ex4Method();
int[] nums = new int[20];
int i = 0;
System.out.print("Enter a number (1-50): ");
nums[i] = reader.readInt();
while (nums[i] >= 1 && nums[i] <= 50 && i < 19) {
i++;
System.out.print("Enter a number (1-50): ");
nums[i] = reader.readInt();
//occurences[nums[i]]++;
}
//for(int x=0;x<=4;x++) {
//System.out.println(occurences[x]);
//}
object.Average(nums);
object.MaxAndRange(nums);
object.findMedian(nums);
object.Mode(nums);
object.Histogram(nums);
}
}
Any suggestions? Thanks

This is the code right here:
public void Histogram(int[] f) {
String[] asterisk = new String[6];
System.out.println("Histogram: ");
System.out.println(" ");
for (int i = 0; i <= f.length - 1; i++) {
if (f[i] >= 1 && f[i] <= 10) {
asterisk[1] += "*";
}
if (f[i] >= 11 && f[i] <= 20) {
asterisk[2] += "*";
}
if (f[i] >= 21 && f[i] <= 30) {
asterisk[3] += "*";
}
if (f[i] >= 31 && f[i] <= 40) {
asterisk[4] += "*";
}
if (f[i] >= 41 && f[i] <= 50) {
asterisk[5] += "*";
}
}
System.out.println(" 1-10 |" + asterisk[1]);
System.out.println("11-20 |" + asterisk[2]);
System.out.println("21-30 |" + asterisk[3]);
System.out.println("31-40 |" + asterisk[4]);
System.out.println("41-50 |" + asterisk[5]);
}
More specifically, this part:
asterisk[i] += "*";
In Java, + operator for strings means concatenation, and both operands are converted to strings beforehand.
At the very beginning, new String[6] is filled with default values for String type, which, for any type inherited from java.lang.Object is a null value. When you concatenate using operators, the very first pass over any slot in the array will always encounter a null value.
And
String.valueOf(null) + "*"
will output "null*"

Related

Reverse and sum the number occurrence in string - java

I would like to write a function that will be reverse a number and then sum it up.
For example, the input string is
We have 55 guests in room 38
So the expected output should be
83 + 55 = 138
I have face a question is that I can't read the last number
example:
input string is '8 people'
output is 0
Here's the code I've written :
int total = 0;
String num = "";
String a = input.nextLine();
for (int i = a.length() - 1; i > 0; i--) {
if (Character.isDigit(a.charAt(i))) {
num += a.charAt(i);
if (!Character.isDigit(a.charAt(i - 1))) {
total += Integer.valueOf(num);
num = "";
}
}
}
All you really need to do is :
String input = "We have 55 guests in room 38";
int sum = 0;
String[] split = input.split(" "); // split based on space
for (int i = 0; i < split.length; i++) {
if (split[i].matches("[0-9]+")) {
sum = sum + Integer.parseInt(new StringBuffer(split[i]).reverse().toString());
}
}
System.out.println(sum);
Explanation:
Here we use regex to check if the String split contains only
digits.
Now we reverse the String and then parse it to an int before
summing.
Try this and works for any input in the form "We have x guests in room y". For your program however, instead if the for loop > 0 do > -1 I think:
Scanner scan = new Scanner(System.in);
scan.next(); scan.next();
String numberOne = "" + scan.nextInt();
scan.next(); scan.next(); scan.next();
String numberTwo = "" + scan.nextInt();
// String numberOne = "" + scan.nextInt(), numberTwo = "" + scan.nextInt();
String numberOneReversed = "", numberTwoReversed = "";
for(int k = numberOne.length() - 1; k > -1; k--)
numberOneReversed += numberOne.charAt(k);
for(int k = numberTwo.length() - 1; k > -1; k--)
numberTwoReversed += numberTwo.charAt(k);
int sum = Integer.parseInt(numberOneReversed) + Integer.parseInt(numberTwoReversed);
System.out.println("" + numberOneReversed + " + " + numberTwoReversed + " = " + sum);
scan.close();
Note for your program as defined in your question:
for (int i = a.length() - 1; i > -1; i--) {
instead of
for (int i = a.length() - 1; i > 0; i--) {
and
if (i != 0 && !Character.isDigit(a.charAt(i - 1))) {
instead of
for (int i = a.length() - 1; i > 0; i--) {
Will return the sum correctly.
Okay here is what I created using BigIntegers instead of ints:
public static BigInteger nameOfFunctionGoesHere(String input) {
BigInteger total = new BigInteger(new byte[] {0});
int i = 0;
while (i < input.length()) {
if (Character.isDigit(input.charAt(i))) {
int j = i + 1;
while (!(j >= input.length()) && Character.isDigit(input.charAt(j))) {
j++;
}
String num = input.substring(i, j);
char[] flipped = new char[num.length()];
for (int n = num.length() - 1; n >= 0; n--) {
flipped[n] = num.charAt(num.length() - (n + 1));
}
total = total.add(new BigInteger(new String(flipped)));
i = j;
} else {
i++;
}
}
return total;
}
You can of course use ints as well like this:
public static int nameOfFunctionGoesHere(String input) {
int total = 0;
int i = 0;
while (i < input.length()) {
if (Character.isDigit(input.charAt(i))) {
int j = i + 1;
while (!(j >= input.length()) && Character.isDigit(input.charAt(j))) {
j++;
}
String num = input.substring(i, j);
char[] flipped = new char[num.length()];
for (int n = num.length() - 1; n >= 0; n--) {
flipped[n] = num.charAt(num.length() - (n + 1));
}
total = total + Integer.parseInt(new String(flipped));
i = j;
} else {
i++;
}
}
return total;
}
With longs too:
public static long nameOfFunctionGoesHere(String input) {
long total = 0;
int i = 0;
while (i < input.length()) {
if (Character.isDigit(input.charAt(i))) {
int j = i + 1;
while (!(j >= input.length()) && Character.isDigit(input.charAt(j))) {
j++;
}
String num = input.substring(i, j);
char[] flipped = new char[num.length()];
for (int n = num.length() - 1; n >= 0; n--) {
flipped[n] = num.charAt(num.length() - (n + 1));
}
total = total + Long.parseLong(new String(flipped));
i = j;
} else {
i++;
}
}
return total;
}

Loop structure for histogram

I'm trying to write a Java program that generates a histogram of asterisks for each occurrence of a value in an array.
If the elements are, respectively, 0,1,2,3,4,5,6,7,8,9 the output should have an asterisk for each occurrence. For example,
0:*
1:*
2:*
3:*
4:*
5:*
6:*
7:*
8:*
9:*
However, my output is
0:**********
1:
2:
3:
4:
5:
6:
7:
8:
9:
The following code below is my own.
public static void drawHistogram(double[] array) {
String count = "";
for (int i = 0; i < array.length; i++) {
if (array[i] >= 0 && array[i] < 1) {
count += "*";
} else if (array[i] >= 1 && array[i] < 2) {
count += "*";
} else if (array[i] >= 2 && array[i] < 3) {
count += "*";
} else if (array[i] >= 3 && array[i] < 4) {
count += "*";
} else if (array[i] >= 4 && array[i] < 5) {
count += "*";
} else if (array[i] >= 5 && array[i] < 6) {
count += "*";
} else if (array[i] >= 6 && array[i] < 7) {
count += "*";
} else if (array[i] >= 2 && array[i] < 8) {
count += "*";
} else if (array[i] >= 2 && array[i] < 9) {
count += "*";
} else if (array[i] >= 9 && array[i] < 10) {
count += "*";
} else if (array[i] >= 10 && array[i] < 11) {
count += "*";
}
}
for (int j = 0; j <= 10; j++) {
System.out.print(j + count);
count = "";
System.out.println();
}
}
How can I fix this issue?
This solution uses (int) Math.floor(array[i]) to choose the bracket into which to put the double value, thus getting rid of the multiple if-then-else statements. I've also used StringBuilder instead of String to make the repeated concatenation of asterisks a little more efficient.
public static void drawHistogram(double[] array) {
StringBuilder histoGram[] = new StringBuilder[11];
for (int i = 0; i < histoGram.length; i++) {
histoGram[i] = new StringBuilder();
}
for (int i = 0; i < array.length; i++) {
int bracket = (int) Math.floor(array[i]);
if (bracket >= 0 && bracket < histoGram.length) {
histoGram[bracket].append("*");
}
}
for (int j = 0; j < 11; j++) {
System.out.format("%02d: %s\n", j, histoGram[j].toString());
}
}
Test main method:
public static void main(String args[]) {
double[] testValues = new double[100];
for (int i = 0; i < 100; i++) {
testValues[i] = Math.random() * 11.0;
}
drawHistogram(testValues);
}
Sample output:
00: *******
01: ********
02: ***********
03: ************
04: ********
05: **********
06: *******
07: ********
08: **********
09: ************
10: *******
public static void drawHistogram(double[] array) {
String count[] = new String[array.length];
for (int i = 0; i < array.length; i++) {
if (array[i] >= 0 && array[i] < 1) {
count[0] = "*";
} else if (array[i] >= 1 && array[i] < 2) {
count[1] = "*";
} else if (array[i] >= 2 && array[i] < 3) {
count[2] = "*";
} else if (array[i] >= 3 && array[i] < 4) {
count[3] = "*";
} else if (array[i] >= 4 && array[i] < 5) {
count[4] = "*";
} else if (array[i] >= 5 && array[i] < 6) {
count[5] = "*";
} else if (array[i] >= 6 && array[i] < 7) {
count[6] = "*";
} else if (array[i] >= 2 && array[i] < 8) {
count[7] = "*";
} else if (array[i] >= 2 && array[i] < 9) {
count[8] = "*";
} else if (array[i] >= 9 && array[i] < 10) {
count[9] = "*";
} else if (array[i] >= 10 && array[i] < 11) {
count[10] = "*";
}
}
for (int j = 0; j <= 10; j++) {
System.out.print(j + count[j]);
System.out.println();
}
}
It seems that you are using only a single variable to count up the occurrences of numbers in this method. This results in you program showing that 0 has nine occurrences and the rest of the numbers have 0 occurrences. I agree with the user David Choweller in the comments, who suggested that you could use an array to solve this problem. However, another solution might be a HashMap, where you store the number as the key, and the string that you want to print out as the value. Then, you can use the loop through the numbers at the end as you do currently, and print out the values associated with them.
You can use a Java Hasmap :
int myArray[] = new int[]{1, 2, 1, 3, 3, 1, 2, 1, 5, 1};
public static void main (String args[]) {
HashMap<Integer, String> hash = new HashMap<>();
hash.put(5, "");
hash.put(4, "");
hash.put(3, "");
hash.put(2, "");
hash.put(1, "");
for (int i = 0; i < myArray.length; i++){
hash.put(new Integer(myArray[i]), hash.get(myArray[i])+"*");
}
for(Integer key: hash.keySet()){
System.out.println(key+": "+ hash.get(key));
}
}

Reading numbers from a txt file and comparing them

With this program I want to read and compare the numbers that I'm given in a text file and print out
"buy" whenever the number goes up three consecutive time and "sell" whenever the number goes down three consecutive times.
The problem with my program is that it only reads 13 of the 15 lines of the "numbers.txt" and the buy-sell is at wrong places.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Practise {
public static void main(String[] args) throws IOException {
int num = 0;
int up = 0;
int down = 0;
int same = 0;
FileInputStream file = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextDouble()) {
Double[] array = new Double[15];
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextDouble();
}
for (int a = 0; a < array.length && a + 1 < array.length - 1; a++) {
num++;
System.out.print(num + " " + (array[a]));
if (array[a] < array[a + 1]) {
up++;
} else if (array[a] > array[a + 1]) {
down++;
} else {
same++;
}
if ((up >= 3 && (down > 1 || same >= 1))) {
System.out.print(" " + "sell");
up = 0;
same = 0;
} else if ((down >= 3 && (up > 1 || same >= 1))) {
System.out.print(" " + "buy");
down = 0;
same = 0;
}
System.out.println();
}
}
}
}
The file numbers.txt:
26.375
25.500
25.125
25.000
25.250
27.125
28.250
26.000
25.500
25.000
25.125
25.250
26.375
25.500
25.500
Expected output:
1 26.375
2 25.500
3 25.125
4 25.000
5 25.250 buy
6 27.125
7 28.250
8 26.000 sell
9 25.500
10 25.000
11 25.125 buy
12 25.250
13 26.375
14 25.500 sell
15 25.500
You use a + 1 < array.length - 1, which can be transformed to a < array.length - 2. I guess you understand that the second operand in && limits your iteration to 13.
a < array.length && a < array.length - 2 = a < 15 && a < 13 = a < 13
I did some minor changes on your code for it to work; you could refactor it a lot, but I stick to your style, so you can better understand the logic.
int num = 1;
//print the 1st element
System.out.println(num + " " + array[0]);
for (int a = 1; a < array.length; a++) {
num++;
System.out.print(num + " " + (array[a]));
//plz note that we check with the before, not after
if (array[a] < array[a - 1]) {
down++;
} else if (array[a] > array[a - 1]) {
up++;
} else {
same++;
}
//changed down > to down ==
if ((up >= 3 && (down == 1 || same >= 1))) {
System.out.print(" " + "sell");
up = 0;
same = 0;
}
//changed up > to up ==
else if ((down >= 3 && (up == 1 || same >= 1))) {
System.out.print(" " + "buy");
down = 0;
same = 0;
}
System.out.println();
}
Answering to OPs comment: if you want to support more than 15 records you can keep adding to a list:
List<Double> list = new ArrayList<>();
while (scanner.hasNextDouble()) {
list.add(scanner.nextDouble());
}
//if you want to work with array
Double[] array = new Double[list.size()];
array = list.toArray(array);
Here's a working example (had to change array to list)
int num = 0, up = 0, down = 0, same = 0;
FileInputStream file = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(file);
List<Double> list = new ArrayList<>();
while (scanner.hasNextDouble()) {
list.add(scanner.nextDouble());
}
int position = 0;
while (position + 2 < list.size()) {
Double[] nums = new Double[3];
System.out.println(nums[0] = list.get(position));
System.out.println(nums[1] = list.get(position + 1));
System.out.println(nums[2] = list.get(position + 2));
if (nums[1] > nums[0] && nums[2] > nums[1]) {
System.out.println("buy");
up++;
} else if (nums[1] < nums[0] && nums[2] < nums[1]) {
System.out.println("sell");
down++;
} else {
same++;
}
position += 2;
}
System.out.println("Ups total: " + up);
System.out.println("Downs total: " + down);
System.out.println("Same total: " + same);

How to print my methods on the main method to display the output?

import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
System.out.println("best: ");
int best1 = best(grades);
System.out.print(best1);
// Print Grade
System.out.println("Grade: ");
char [] grade = (char[]) best(grades);
for (char i = 0; i < grade.length; i++) {
String output= " ";
output += "Student " + i + " score is " +
grades[i] + " and grade is " + grade + "\n";
}
}
private static char gradeLetter(int[] grades) {
char grade = 0;
for (int i = 0; i < grades.length; i++) {
if (grades[i] >= best(grades) - 10)
grade = 'A';
else if (grades[i] >= best(grades) - 20)
grade = 'B';
else if (grades[i] >= best(grades) - 30)
grade = 'C';
else if (grades[i] >= best(grades) - 40)
grade = 'D';
else
grade = 'F';
}
return grade;
}
public static int best(int[] grades) {
System.out.println("The best scores is: ");
int best = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
I am trying to show the output like this
Student 1 score is 40 and grade is C
Student 2 score is 55 and grade is B
Student 3 score is 70 and grade is A
Student 4 score is 58 and grade is B
But I am having problems with printing the gradeLetter method.
Perhaps this is what you are trying to do :
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
int best = best(grades);
System.out.println("The best scores is: " + best);
// Print Grade
System.out.println("Grade: ");
for (int i = 1; i <= grades.length; i++) {
char grade = gradeLetter(grades[i-1], grades);
String output = " ";
output += "Student " + i + " score is " + grades[i-1] + " and grade is " + grade + "\n";
System.out.println(output);
}
}
private static char gradeLetter(int grade, int[] grades) {
char charGrade = 0;
if (grade >= best(grades) - 10)
charGrade = 'A';
else if (grade >= best(grades) - 20)
charGrade = 'B';
else if (grade >= best(grades) - 30)
charGrade = 'C';
else if (grade >= best(grades) - 40)
charGrade = 'D';
else
charGrade = 'F';
return charGrade;
}
public static int best(int[] grades) {
int best = grades[0];
for (int i = 1; i < grades.length; i++) { //will save a compare operation
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
char [] grade = (char[]) best(grades);
Its a invalid part in your code. You can not convert an int to char[] array.
Just wanted to show you some other way of looking at it...
import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
// Search the array for min, max and average
int[] grades = {40, 55, 70, 58};
gradeStats(grades);
}
static void gradeStats(int[] array) {
final int A = 90;
final int B = 80;
final int C = 70;
final int D = 60;
int minimumValue = 100;
int maximumValue = 0;
double gradeSum = 0;
// Get max and min grades
for (int i = 0; i < array.length; i++) {
if (array[i] < minimumValue) {
minimumValue = array[i];
}
if (array[i] > maximumValue) {
maximumValue = array[i];
}
}
// Get sum of grades
for (int i = 0; i < array.length; i++) {
gradeSum = gradeSum + array[i];
}
for (int i = 0; i < array.length; i++) {
if (array[i] >= A) {
System.out.println("Grade \"" + array[i] + "\" is an A.");
} else if (array[i] >= B) {
System.out.println("Grade \"" + array[i] + "\" is an B.");
} else if (array[i] >= C) {
System.out.println("Grade \"" + array[i] + "\" is an C.");
} else if (array[i] >= D) {
System.out.println("Grade \"" + array[i] + "\" is an D.");
} else {
System.out.println("Grade \"" + array[i] + "\" is an F.");
}
}
System.out.println("The lowest grade is a " + minimumValue + ".");
System.out.println("The highest grade is a " + maximumValue + ".");
System.out.printf("The sum is %d.\n", (int) gradeSum);
System.out.println("The average is " + gradeSum / array.length + ".");
}
}

How can I output several numbers per line in java

Hi I am new in programming,so please don't laugh from my stupid question.
I wrote program which ask user for input a number than program should output all the numbers from 0 to that entered number(doesn't matter if it is positive or negative).
I have tried 3 different versions
Here is my code:
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
int num = PutiL.validNum(min, max, "number");
//this my utility methode which check if number is in range and if it is not a double or letter
int i = 0, z;
int y = 0;
//3rd version
while (i <= num) {
for (z = 0; z < 4; z++) {
System.out.print(i + " ");
i++;
}
System.out.println();
}
//2nd version
if (num > 0) {
for (i = 0; i <= num; y++) {
for (z = 0; z < 4; z++) {
System.out.print(i + ",");
i++;
}
System.out.println();
}
} else {
for (i = 0; i > num; y--) {
for (z = 0; z < 4; z++) {
System.out.print(i + ",");
i--;
}
System.out.println();
}
}
//1st version`enter code here`
if (num > 0) {
for (i = 0; i <= num; i++)
{
System.out.print(i + ",");
}
} else {
for (i = 0; i >= num; i--) {
System.out.print(i + ",");
}
}
System.out.println();
Problem is that code doesn't stop straight after number typed in by user. Will someone give me a hint what is wrong as I don't have any more ideas.
And here is the PutiL methode
public static int validNum(int min, int max, String words) {
int num;
do {
System.out.println("Please enter " + words);
while (!kb.hasNextInt()) {
System.out.println("Please re-enter ");
kb.nextLine();
}
num = kb.nextInt();
if (num < min || num > max) {
System.out.println("Not in range - re-enter\tproper range is "
+ min + " - " + max);
}
} while (num < min || num > max);
return num;
}
//2nd version
if (num > 0)
{
for (i = 0; i <= num; y++) <== THIS MAKE INFINITE too, OKAY.. ^^, change i to stop
{
for (z = 0; z < 4; z++)
{
System.out.print(i + ",");
i++;
}
System.out.println();
}
} else
{
for (i = 0; i > num; y--) <== THIS MAKE INFINITE LOOPS, OKAY.. ^^, it must i to stop
{
for (z = 0; z < 4; z++)
{
System.out.print(i + ",");
i--;
}
System.out.println();
}
}
don't forget to accepted the answer if it goes right.. ^^
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
if (number >= 0) {
for (int i = 0; i <= number; i++) {
System.out.println(i);
}
} else {
for (int i = 0; i >= number; i--) {
System.out.println(i);
}
}
}
Or a bit more concise and with duplicating the println statement...
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
int increment = number >= 0 ? 1 : -1;
for (int i = 0; i != number + increment; i += increment) {
System.out.println(i);
}
}
This will give the user a dialog and the value the user enters will count to zero.
First import:
import javax.swing.JOptionPane.*;
Then:
int user_choice = Integer.parseInt(
showInputDialog(null, "Please enter a number."));
if(user_choice > 0){
for(int temp = 0; temp <= user_choice; temp++){
System.out.println(temp);
}
}
else{
for(int temp = 0; temp >= user_choice; temp--){
System.out.println(temp);
}
}
"program should output all the numbers from 0 to that entered number"
and
"I want them print for example 4 in a one line and than skip to another line"
if(num >=0) {
for (z = 0; z <= num; z++)
{
System.out.print(z + " ");
if(z > 0 && z%4==0)
System.out.println();
}
}
else {
// similar loop for negatives
}
System.out.println();
Thank you all for help I solve it my self was really easy here is code I useit and now its work perfectly
for(i = 0; i <= num; i++)
{
System.out.print(i + " ");
y++;
if(y % 4 == 0)
{
System.out.println();
}
}
for(i = 0; i >= num; i--)
{
System.out.print(i + " ");
y++;
if(y % 4 == 0)
{
System.out.println();
}
}
But thank you all again for giving my ideas.

Categories