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: Creating a separate method for for loop

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Creating a separate method for for loop

    I'm trying to create a PRNG which takes a seed through Scanner.

    Here's the (terrible) code:

    import java.util.*;
     
    public class Numbers {
     
    	public static void main(String[] args) {
     
    		Tokeniser();
     
    	}
     
    	private static void Tokeniser() {
     
    		Scanner sc = new Scanner(System.in);
    		String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     
    		System.out.println("ENTER SEED DATA");
    		String input = sc.next();
    		char[] tokens = input.toCharArray();
     
    		for (int i = 0; i < input.length(); i++) {
    			alphabet.indexOf(tokens[i]);
     
    			int result = tokens[i];
     
     
    		Random rand = new Random(result);
     
    		for (int j = 0; j < result; j++) {
    			long nextRandom = rand.nextInt(result);
    			System.out.print(nextRandom);
     
    		}
     
    	}
    	}
    }

    I'm not sure exactly what the issue is, but it only bases its answer off of the first letter of the input, every time. The problem could well be the fact that the whole process after the for loop IS a loop also, so it never gets anywhere past the first token before printing and ending.

    What I want to know if how to put this looping process somewhere else, and how to get 'results' to stay in scope when I do.

    Thanks
    Last edited by BenjaminJ; October 10th, 2014 at 12:22 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a separate method for for loop

    Could you please tighten up your code a bit. Why is your indentation all over the place and why are there large gaps between lines. Your code is hard to read, and it doesn't need to be. Post the reformatted code.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating a separate method for for loop

    Okay, is that better?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Creating a separate method for for loop

    You aren't doing anything with the alphabet.indexOf(tokens[i]); call.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating a separate method for for loop

    So, instead of:

    alphabet.indexOf(tokens[i]);
     
    			int result = tokens[i];

    it should be

    int result = alphabet.indexOf(tokens[i]);

    ?

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Creating a separate method for for loop

    If that is what you are trying to do, then yes. Should result be the index of the character?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating a separate method for for loop

    Partially - I was hoping to get the index of every character individually. So 'abc' would be '123' and then '123' would act as the seed for the psuedo-random number generator. Maybe a for loop isn't the way to do that?

    The idea was to get the input, break it into chars and then reference each char to a number. I think the way I've done it only returns one number, though.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a separate method for for loop

    Quote Originally Posted by BenjaminJ View Post
    Okay, is that better?
    Yes, thank you.

Similar Threads

  1. Separate method to create Window
    By johnFuller001 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 16th, 2013, 02:56 AM
  2. Creating multiple objects with a for loop
    By JackCannon15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2011, 07:26 PM
  3. Need Help Creating a Loop.
    By chrisnojen117 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 14th, 2011, 10:42 AM
  4. Creating multiple arraylists with a loop?
    By DLX in forum Collections and Generics
    Replies: 4
    Last Post: December 28th, 2010, 06:58 PM
  5. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM