Results 1 to 20 of 20
  1. #1
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Exclamation Need help ASAP! Problem due tomorrow!

    JAVA programmers: your program name must be: prob02.class C programmers: your program name must be: prob02.exe
    Task Description
    The CodeWars judges and scorekeepers need help printing the team names above each column on our score sheet. But we want them to look nice, so they need to be printed vertically. All we have to use is a list of team names. Please write us a program that will take a list of team names, and print them vertically.
    Program Input
    Your program will prompt for a team name. Continue prompting for names until you receive “END”. Unlike the real CodeWars, this one will be limited to 16 teams maximum.
    Program Output
    Your program will output to the screen each team’s name, in the order it was entered, arranged vertically such that it is read from top to bottom. There should be two spaces between each name as printed.
    Sample Input/Output
    Enter team name: Aardvarks
    Enter team name: Emus
    Enter team name: Road Runners
    Enter team name: Coyotes
    Enter team name: END
    A E R C
    a m o o
    a u a y
    r s d o
    d t
    v R e
    a u s
    r n
    k n
    s e
    r
    s
    I need this done, due tomorrow as an HP CodeWars Qualifier!
    I'm not very far along, but here's my code:
    import java.util.*;
    public class prob02
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    int num = 1;
    int num2 = num-1;
    int num3 = 0;
    int num4 = 1;
    int num5 = num4-1;
    int count = 0
    String input = "";
    String[] teams = new String[num];
    char[] letters = new char[num4]

    while(input != "END" && teams.length<16)
    {
    System.out.println("Enter a team name");
    input= scan.nextLine();
    if(in != "END")
    {
    teams[num2]=in;
    num++;
    }
    }
    for(cntrl=0; cntrl<teams.length; cntrl++)
    {
    for(cntrl2=0; cntrl2<teams[cntrl].length; cntrl2++)
    {
    letters[num5]=teams[cntrl].charAt(cntrl2);
    num4++
    count++
    }

    }

    System.out.print

    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    ASAP!

    No problems. You relax and put your feet up while I drop everything and do your work for you.

  3. #3
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    ASAP!

    No problems. You relax and put your feet up while I drop everything and do your work for you.
    Thank you!
    There were 2 others that I finished and when I got to this one, it had me stumped.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by ProtoMan View Post
    Thank you!
    There were 2 others that I finished and when I got to this one, it had me stumped.
    :D get to work Junky, no slacking off!!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Half of zero is zero. neener neener neener.

    By the way, I've nearly done it. Just a few bugs to work out.

  7. #7
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    My main problems with it are that the while loop at the beginning isn't working and that I just plain old can't think of a way to get it vertical.
    Can anyone help me now?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    if(in != "END")
    2 problems:

    Do not compare objects with == or !=, use the equals method instead.
    You don't have an "in" variable.

  9. #9
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    if(in != "END")
    2 problems:

    Do not compare objects with == or !=, use the equals method instead.
    You don't have an "in" variable.
    What would I use to check if it doesn't equal something?
    Would I have to do if(input.equals("END")) and use the else instead?

  10. #10
    Ultra is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default

    I would love to see senior members simply ignoring pronto orders such as this one.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    The equals method returns a boolean. Simply negate it with the not operator.

  12. #12
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    The equals method returns a boolean. Simply negate it with the not operator.
    Ok then, I've got this so far:

    import java.util.*;
    public class prob02
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    int num = 1;
    int num2 = num-1;
    int num3 = 0;
    int num4 = 1;
    int num5 = num4-1;
    int count = 0;
    String input = "";
    String[] teams = new String[num];
    char[] letters = new char[num4]

    while(input != "END" && teams.length<16)
    {
    System.out.println("Enter a team name");
    input= scan.nextLine();
    if(input.equals("END") ! true)
    {
    teams[num2]=in;
    num++;
    }
    }
    for(cntrl=0; cntrl<teams.length; cntrl++)
    {
    for(cntrl2=0; cntrl2<teams[cntrl].length; cntrl2++)
    {
    letters[num5]=teams[cntrl].charAt(cntrl2);
    num4++;
    count++;
    }

    }
    }
    }
    Now, unless I'm mistaken, I need to stuff all the strings into a char[] with the first letters of each string as the first letter of each name, and then the second letter, and so on.
    I'm trying to think of a way I could do that.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've still got the same issue that Junky advised that you fix. Don't compare Strings like so:

    Java Code:
    if (foo != "bar")  {

    Instead use the equals method:

    Java Code:
    if (!foo.equals("bar"))  {
    Also, don't use [quot&#101;] [/quot&#101;] tags where you should be using [cod&#101;] [/cod&#101;] tags.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    true = ! false
    false = ! true
    if(true)
    if(! false)
    Given that the equals method returns a boolean can you figure out the syntax. If not google Java not operator.

    Aww! Answer just given away. :(

  15. #15
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    public class prob02
    {
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
    		int num = 1;
    		int num2 = num-1;
    		int num3 = 0;
    		int num4 = 1;
    		int num5 = num4-1;
    		int count = 0;
    		String input = "";
    		String[] teams = new String[num];
    		char[] letters = new char[num4]
    		
    		while(!input.equals("END") && teams.length<16)
    		{
    			System.out.println("Enter a team name");
    			input= scan.nextLine();
    			if(!input.equals("END"))
    			{
    				teams[num2]=in;
    				num++;
    			}
    		}
    		for(cntrl=0; cntrl<teams.length; cntrl++)
    		{
    			for(cntrl2=0; cntrl2<teams[cntrl].length; cntrl2++)
    			{
    				letters[num5]=teams[cntrl].charAt(cntrl2);
    				num4++;
    				count++;
    			}
    			
    		}
    	}
    }
    Like so? Sorry, I don't work with booleans often, so I've never had to use ! before... Come to think of it, I don't even remember being taught it.

    Edit:
    I'm still trying to think of a way of making that char array out of the strings to work...
    Maybe something like this:

    Java Code:
    		for(cntrl=0; cntrl<letters.length; cntrl++)
    		{
    			for(cntrl2=0; cntrl2<teams[cntrl].length; cntrl2++)
    			{
    				letters[num5] = teams[cntrl].charAt(cntrl2)
    				num4++
    			}
    		}
    Last edited by ProtoMan; 02-22-2011 at 04:03 AM. Reason: Adding Last Part

  16. #16
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Bump.
    (Sincerely sorry if this is generally frowned upon on this forum. I cntrl+f'd it in the FAQ and found nothing, so I suspected it was fine.)

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by ProtoMan View Post
    I'm still trying to think of a way of making that char array out of the strings to work...
    Have you read the API for the String class? It has a method to get a char array. But you do not need char arrays.

    Print first letter of name one
    Print first letter of name two
    ...
    Print first letter of name N
    Print new line
    Print second letter of name one
    Print second letter of name two
    ...
    Print second letter of name N
    Print new line
    etc

    You can use another String method to get a single character.

  18. #18
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Have you read the API for the String class? It has a method to get a char array. But you do not need char arrays.

    Print first letter of name one
    Print first letter of name two
    ...
    Print first letter of name N
    Print new line
    Print second letter of name one
    Print second letter of name two
    ...
    Print second letter of name N
    Print new line
    etc

    You can use another String method to get a single character.
    How do you know how many strings you would get as input, or how long a specific string is? According to the problem, they could have a 1000 letter string and it should still work.

    You could do a for loop that went like this:
    Java Code:
    		for(int cntrl = 0; cntrl<teams.length(); cntrl++)
    		{
    			System.out.print(teams[cntrl].charAt(0) + " ");
    		}
    But I would have to make it go through for each char in the string until it hit the last letter of the biggest string.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by ProtoMan View Post
    How do you know how many strings you would get as input
    You don't but you do know how many there are after user enters "END". Since you do not know how many you would be better storing them in a List rather than an array.

    or how long a specific string is?
    Once again String has a method for this. As a String is read in find out how long it is and then see if it is the longest String you have so far.

    When it comes to output you have to check if the current String has another character to print. If it does print that character if not print a space.

  20. #20
    ProtoMan is offline Member
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You don't but you do know how many there are after user enters "END". Since you do not know how many you would be better storing them in a List rather than an array.


    Once again String has a method for this. As a String is read in find out how long it is and then see if it is the longest String you have so far.

    When it comes to output you have to check if the current String has another character to print. If it does print that character if not print a space.
    Still a little lost. I must suck at programing... D:

    So you make an int to keep track of how long the longest string is, and an if statement to see if the current string is bigger than the int?

    And would you say something like this for the last part:
    Java Code:
    		if(cntrl>=teams[cntrl].length)
    		{
    			System.print(" ");
    		}

Similar Threads

  1. Replies: 7
    Last Post: 12-09-2010, 02:26 PM
  2. Replies: 16
    Last Post: 06-27-2010, 04:16 AM
  3. Ordered Array plz help....due tomorrow!!
    By canyonist in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 09:57 AM
  4. Replies: 10
    Last Post: 02-11-2009, 07:38 AM
  5. Need help~~~ Midterm tomorrow!!!
    By str4kt in forum New To Java
    Replies: 23
    Last Post: 10-10-2008, 09:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •