0

I am trying to possibly get this program to print out as.

Smith 1000 doe 1200 john 1400 bailey 900 potter 1600 

the program itself has to arrays I either need to find out a way of possibly combining the two 1d arrays or just a way to format it correctly so it prints out in the above way.

The Program:

import java.util.*; public class TwoArrays { static Scanner console = new Scanner(System.in); public static void main(String[]args){ String [] name = new String[5]; int [] vote = new int[5]; String lastname; int votecount; int i; for(i=0; i<name.length; i++){ System.out.println("Enter the last name of the candidate: "); lastname = console.next(); name[i]= lastname; System.out.println("Enter the number of votes the candidate got: "); votecount = console.nextInt(); vote[i] = votecount; } String printing = Print(name); int printing2 = Print2(vote); } public static String Print(String [] pname){ for (int i=0; i<pname.length; i++){ System.out.println(pname[i]+ " \n"); } return "nothing"; } public static int Print2(int [] pvote){ for (int i=0; i<pvote.length; i++){ System.out.println(pvote[i]+ " \n"); } return 0; } } 
1
  • 2
    Use a hash map where the name is the key, and the votes are the value Commented Dec 10, 2013 at 15:56

4 Answers 4

2

For that you need set reasonable spaces using System.out.printf. Here I put %-15s for left alignment. You can easily calculate it form pname sizes.

 public static void print(String[] pname, int[] pvote) { for (int i = 0; i < pname.length; i++) { System.out.printf("%-15s %d\n", pname[i], pvote[i]); } } 
Sign up to request clarification or add additional context in comments.

Comments

0
for (int i=0;i<pname.length;i++) { System.out.println(pname[i]+ " "+pvote[i]); } 

2 Comments

I would prefer to use a tab so the numbers align as requested in the example. Or print the name in a fixed-width field.
Yes, that would be tidier. I only used fixed width because that was what was in the existing code.
0

Add this as an instance variable:

HashMap<String, Integer> candidateMap = new HashMap<String, Integer>(); 

Then in your loop:

System.out.println("Enter the last name of the candidate: "); lastname = console.next(); System.out.println("Enter the number of votes the candidate got: "); votecount = console.nextInt(); candidateMap = candidateMap.put(lastname, votecount); 

Then your print method (credit to @Masud for proper print formatting):

public static void Print(){ for (String candidate : candidateMap.keySet()){ System.out.printf("%-15s %d\n", candidate, candidateMap.get(candidate)); } } 

2 Comments

Tab \t can't display properly for various word length.
@Masud you're right. hope you don't mind if I use what you have in your answer with proper credit.
0

"just a way to format it correctly so it prints out in the above way.":

 public static void Print(String [] pname, int [] votes){ for (int i=0; i<pname.length; i++){ System.out.printf("%-10s %5d\n", pname[i], votes[i]); } } 

Then obvious call it just once, with both arrays as arguments:

Print(name, vote); 

(with latest edit, you get some nice alignment. The name is left aligned in a field of 10, the number is right aligned in a field of width 5. You can add other characters in between (for example, a :) just by inserting it into the format string.)

3 Comments

I tried this it did not work correctly first off if it is a void the return can't be there and when I went to call 'Print(name, vote)' nothing ended up printing
Sorry - your comment was truncated. What is the error you are getting? I thought that return with nothing is valid as the end of a void function... And this looks remarkably similar to @Masud's code - is that not working for you either?
it was just the return that was throwing things off.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.