Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Learning Java

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Learning Java

    I am trying to use two methods in one program class. The first one works to give a simple list of numbers from a "for loop." I want to have the second method take those list of numbers and convert them (as a string) into an array. One problem is how to give them proper names.

    Here is the code:
    public class Example2 {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		//call method
    		//this works to call the method of numbers below
    		Numbers();
     
    		//call method to convert string of numbers to character Array
    		Convert();
    	}
     
    	public static int Numbers()
    	{		
    		for (int i = 0;i < 10; i++)
    		{
    		System.out.println (i);
    		}	
    	return 0;
    		//why use return 0?
    	}
     
    	public static int Convert()
    	{
    		//error is with labeling it with Numbers as I try... 
    		//...to pass the list of numbers to this method
    		String Numbers = null;
    		char[] CharArray = Numbers.toCharArray();
    		System.out.println(CharArray);
     
    		return 0;
    	}
    }


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Learning Java

    In order to make the code easier to read, place it inside code tags (the # sign wraps the selected text in code tags).

    Can you give an example of what the expected results should be?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Learning Java

    Quote Originally Posted by Zymus View Post
    In order to make the code easier to read, place it inside code tags (the # sign wraps the selected text in code tags).
    We are trying to replace the code tags with the highlight tag (please see my signature)
    I have edited the post and added them for you this time.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Learning Java

    I've looked over your code.

    	public static int Numbers()
    	{		
    		for (int i = 0;i < 10; i++)
    		{
    		System.out.println (i);
    		}	
    	return 0;
    		//why use return 0?
    	}

    You say why return 0? Well with a method like this you have to return the variable type. So in this case it needs to return an integer value. So you could return 0, 100, 420 or any integer value.

    You could change int to void in which case you wouldn't need to return anything.

    	public static void Numbers()
    	{		
    		for (int i = 0;i < 10; i++)
    		{
    		System.out.println (i);
    		}	
    	}

    To pass the value of i to the next method I would call Convert() within your for loop and update the Convert method like so:

    	public static void Numbers()
    	{		
    		for (int i = 0;i < 10; i++)
    		{
    		//System.out.println (i);
    		Convert(i);
    		}	
     
    	}
     
    	public static void Convert(int i)
    	{
    		System.out.println(i);
    	}

    Try it for yourself..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning Java

    Thanks. JavaPF's code example worked. My goal of the program is the take the list of numbers (zero to 9) and put them into an array of numbers.

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning Java

    The first method just gives a list of numbers (0 to 9). I am trying to take that list output and use the second method to convert the list to an array. Then I can work with each element of the array. But how do I label the string to array conversion?
    Will it be convert.toCharArray or numbers.toCharArray? Or is there a better way to convert to an array?

  7. #7
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Learning Java

    Quote Originally Posted by jgc1 View Post
    The first method just gives a list of numbers (0 to 9). I am trying to take that list output and use the second method to convert the list to an array. Then I can work with each element of the array. But how do I label the string to array conversion?
    Where is the array that you want to manipulate in your code?

    For loops are good for populating arrays but first you must have an array to populate. In the code that you have posted in the OP, your method Numbers() only returns one value and only does so once, because it is only called once.

    What is happening in your first method is that the output is being printed to the console, but it is not being stored anywhere for further manipulation. There might be a way to recapture the output printed to the console, but it is arguably more straightforward to place the values into an array and then print the array to the console.

    Quote Originally Posted by jgc1 View Post
    Will it be convert.toCharArray or numbers.toCharArray? Or is there a better way to convert to an array?
    A brief notational comment:

    If you are referring to the methods that you have written, it is clearer to refer to them as convert(), numbers(), and toCharArray().

  8. #8
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Lightbulb Re: Learning Java

    Is this useful to you at all?

    - Main now has 2 arrays declared in it called intArray and charArray. intArray is for the storage of the values created in numbers.

    - Numbers() method takes the intArray, stores the values for later use but also prints out each value to the screen...as you wanted.

    - Convert() method takes the filled intArray and the empty charArray and converts each of the numbers (0 - 9) to their respective char version using the Character method, forDigit(). Then they are stored in charArray.

    - charArray is printed to the screen.

    *NOTE* Character.forDigit(numbers[x], 10) is there because I know the values wont exceed past the value 9. This won't work if your values exceed the value 9.

    public class Example2 {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		//DECLARE ARRAYS
    		int[] intArray = new int[10];
    		char[] charArray = new char[10];
     
    		//call method
    		//this works to call the method of numbers below
    		intArray = Numbers(intArray);
     
    		//call method to convert string of numbers to character Array
    		charArray = Convert(intArray, charArray);
     
                    //print charArray to screen
    		for(int i = 0; i < 10; i++)
    		{
    			System.out.print(charArray[i] + " ");
    		}
    	}
     
    	public static int[] Numbers(int[] numbers)
    	{
    		System.out.print("These are the ints: ");
    		for (int i = 0; i < 10; i++)
    		{
    			numbers[i] = i;	//Store number in an integer array
    			System.out.print(numbers[i] + " "); //Print each number to screen
    		}
    		System.out.println("");
     
    		return numbers;
    	}
     
    	//Returns an array of numbers as characters
    	//@PARAM The array to be converted
    	//and The array to store the conversions
    	public static char[] Convert(int[] numbers, char[] converted)
    	{
    		System.out.print("These are the chars: ");
    		for(int i = 0; i < converted.length; i++)
    		{
    			converted[i] = Character.forDigit(numbers[i], 10);
    		}
     
    		return converted;
    	}
    }

    Output:
    L:\Code\Java\java Exercise2
    These are the ints: 0 1 2 3 4 5 6 7 8 9
    Theses are the chars: 0 1 2 3 4 5 6 7 8 9
    Last edited by TopdeK; May 6th, 2011 at 07:58 PM.

Similar Threads

  1. New to Java....still learning...Help Required
    By muraduk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2010, 03:15 PM
  2. Hello. Just Learning Java For Fun..
    By derekeverett in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2010, 04:24 PM
  3. :!! Unsure how to set this up/ Still learning java loops!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 4
    Last Post: September 29th, 2009, 10:28 AM
  4. Java Learning Resource for Beginners
    By Freaky Chris in forum The Cafe
    Replies: 1
    Last Post: April 29th, 2009, 04:57 AM
  5. 15, Loves ICT, learning Java coding.
    By Sergant Mitch in forum Member Introductions
    Replies: 1
    Last Post: September 20th, 2008, 09:40 AM