Results 1 to 19 of 19
  1. #1
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Making a list in java.

    Hi,

    I'm compleately new to java and any other programming language aswell.
    I need to make a simple program that allows the user to enter six names before the program stops taking any more input and lists the names separated by commas.

    I'm having trouble figuring out how to accomplish this, and would appreciate any input that would point me in the right direction.

    Thanks

  2. #2
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    ok.. so what your gonna do is set 6 blank variables all with different names. then your gonna ask the user for 6 names and set each name to a different variable. then print out each variable seperated by commas..

    this sounds like HW so i wont give you the code.

    good luck hope i could help

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

    Default

    think for-loop and an array of String.

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Oh, where is MK12 with tutorials and range of codes )
    VinTiger, and you talk about GUI or console app?

  5. #5
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks,

    So, do I use the variable "char" to store the names in?

    and if so, how do I make it a blank variable that accepts the users input?

    thanks again

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

    Default

    VinTiger, I'm not meaning to insult you, but you are asking some extremely basic questions that suggest that you may not be keeping up in class.

    I think you need to either re-read your notes, book or the very basic tutorials. You don't know enough for us to be able to help you yet, but with study you can change this. Good luck.

  7. #7
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    ill give you a hint..

    if i type one letter its a character.

    char bob="a";

    you need a string it can hold long multiple chracters like a name.

    string bob='bob';

  8. #8
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    also.. if you need a blank variable just dont give it a value.

    string bob;

    its simple.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I would have to agree with Fubarable... you need to know more java programing basics before you continue. To answer your questions:
    So, do I use the variable "char" to store the names in?
    No, char is a variable type for characters, example:
    Java Code:
    char letter = 'a';
    how do I make it a blank variable that accepts the users input?
    to create a variable (a string) for the user's input you would do the following:
    String name = "";

    Please review the following link... it contains everything you need to know about starting to program in Java:
    The Java™ Tutorials

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    i think what he is asking is VERY simple. he shouldt have to much trouble if he just keeps thinking about it.

    ask for 6 names.
    set the names as 6 variables.

    print the 6 names.

  11. #11
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Smile

    Thanks Ramsrocker, that's exactly what I needed to know. I know that these questions must seem trivial to someone that knows programming, but right now I'm still trying to to get my head around where all these variables and statements belong in the code.

    Thanks again

  12. #12
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    I'll give a little pseudo code:
    Java Code:
    import the Scanner class (search google "Java Scanner api 6")
    Your Class {
         TheMethodThatDoesTheWork() {
               Declare a String;
               Make a new instance of Scanner;
               loop 6 times {
                    StringVariable = Scanner's method that gets the next word
                           /* check out the Scanner class's api */
                    Print out: StringVariable followed by a comma
               }
               Close the Scanner;
          }
          main method {
                     call your method. If non-static, make an instance of your class
                     to call it.
          }
    }
    Hope this helped.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  13. #13
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks, it did help a lot.

    Unfortunately I'm still doing something wrong. I'm posting my begginners code for anyone to look at. I'm geting a error message saying: illegal start of expression.

    Thanks again

    import java.util.Scanner;

    import java.io.*;

    public class NamesInput5

    {

    public static void main(String[] args)

    {

    // Declarations

    String names;
    int i;
    int begin = 0;
    int end = 5;

    Scanner in = new Scanner(System.in);



    // Prompts

    for(i = begin; i <= end; i++) {


    System.out.println("Enter name");



    // Read in values


    names = in.nextLine();

    }

    System.out.println("Here is what you entered: ");

    System.out.println(names,);

    }



    }

  14. #14
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    use code tags please. Heres problem:
    Java Code:
    System.out.println(names[COLOR="Red"],[/COLOR]);
    get rid of the comma
    It still won't work, it will only print the last name entered. try this:
    Java Code:
      #13 (permalink)  
     Today, 04:39 PM
    VinTiger  
    Member
     	
    Join Date: Feb 2009
    Posts: 4
    
    Thanks, it did help a lot.
    
    Unfortunately I'm still doing something wrong. I'm posting my begginners code for anyone to look at. I'm geting a error message saying: illegal start of expression.
    
    Thanks again
    
    import java.util.Scanner;
    
    import java.io.*; 
    
    public class NamesInput5
    
    {
    
    public static void main(String[] args)
    
    {
    
    // Declarations
    
    String names;
    int i;
    int begin = 0;
    int end = 5;
    
    Scanner in = new Scanner(System.in);
    
    
    
    // Prompts
    [COLOR="DarkOrange"]// Put the orange line here[/COLOR]
    for(i = begin; i <= end; i++) {
    
    
    System.out.println("Enter name");
    
    
    
    // Read in values 
    
    
    names = in.nextLine();
    [COLOR="Blue"]// put blue line here[/COLOR]
    }
    
    [COLOR="DarkOrange"]System.out.println("Here is what you entered: ");[/COLOR]
    
    [COLOR="Blue"]System.out.println(names[COLOR="Red"],[/COLOR]);[/COLOR]
    
    }
    
    
    
    }
    Better yet, instead of printing the names variable, just get rid of the names variable (and its declaration) and do this instead:
    Java Code:
    System.out.print(in.nextLine());
    Some other tips:
    - use CODE tags
    - the begin and end are pointless waste of space, just use the numbers in your loop, don't use the variables, e.g. for(i = 0; i <= 5; i++)
    -Instead of i <= 5 (when you change end to 5), do < 6. same meaning, but more commonly done this way
    - dont skip so many lines in between statements.
    - Try putting all that in a method and then call the method in the main method.
    Hope this Helped
    -MK12
    Last edited by MK12; 02-15-2009 at 11:57 PM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Other problems witht your program

    Minor: you might get a nastygram indicating that you haven't initiated a variable... just make variable names = ""; to make that msg go away.
    Major: You're not saving all the names that you entering... only the last (you're overwirting all the names since you're using only one variable: names). To avoid this, save the names (strings) that are entered to an array. Here's a link to what an array is and how to use it:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Ohh yeah I forgot my way would print what they said immediately after..
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  17. #17
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks for all the help guys,
    I didn't realize just how much were involved in learning to program, but I'll keep trying until I get it right.

    Thanks again

  18. #18
    VinTiger is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    Ok, I got my program to work. The arrays were what I needed to learn in order to save my input and then present it after the loop had finnished.

    Thanks a lot:)

  19. #19
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Your welcome. Please go to top of page. Click Thread Tools then click Mark This Thread As Solved in the submenu, so everyone knows.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Need help with Java classes for making a program.
    By TheDarkReverend in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 05:50 AM
  2. Making Java Application Executable
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-23-2008, 01:59 PM
  3. making a count down timer using java
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 10:49 PM
  4. Making connect 4 in java
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  5. Making a Java web calendar
    By toby in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-07-2007, 07:05 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
  •