hi i made a program that counts the elements in an array and i done it already. Now i want to display the result in a textView.. I want to display it this way...
1 appeared 2times
2 appeared 1times
3 appeared 1times
6 appeared 1times
this is my code..
The last element only displays in the textView..
please help me..Thanks
String []values = ( input.getText().toString().split(","));
Arrays.sort(values);
int c=1,i=0,range=4;
while(i<values.length-1){
while(values[i]==values[i+1]){
c++;
i++;
}
jLabel7.setText(values[i] + " appeared " + c + " times");
c=1;
i++;
if(i==values.length-1)
jLabel7.setText(values[i] + " appeared " + c + " times");
}
Try this:
jLabel7.setText(jLabel7.getText() + "\n" + values[i] + " appeared " + c + " times");
Att:
If u are using swing componentes, is not a TextView but JLabel, or u are working for Android?
update your code to:
String []values = ( input.getText().toString().split(","));
Arrays.sort(values);
int c=1,i=0,range=4;
while(i<values.length-1){
while(values[i]==values[i+1]){
c++;
i++;
}
jLabel7.setText(jLabel7.getText() + "\n" + values[i] + " appeared " + c + " times");
c=1;
i++;
if(i==values.length-1)
jLabel7.setText(jLabel7.getText() + "\n" + values[i] + " appeared " + c + " times");
}
append all the values into a single String object and then use the setText method to display the string
Related
I have a working code but my output doesn't count up.
Here is the code I am working with:
for(Course course : courses) {
for(int i=0;i<1;i++) {
System.out.println("[" + (i+1) + "]" + course.getCode() + "(" + course.getCreditHour() + ")");
}
}
System.out.print("Enter your choice : ");
I need the (i+1) to count from one to 7.
Here is a copy of the output I currently get:
Please type the number inside the [] to register for a course
The number inside the () is the credit hours for the course
[1]IT1006(6)
[1]IT4782(3)
[1]IT4789(3)
[1]IT4079(6)
[1]IT2230(3)
[1]IT3345(3)
[1]IT2249(6)
Enter your choice :
I need the numbers inside the square brackets to count from 1 to 7.
This is for an academic assignment.
Your inner loop isn't doing anything. There's no point in using a loop if you've hard coded it to just run once.
I'd get rid of your outer loop and just index courses directly:
for(int i = 0; i < courses.size(); i++){
Course course = courses.get(i);
System.out.println("[" + (i+1) + "]" + course.getCode() + "(" + course.getCreditHour() + ")");
}
for(Course course : courses) means : for each course so i is reinitialised you whant a variable that will be increment on each iteration so the variable must be declared outside the block. you can write some thing like this :
int i = 1;
for(Course course : courses) {
System.out.println("[" + (i++) + "]" + course.getCode() + "(" + course.getCreditHour() + ")");
}
System.out.print("Enter your choice : ");
the method of #Carcigenicate will work too but can handle so performance issue if you use linked structure as linkedlist tthis will become for an Array :
for (int i = 0 ; i < courses.lenght ; i++){
System.out.println("[" + i + "]" + courses[i].getCode() + "(" + courses[i].getCreditHour() + ")");
}
System.out.print("Enter your choice : ");
and on collections :
for (int i = 0 ; i < courses.getSize(); i++){
System.out.println("[" + i + "]" + courses.get(i).getCode() + "(" + courses.get(i).getCreditHour() + ")");
}
System.out.print("Enter your choice : ");
I'm making a random creature generator, its going all nice and dandy, however when it comes to printing the results, it prints the same result 5 times. I tried some different things like using println() multiple times and do while loops, however every time I run the file I just get a bunch of the same results. "a b c d e" are strings that generate the creature
int x = 1;
do {
System.out.println(x +" " +a +" " +b +" " +c +" " +d +" " +e);
x++;
} while (x<=5);
The reason why you're getting the same answer 5 times is because your do-while loop runs 5 times without changing the 'creatures' .
System.out.println(a +" "+ b + " " + c + " " + d + " " +e);
If you remove the do-while loop you'll get the same answer only once however just in case i misunderstood your question i made a small demo of a simple way in which to get multiple random results with a for-loop,a String-array and the Random class
String[] creatures = {"Dog", "Cat", "Fish", "Monkey", "Horse"};
Random r = new Random();
for (int i = 0; i < 5; i++) {
String creature1 = creatures[r.nextInt(creatures.length)];
String creature2 = creatures[r.nextInt(creatures.length)];
String creature3 = creatures[r.nextInt(creatures.length)];
String creature4 = creatures[r.nextInt(creatures.length)];
String creature5 = creatures[r.nextInt(creatures.length)];
System.out.println(creature1 + " " + creature2 + " " + creature3
+ " " + creature4 + " " + creature5);
}
I need the currentStockLevel for another void Method in java, is there any possibility to get it?
I think no, because of void right?
public void receive (int currentStock)
{
String outLine;
if (currentStockLevel > 0)
outLine = productCode;
{
outLine = ". Current Stock: " + currentStockLevel;
outLine += " Current Stock changed from " + currentStockLevel;
currentStockLevel += currentStock;
outLine += " to " + currentStockLevel;
int storeCost = wholeSalePrice * currentStockLevel;
System.out.println (productCode + ":" + " Received " + currentStockLevel + "." + " Store Cost " + "$" + storeCost + "." + " New stock level: " + currentStockLevel);
}
I am making my program to throw a die (as in dices) for a school assignment in Java SE. The user can place a character as standard input, so the character the user picks will represent the eyes of the die. Sometimes when I print the result, it shows a completely different character.
package ThrowADie;
import java.util.Scanner;
public class ThrowADie {
public static void main(String[] args) {
//Ask user for the char in which the dices eyes should be printed in.
System.out.print("Which character should I use for the eye: ");
//Allow user to place input in the eye variable
Scanner input = new Scanner(System.in); //Make the stdinput object
char eye = input.next().charAt(0);
//Time to throw the die. Place result in dieResult
int dieResult = throwDie();
//Reveal of the result
printDieResult(dieResult, eye);
}
/*
* Method name: throwDie()
* Purpose: Picks a number from 1 to 6 randomly, like a die does
* Parameters: N/A
* Returns: Integer number from 1 to 6
*/
public static int throwDie(){
int range = (6 - 1) + 1;
return (int)(Math.random() * range) + 1;
}
/*
* Method name: printDieResult()
* Purpose: Generate result of the die throw in ASCII art
* Parameters: numberOfEyes, typeOfEyes
* Returns: N/A
*/
public static void printDieResult(int numberOfEyes, char typeOfEyes){
if (numberOfEyes == 1){
//Print art
System.out.println(
" " + " " + " \n"
+ " " + typeOfEyes + " \n"
+ " " + " " + " ");
} else if (numberOfEyes == 2){
//Print art
System.out.println(
typeOfEyes + " " + " \n"
+ " " + " " + " \n"
+ " " + " " + typeOfEyes);
} else if (numberOfEyes == 3){
//Print art
System.out.println(
typeOfEyes + " " + " \n"
+ " " + typeOfEyes + " \n"
+ " " + " " + typeOfEyes);
} else if (numberOfEyes == 4){
//Print art
System.out.println(
typeOfEyes + " " + typeOfEyes + "\n"
+ " " + " " + " \n"
+ typeOfEyes + " " + typeOfEyes);
} else if (numberOfEyes == 5){
//Print art
System.out.println(
typeOfEyes + " " + typeOfEyes + "\n"
+ " " + typeOfEyes + " \n"
+ typeOfEyes + " " + typeOfEyes);
} else {
//Print art
//Accidentally written down 9 eye representation
System.out.println(
typeOfEyes + typeOfEyes + typeOfEyes + "\n"
+ typeOfEyes + typeOfEyes + typeOfEyes + "\n"
+ typeOfEyes + typeOfEyes + typeOfEyes);
}
}
}
Output
This program will generate proper results. But occasionally the char that has been input, that represent the eye of the die, transforms in to a number.
In the case below, the program should print 9 '#' characters. Instead it prints 192 on the first row. (I know dices have 6 eyes but I bumped into this strange output while accidentally printing 9 eyes)
run:
Which character should I use for the eyes: #
192
###
###
I can not find the cause of this, can anyone see what I have done wrong here?
Character arithmetic!
Consult this table. # is character 64
typeOfEyes + typeOfEyes + typeOfEyes + "\n"
This first line is actually adding up the values of the characters (64 + 64 + 64) = 192, then addending that with a newline, so we get 192\n.
The compiler is choosing to add those up rather than create a String of characters. The easy way to solve this is to preface that with an empty string in front: "" + typeOfEyes...
Basically, the compiler is "dumb." When we add integers to Strings, "foo" + 123 the compiler can interpret that as foo123 just fine because it recognizes the first element as a String. However, we've defined a char which is a numeric type representing a character. So the compiler does math with it. Even though we shouldn't be. Adding the String literal tells it we actually want text.
int test = (int) (typeOfEyes + typeOfEyes + typeOfEyes);
System.out.println("\n" + test + "\n"
+ typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
+ typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
+ typeOfEyes + "" + typeOfEyes + "" + typeOfEyes);
Which character should I use for the eye: #
192
###
###
###
My assignment calls for the line number to be display with the output. The professor suggested I do it with a counter and as seeing Java doesn't have an easy way to print out the current line number, I just created a counter as suggested. The below code is as follows:
//Count Increment
for (count = 1; count<= 5; count++)
{
}
//Display information
System.out.println(count + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
System.out.println(count + "." + " " + "Total Rooms:"+ " " + numofRooms);
System.out.println(count + "." + " " + "Total Area:"+ " " + totalSqFt + " sq.ft");
System.out.println(count + "." + " " + "The price per Sq. Ft is " + "$" + priceperSqFt);
System.out.println(count + "." + " " + "The estimated property value is "+ "$" + estimatedPropertyvalue);
However, the output starts the line counter at six as demonstrated here:
6. Street: park avenue #44
6. Total Rooms: 5
6. Total Area: 2500.0 sq.ft
6. The price per Sq. Ft is $120.4
6. The estimated property value is $301000.0
Removing the brackets doesn't help either. How can I get the line count to correctly state 1,2,3,4,5?
Please ask for clarification if needed!! Thanks.
Your prints are outside of the for loop. Your for loop ends when the counter is "6" which is when it exits the for loop. This variable doesn't change so the current value is "6",that is why it always prints "6" below on your code. If you want to print the line number for each instruction you could do something like this:
count = 0;
System.out.println(++count + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
"++count", you increment the variable the moment you write a line, in the first case it should print 1 then 2 etc. Hope this helped :)
The loop is not required cause you are only counting the lines one time each. If you put those lines in a loop that goes from 0 to 5 you will be counting each line 5 times. Since you only need to count each line ONE time you dont need the loop and just the simple increment I previously mentioned. Hope this clears out why the loop is not required
I assume that you have somewhere above this a line defining count:
int count;
So after the for loop, you've incremented count to 6 and then started printing with count left at the last incremented value from the for loop.
So, remove the for loop and just pre-increment the count variable for each line of ouput.
int count = 0;
//Display information
System.out.println( (++count) + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
...
class Print{
static int lineno = 0;
private int static getLineNo(){
lineno = lineno + 1;
return lineno;
}
}
//Display information
System.out.println(Print.getLineNo() + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
System.out.println(Print.getLineNo() + "." + " " + "Total Rooms:"+ " " + numofRooms);
System.out.println(Print.getLineNo() + "." + " " + "Total Area:"+ " " + totalSqFt + " sq.ft");
System.out.println(Print.getLineNo() + "." + " " + "The price per Sq. Ft is " + "$" + priceperSqFt);
System.out.println(Print.getLineNo() + "." + " " + "The estimated property value is "+ "$" + estimatedPropertyv