How can I insert that code in my program?
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.
How can I insert that code in my program?
What does the program need to have done and Where does the program need that functionality?
Often you create a method to do some task and add calls in the program where it needs that task to be done.
If you don't understand my answer, don't ignore it, ask a question.
In my program I have this:
And I want that the number of turns be random. How to do that?
What variable are you talking about?want that the number of turns be random
Use code like you used in testing how to create random numbers back in posts #18 and#22
If you don't understand my answer, don't ignore it, ask a question.
My program:
When the program print me random number like 2 in this:package Dice; import java.util.Random; import java.util.Scanner; public class Dice { static int game; String [] players; int [] Wonthenumberofturns; int esk; int [] er; int [] turnswinners; private Scanner scan; private Scanner scan2; private Scanner scan3; private int number; private int number2; public void Players(){ scan = new Scanner(System.in); System.out.println("Insert how many players play in game:"); int game = scan.nextInt(); scan.nextLine(); players = new String [game]; Wonthenumberofturns = new int [game]; for(int i = 0; i < game; i++){ System.out.println("Insert player number" + Integer.toString(i+1)); players[i] = scan.nextLine(); Wonthenumberofturns[i] = 0; } //scan.close(); } public void Turnnumber(){ int min = 0; int max = 10; game = (int) (Math.random() * (max+min)); System.out.println(game); er = new int [game]; turnswinners = new int [game]; } public void Go(int Gonumber){ int Biggest = 0; int Record = 0; scan3 = new Scanner(System.in); for(int i = 0; i < game; i++){ System.out.println("Enter the number of players rolling the" + players[i]); int Rezult = scan3.nextInt(); scan3.nextLine(); if (Rezult > Biggest){ Biggest = Rezult; Record = i; } } System.out.println("Turn win the player" + players[Record]); turnswinners[Gonumber] = Record; er[Gonumber] = Biggest; Wonthenumberofturns[Record]++; } public void Winner(){ for(int i = 0; i < game; i++){ int winnernumber = turnswinners[i]; int taskai = er[i]; System.out.println("Turn" + Integer.toString(i+1) + "won" + players[winnernumber]); System.out.println("collected" + Integer.toString(taskai) + "points"); } int winners = 0; int winner = 0; for (int i = 0; i < game; i++) { if (Wonthenumberofturns[i] > winners) { winners =Wonthenumberofturns[i]; winner = i; } } System.out.println("Game winner" + players[winner]); } public void zaid(){ Players(); Turnnumber(); for(int i = 0; i < game; i++){ Go(i); } Winner(); } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getNumber2() { return number2; } public void setNumber2(int number2) { this.number2 = number2; } public static void main(String[] args) { Dice game = new Dice (); game.zaid(); } public static int getI() { return game; } }
My program end good.
But when random number is like 9 program give me the error:
Insert how many players play in game: 2 Insert player number1 Lukas Insert player number2 Matty 9 Enter the number of players rolling theLukas 2 Enter the number of players rolling theMatty 2 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Dice.Dice.Go(Dice.java:57) at Dice.Dice.zaid(Dice.java:92) at Dice.Dice.main(Dice.java:111)
At line 57 the code used an index that was past the end of the array. Remember array indexes range in value from 0 to the length-1. If an array has two elements, 0 and 1 are the valid indexes.Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Dice.Dice.Go(Dice.java:57)
A lot of the code in the program needs formatting by indenting nested statements. Too many statements in the posted code are not properly intented which makes the code hard to read.
If you don't understand my answer, don't ignore it, ask a question.
And how can I correct it?
Change the code so the index's value does not go past the end of the array.
If you don't understand my answer, don't ignore it, ask a question.
Lukas (November 6th, 2013)
Okey I change. Thanks.
--- Update ---
And can you help me in my post?:
http://www.javaprogrammingforums.com...ats-wrong.html
Please copy the full text of the error message and paste it here.
If you don't understand my answer, don't ignore it, ask a question.
My post: http://www.javaprogrammingforums.com...ats-wrong.html
When I starting program everything is okey, but when I inserting the books the program showed me the last book. I want that the program show all books where I insert.
Please copy the full contents of the command prompt window that shows the program's input and output and paste it here. Add some comments showing what you want the output to be.
If you don't understand my answer, don't ignore it, ask a question.
import java.util.Scanner; public class Authors2 { int authors03; String authors; String books2; Scanner scan; public void Authors(){ scan = new Scanner(System.in); System.out.println("Please write the author: "); authors = scan.nextLine(); System.out.println("Please write how much author books you want to write: " + authors); authors03 = scan.nextInt(); scan.nextLine(); for(int i = 0; i < authors03; i++){ System.out.println("Please write author books: " + authors); books2 = scan.next(); } } public void result(){ System.out.println("You have writed the author: " + authors); System.out.println("You have entered these author books: " + books2); //Program print me the last book. I want that the program print me all books. } public void Kiak(){ Authors(); result(); } }
The posted code can't be executed for testing. It is missing a main() method.
Did you have questions about the code in post#38?
If you don't understand my answer, don't ignore it, ask a question.
He has main() method.
My question is: When I starting program everything is okey, but when I inserting the books the program showed me the last book. I want that the program show all books where I insert.public class Main { /** * @param args */ public static void main(String[] args) { Authors2 book = new Authors2 (); book.Kiak(); } }
Please copy the full contents of the command prompt window that shows the program's input and output and paste it here. Add some comments showing what you want the output to be.the program showed me the last book.
If you don't understand my answer, don't ignore it, ask a question.
Please write the author: Lukas Please write how much author books you want to write: Lukas 2 Please write author books: Lukas Test1 Please write author books: Lukas Test2 You have writed the author: Lukas You have entered these author books: Test2
What is wrong with the program's output?
What do you want the output to look like?
If you don't understand my answer, don't ignore it, ask a question.
I want that the program print me:
You have entered these author books: Test1, Test2
or:
You have entered these author books: Test1
You have entered these author books: Test2
The posted code has ONE variable that holds one String that is the value of the last name the user entered.
If you want to save more than one book name, you will need to change the program to save all the names the user entered. For example it could use a collection like ArrayList to save the names.
If you don't understand my answer, don't ignore it, ask a question.
public class Authors2 { int authors03; String authors; int books2; Scanner scan; int[] anArray; private String books22; public void Authors(){ scan = new Scanner(System.in); System.out.println("Please write the author: "); authors = scan.nextLine(); System.out.println("Please write how much author books you want to write: " + authors); authors03 = scan.nextInt(); scan.nextLine(); for(int i = 0; i < authors03; i++){ System.out.println("Please write author books: " + authors); setBooks22(scan.next()); anArray = new int[1]; } } public void result(){ anArray[0] = books2; System.out.println("You have writed the author: " + authors); System.out.println("You have entered these author books: " + anArray); //Program print me the last book. I want that the program print me all books. }
My result is:
You have entered these author books: [I@b988a6
That is the String returned by an int array's toString() method.[I@b988a6
If you want to print the array's contents you will need to index into the array to access each element in the array.
Why are you trying to use an int array to save book names? The names are String, not int.
If you don't understand my answer, don't ignore it, ask a question.
for(int i = 0; i < authors03; i++){ System.out.println("Please write author books: " + authors); setBooks(scan.next()); anArray = new String[1]; } } public void result(){ anArray[0] = books; System.out.println("You have writed the author: " + authors); System.out.println("You have entered these author books: " + anArray); //Program print me the last book. I want that the program print me all books. }
You have entered these author books: [Ljava.lang.String;@b988a6
That is the String returned by an array of Strings toString() method.[Ljava.lang.String;@b988a6
If you want to see the contents of the array, you will need to access each element in the array one at a time using array notation: theArray[theIndex]
For debugging you can print the full contents of the array:System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
If you don't understand my answer, don't ignore it, ask a question.
public void Authors(){ scan = new Scanner(System.in); System.out.println("Please write the author: "); authors = scan.nextLine(); System.out.println("Please write how much author books you want to write: " + authors); authors03 = scan.nextInt(); scan.nextLine(); for(int i = 0; i < authors03; i++){ System.out.println("Please write author books: " + authors); setBooks(scan.next()); anArray = new String[1]; } } public void result(){ anArray[0] = books; System.out.println("You have writed the author: " + authors); String[] setBooks = anArray; System.out.println("You have entered these authors books "+ java.util.Arrays.toString(setBooks)); //Program print me the last book. I want that the program print me all books. }
Now there is no errors but the result is the last book. I did theArray(theIndex);
anArray[0] = books;