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 15 of 15

Thread: Help with array nots being updated

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with array nots being updated

    Two spots in my code not working as enteneded if anyone could help tell me were im going wrong would be grateful in one spot it seems to be skipping threw my if statement and the other is not updating on my counter at the end
    package roulette;
     
    import java.util.*;
     
     
    public class Roulette {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    Scanner scan = new Scanner(System.in);
    Scanner scan1 = new Scanner(System.in);
    Scanner scan2 = new Scanner(System.in);
    char userinput;
    char bet;
    int howmuch;
    int odds;
    int iamount =500;
    int betnum=0;
    int additional=0;
    int amountholder=0;
    int numholder=0;
    int wins=0;
    int lose=0;
    int newcash=0;
    String s;
     
    Random rand =new Random();
     
    int value=0;
    int[] arr = new int[value];
    int[] occurrences = new int[100];
     
     
    System.out.println("Enter Wheel Size/odds");
    		odds=scan.nextInt();
     
    do		{
    	System.out.println("Do you want to play?");
    	System.out.println("Type: b to bet");
    	System.out.println("Type q to quit");
    	userinput = scan1.nextLine().charAt(0);
     
    			if	(userinput== 'b')
    				{
    				System.out.println("How much do you want to bet?(1-"+ iamount + ")");
    				howmuch = scan.nextInt();
     
    				System.out.println("You bet"+"  "  + howmuch + "$");		
     
    						if (howmuch> iamount)
    						{
    							System.out.println("You bet to much");	
    						}
    						else
    							{
    							System.out.println("What number do you want to bet on ?" +"1-" +(odds) );
    							betnum= scan.nextInt();
    									if (betnum> odds)
    									{
    										System.out.println("You choice is out of bounds");	
    									}
     
    									else {
     
    							System.out.println("You bet on"+"  "+ betnum);
    							System.out.println("Do you want to bet on another number?");
    							System.out.println("If yes type: a");
    							System.out.println("To spin type: s");
    							bet = scan1.nextLine().charAt(0);
     
     
    										if (bet == 'a'|| bet == 's')
     
    										{
     
    											if (bet == 'a')
    											{ 
     
    												System.out.println("If you bet a number leave a space between each number");
    												s=scan2.toString();
    												String arrayString[] = s.split("\\s+"); 
     
     
    												String[] items = s.split("\\s+");
     
    												int[] results = new int[items.length];
     
    												for (int i = 0; i < items.length; i++) 
    												{
     
    												    try {
    												        results[i] = Integer.parseInt(items[i]);
    												    } catch (NumberFormatException nfe) {};
     
    												//for some reason i cant get this to go threw this
    											amountholder= howmuch* arrayString.length;
    												}			
    														if (amountholder>iamount)
    															{ 
    																System.out.println("You bet to much");	
    															}	
     
    															else {
    															int numholder1 = rand.nextInt(odds) + 1;		
    															int[] array = results;
    																	 value =numholder;
     
    																	int index = Arrays.asList(array,betnum).indexOf(numholder);
    																	System.out.println("You got" + numholder1);
     
     
     
     
     
    																	if(index>0);
    																	{      
    																		wins++;
     
    																		newcash=iamount +(howmuch* (odds-1));
     
    																		System.out.println("Congratz you Won" + " "+ (howmuch* (odds-1)));
    																	}                                      
     
    																	if(index<0) 
    																	             {
    																		newcash= (iamount-howmuch);
    																		lose++;
    																		System.out.println("Sorry you lost" +" " + howmuch);
     
    																	              }                
    																	}
     
    										System.out.println("You now have "+ newcash + "$");
    										iamount=newcash++;						
    										System.out.println("Type: b to bet");
    										System.out.println("Type q to quit");
    										userinput = scan1.nextLine().charAt(0);
     
     
     
     
     
    											}		
     
     
     
     
    											if(bet == 's');
    															{
    													numholder = rand.nextInt(odds) + 1;			
    													System.out.println("You got" + numholder);
    																			if (numholder == betnum|| numholder==additional)
    																			{ wins++;
     
    																			newcash=iamount +(howmuch* (odds-1));
    																			value=numholder;
    																			System.out.println("Congratz you Won" + " "+ (howmuch* (odds-1)));
     
    																			}	
     
    																			else 
    																			{
    																				newcash= (iamount-howmuch);
    																				lose++;
    																				System.out.println("Sorry you lost" +" " + howmuch);
     
    																			}
     
    												System.out.println("You now have "+ newcash + "$");
     
     
     
    												iamount=newcash++;
     
    															}			
     
    										}					
     
    											}
     
     
     
    							}			
     
     
     
     
    				}		
     
     
     
     
     
     
     
     
     
    }
     
    			while(userinput != 'q');
    				System.out.println("Wins:"+ " "+ wins + "Losses :"+ " "+ lose);
    				System.out.println("");	
     
    				// should be my updater but its getting bypassed donno why
     
    				for (int a : arr) {
    				    occurrences[a]++;	
     
    				System.out.println("Picks"+occurrences[a]++);}	
     
    				scan.close();
    			scan1.close();
    			scan2.close();
    			if(iamount>500)    {
    				System.out.println("Net gains :"+ (iamount-500));	
    								}	
    			else {
    				System.out.println("Net losses :" + (500 -iamount));	
    				}
     
    	}}


    sample output :
    Enter Wheel Size/odds
    2
    Do you want to play?
    Type: b to bet
    Type q to quit
    b
    How much do you want to bet?(1-500)
    12
    You bet 12$
    What number do you want to bet on ?1-2
    1
    You bet on 1
    Do you want to bet on another number?
    If yes type: a
    To spin type: s
    // this is were i need the help when you hit a do you need to throw each extra bet into an array or can you use some type of if statement?

    To spin type: s
    s
    You got1
    Congratz you Won 12
    You now have 512$
    You chose to quit
    Do you want to play?
    Type: b to bet
    Type q to quit
    q
    Wins: 1Losses : 0
    // this is were i need the counter to say 2 came up 3times or however many times your spinned till u quit
    Net gains :12


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    this is were i need the help when you hit a do you need to throw each extra bet into an array
    Can you explain what problem you are having?

    What is the purpose of saving the data in an array? How will the array's contents be used by the program?

    I problem I see is shown by the warning messages the compiler generates. Make sure that all warning messages are displayed so you will see them.

    Use the -Xlint option with the javac command to get all warnings.
    For example:
    D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint Roulette.java
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help with array nots being updated

    a
    If you bet a number leave a space between each number
    You got12
    Congratz you Won 132
    Sorry you lost 12
    You now have 488$
    Type: b to bet
    Type q to quit


    it works without throwing an error just seem to be skipping it when i hit a to go into the if statement i get the print statement then it gives be both a win and loss statement for some reason not really sure were the error is

    --- Update ---

    purpose of the data being saved is that each extra bet need to be compared to the final spin

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    I problem I see is shown by the warning messages the compiler generates. Make sure that all warning messages are displayed so you will see them.

    Use the -Xlint option with the javac command to get all warnings.
    For example:
    D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint Roulette.java
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with array nots being updated

    it seems to be splitting the string and placing in array but after that it errors
    String.charAt(int) line: not available
    Roulette.main(String[]) line: 41

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    Can you copy then full text of the error message and paste it here. I don't understand what you posted.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with array nots being updated

    instead of going down the line after the if statement is parsed it kicks it out to the begining do statement

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    Is that because the if is false?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with array nots being updated

    java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    this is all im getting as an error

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    That error says that the String being indexed is empty. There is no character at index 0

    The problem is the Scanner class leaves the endline char in the buffer after a nextInt(). Call nextLine() to clear the buffer before calling a nextLine() to read the next line.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with array nots being updated

    i tried that thats not the issue just makes my hit "a" twice then acts like i hit my spin option anyway

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    To get around the problem with a call to nextLine after a call to nextInt, change all the calls to nextLine() to be calls to next().

    Did you change the compiler's settings to show all warnings?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2014
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with array nots being updated

    i dont believe my scans are the issue im using different scanners for each type so i dont run into that issue its why i have scan1, scan2,scan3

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    Did you miss this question?

    Did you change the compiler's settings to show all warnings?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with array nots being updated

    Also posted at: Roulette Wheel Not Working Completely - Java | Dream.In.Code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JSF - input field is not updated
    By rcbandit in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 19th, 2012, 04:10 PM
  2. Plotting updated graph continuously
    By sudhan in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2011, 09:28 PM
  3. JlList not getting updated properly
    By frozenmonkey in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 12th, 2011, 08:39 AM