Java Program Printing Names

In Original And Reverse

Page 1 of 1

1 Replies - 16336 Views - Last Post: 01 November 2008 - 12:14 PM Rate Topic: -----

#1 taylorxterrorx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-November 08

Java Program Printing Names

Posted 01 November 2008 - 11:58 AM

I have to ask a user for a number of names, they give me the names, and I list them in reverse and original. However, she wants them side by side, not in a list.

Here is my code:

import java.util.*;
public class ReverseNames {
public static void main(String[] args) {

int count = 0;
int index = 0;
Scanner scan = new Scanner (System.in);
String prompt = ("Enter next name: ");
System.out.print("This program will ask you to enter some names. How many do you" +
" have?");
int amount = scan.nextInt();
String[] name = new String[amount];
String[] reverse = new String[amount];
System.out.print("You entered " +amount+ " as the size of your name list.");
System.out.println(" ");


for (index = 0; index < amount; index++)
{
System.out.print("Enter next name: ");
name[index] = scan.next();
}

System.out.println(" ");
System.out.println("The names in reverse and original order: ");
System.out.println(" ");


for (index = name.length - 1; index >= 0; index--)
{

}

System.out.println(" ");

for (String names1 : name)
{
System.out.println(names1+" ");
}


}

}

/**
This program will ask you to enter some names. How many do you have? 3
You entered 3 as the size of your name list.
Enter next name:
Taylor
Enter next name:
Zack
Enter next name:
Sally
The names in reverse and original order:
Sally Zack Taylor
Taylor Zack Sally
*/



What do I need to do to have it like this:

Zack Taylor
Sally Sally
Taylor Zack

Is This A Good Question/Topic? 0
  • +

Replies To: Java Program Printing Names

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: Java Program Printing Names

Posted 01 November 2008 - 12:14 PM

Well from your example, she doesn't want them in reverse letter order, just wants them to appear in reverse order from what they were entered. That will be a matter of entering them into your array "name" and loop forward or backwards through it.

import java.util.*;
public class ReverseNames {
	public static void main(String[] args) {

		int count = 0;
		int index = 0;
		Scanner scan = new Scanner (System.in);
		
		System.out.print("This program will ask you to enter some names. How many do you have?");
		int amount = scan.nextInt();
		
		// One array to hold all the names
		String[] name = new String[amount];

		System.out.print("You entered " +amount+ " as the size of your name list.");
		System.out.println(" ");

		// Ask for all the names
		for (index = 0; index < amount; index++)
		{
			System.out.print("Enter next name: ");
			name[index] = scan.next();
		}

		System.out.println(" ");
		System.out.println("The names in reverse and original order: ");
		System.out.println(" ");

		// Loop through the name array from the end to the start (reverse loop)
		for (index = name.length - 1; index >= 0; index--)
		{
			System.out.print(name[index] + " ");
		}

		System.out.println();

		// Here is a for each loop you made to loop from the start to the end. 
		// You could have also used a for loop like above starting at 0 and going to name.length - 1
		for (String names1 : name)
		{
			System.out.print(names1 + " ");
		}
	}
}



Read through the in-code comments to see what is going on here. The first for loop starts at the end and loops backwards through the array. Then the for each loop you created second goes normal direction.

Notice a few things.... 1) One array for the names. Reverse is not needed. 2) We use print, not println and this will keep the names on the same line. Lastly 3) I removed the first prompt since it is not needed because you have one in the loop.

This program you see above will print your input as you requested... backwards on one line and forwards on the next line.

Enjoy!

"At DIC we be forwards and backwards.... or is that backwards and forwards code ninjas? Oh well, some say we go both ways. " :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1