I have a code which takes name and number from user and save those in an arraylist as object.
I am using this enhanced for loop to printout all name and number which is stored in that arraylist ...
for(Objectclass p : Test) {
System.out.println("Name: " + p.getName() + " Number: " + p.getNumber());
}
it prints like Name: blah blah Number: blah blah
now i want to add counter number before Name and number like
1.Name: blah blah Number: blah blah
2.Name ... number
3.Name ... number
... how can i add that ? if i use another for loop inside this for loop to add counter number ... it prints again and again.
Make a counter variable declared outside of the enhanced for-loop.
int i = 0;
for(Objectclass p : Test) {
System.out.println(++i + ". Name: " + p.getName() + " Number: " + p.getNumber());
}
Or so, you don't have a useless variable after it, switch back to the old method.
for(int i = 0; i < Test.size();){
Objectclass p = Test.get(i++);
System.out.println(i + ". Name: " + p.getName() + " Number: " + p.getNumber());
}
This should work:
int i = 0;
for(Objectclass p : Test)
{
i++;
System.out.println(i + ". Name: " + p.getName() + " Number: " + p.getNumber());
}
Sadly, there is no way of extracting an iteration index from an enhanced for-loop.
you could possibly try -
for(Objectclass p : Test) {
System.out.println("Index: " + Test.indexOf(p+1) + "Name: " + p.getName() + " Number: " + p.getNumber());
}
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 : ");
When I run the program it shows me on the first values on the list which is the one with the [0] here is my array code:
KeyStroke[] clientsDetails = new KeyStroke[5];
clientsDetails[0] = new KeyStroke(9,"OX5BJM","Peter",2039489);
clientsDetails[1] = new KeyStroke(12,"OX1BOL","Kim",2434587);
clientsDetails[2] = new KeyStroke(67,"OX2VBN","Patrick",2233842);
clientsDetails[3] = new KeyStroke(34,"OX2XHB","Liam",2432340);
clientsDetails[4] = new KeyStroke(54,"OX3BUN","Bob",2234098);
And here is the code to support the array:
if(input.matches("S")){
System.out.println("Enter an array postion from 1 to 4 to show paitient's details");
number1 = enterNumber0.nextInt();
System.out.println("Name: " +clientsDetails[0].nameLable);
System.out.println("Age: " +clientsDetails[0].howOld);
System.out.println("Postcode: " +clientsDetails[0].postcode);
System.out.println("Phone Number: " + clientsDetails[0].cellPhoneNumber);
number2 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[1].nameLable);
System.out.println("Age: " +clientsDetails[1].howOld);
System.out.println("Postcode: " +clientsDetails[1].postcode);
System.out.println("Phone Number: " + clientsDetails[1].cellPhoneNumber);
number3 = enterNumber2.nextInt();
System.out.println("Name: " +clientsDetails[2].nameLable);
System.out.println("Age: " +clientsDetails[2].howOld);
System.out.println("Postcode: " +clientsDetails[2].postcode);
System.out.println("Phone Number: " + clientsDetails[2].cellPhoneNumber);
number4 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[3].nameLable);
System.out.println("Age: " +clientsDetails[3].howOld);
System.out.println("Postcode: " +clientsDetails[3].postcode);
System.out.println("Phone Number: " + clientsDetails[3].cellPhoneNumber);
}
Help will be greatly appreciated this is the last bit of my work that I'm stuck on. I have no teacher to advice this for me as to why I added a question on this forum I hope nobody gets offended.
Assuming enterNumber0 is a Scanner that is already created, we can do something like this:
// Populate the array
KeyStroke[] clientsDetails = new KeyStroke[]{
new KeyStroke(9,"OX5BJM","Peter",2039489),
new KeyStroke(12,"OX1BOL","Kim",2434587),
new KeyStroke(67,"OX2VBN","Patrick",2233842),
new KeyStroke(34,"OX2XHB","Liam",2432340),
new KeyStroke(54,"OX3BUN","Bob",2234098)
}
Not 100% sure what input does here, so I've kept this part. Also note that we fetch the ID from the user non-zero-indexed, so we have to subtract 1 to all the values. You also seem to have misunderstood how to use this value to return the correct client details. You use this value as the index in the array.
if (input.matches("S")) {
System.out.println("Enter an array postion from 1 to 5 to show paitient's details");
int number = enterNumber0.nextInt();
if (number >= 1 && number <= 5) {
System.out.println("Name: " +clientsDetails[number - 1].nameLable);
System.out.println("Age: " +clientsDetails[number - 1].howOld);
System.out.println("Postcode: " +clientsDetails[number - 1].postcode);
System.out.println("Phone Number: " + clientsDetails[number - 1].cellPhoneNumber);
}
}
You can use matches which can accept an integer from 1 to 5:
String number = enterNumber0.nextLine();
if(number.matches("[1-5]")){
int index = Integer.parseInt(input) - 1;
//use this index to get the value from your array
System.out.println("Name: " +clientsDetails[index ].nameLable);
System.out.println("Age: " +clientsDetails[index ].howOld);
System.out.println("Postcode: " +clientsDetails[index ].postcode);
System.out.println("Phone Number: " + clientsDetails[index ].cellPhoneNumber);
}
Note -1, because the array start from 0.
if (gpa < 2.0)
System.out.println("The student " +first +last + " is not graduating.");
This is my output statement. However the result I get is
The student BobPaul is not graduating.
How do I add a space between the +first and +last so that way my output statement looks like
The student Bob Paul is not graduating.
the correct answer is
System.out.println("The student " +first + " " + last + " is not graduating.");
It is simple. Just concatenate a string " " with empty space in between.
System.out.println("The student " +first + " " + last + " is not graduating.");
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
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