Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game.
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_weight="15"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="Enter Red Ball : " android:textSize="22sp" android:textColor="#000000" android:layout_weight="60"/> <EditText android:id="@+id/rBall" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:inputType="number"/> </LinearLayout> <Button android:id="@+id/winBtn" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="15" android:layout_gravity="center" android:text="Winning Numbers" /> <TextView android:id="@+id/wResult" android:layout_width="match_parent" android:layout_height="0dp" android:text="White Balls : " android:textColor="#000000" android:textSize="20sp" android:layout_marginTop="20dp" android:layout_weight="15"/> <TextView android:id="@+id/rResult" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="20dp" android:text="Red Ball : " android:textColor="#000000" android:textSize="20sp" android:layout_weight="15"/> </LinearLayout>
Here is the MainActivity.java code below
package com.believe.lotteryapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText wBall1, wBall2, wBall3, wBall4, wBall5, rBall; private TextView wResult, rResult; private Button winBtn; private Random ran ; private ArrayList winningWhiteBalls; private int winningRedBall; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ran = new Random(); winBtn = findViewById(R.id.winBtn); wBall1 = findViewById(R.id.wBall1); wBall2 = findViewById(R.id.wBall2); wBall3 = findViewById(R.id.wBall3); wBall4 = findViewById(R.id.wBall4); wBall5 = findViewById(R.id.wBall5); rBall = findViewById(R.id.rBall); wResult = findViewById(R.id.wResult); rResult = findViewById(R.id.rResult); winBtn.setOnClickListener(this); } private void generateWinningNumbers() { winningWhiteBalls = new ArrayList<>(); for(int i=0;i<5;) { int num = ran.nextInt(70)+1; if(!winningWhiteBalls.contains(num)) { winningWhiteBalls.add(num); i++; } } winningRedBall = ran.nextInt(25)+1; } @Override public void onClick(View v) { int wball1,wball2,wball3,wball4,wball5, rball; ArrayList allowedNumbers = new ArrayList<>(); for(int i=1;i<70;i++) allowedNumbers.add(i); wball1 = Integer.parseInt(wBall1.getText().toString()); wball2 = Integer.parseInt(wBall2.getText().toString()); wball3 = Integer.parseInt(wBall3.getText().toString()); wball4 = Integer.parseInt(wBall4.getText().toString()); wball5 = Integer.parseInt(wBall5.getText().toString()); rball = Integer.parseInt(rBall.getText().toString()); boolean validNumbers = true; if(!allowedNumbers.contains(wball1)) validNumbers = false; else allowedNumbers.remove(new Integer(wball1)); if(!allowedNumbers.contains(wball2)) validNumbers = false; else allowedNumbers.remove(new Integer(wball2)); if(!allowedNumbers.contains(wball3)) validNumbers = false; else allowedNumbers.remove(new Integer(wball3)); if(!allowedNumbers.contains(wball4)) validNumbers = false; else allowedNumbers.remove(new Integer(wball4)); if(!allowedNumbers.contains(wball5)) validNumbers = false; else allowedNumbers.remove(new Integer(wball5)); if(rball < 1 || rball > 25) validNumbers = false; if(!validNumbers) Toast.makeText(getApplicationContext(),"Invalid number(s) selected",Toast.LENGTH_SHORT).show(); else { generateWinningNumbers(); for (int i = 0; i < winningWhiteBalls.size(); i++) wResult.setText(wResult.getText().toString() + " " + winningWhiteBalls.get(i)); rResult.setText(rResult.getText().toString() + " " + winningRedBall); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> <EditText android:id="@+id/wBall5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_weight="15"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="Enter Red Ball : " android:textSize="22sp" android:textColor="#000000" android:layout_weight="60"/> <EditText android:id="@+id/rBall" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:inputType="number"/> </LinearLayout> <Button android:id="@+id/winBtn" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="15" android:layout_gravity="center" android:text="Winning Numbers" /> <!--Created a new button called play again--> <!--make it invisible first--> <!--visible only after winBtn is clicked--> <Button android:id="@+id/playAgainBtn" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="15" android:layout_gravity="center" android:text="Play Again" android:visibility="invisible" /> <TextView android:id="@+id/wResult" android:layout_width="match_parent" android:layout_height="0dp" android:text="White Balls : " android:textColor="#000000" android:textSize="20sp" android:layout_marginTop="20dp" android:layout_weight="15"/> <TextView android:id="@+id/rResult" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="20dp" android:text="Red Ball : " android:textColor="#000000" android:textSize="20sp" android:layout_weight="15"/> </LinearLayout>
MainActivity.java
package com.believe.lotteryapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText wBall1, wBall2, wBall3, wBall4, wBall5, rBall; private TextView wResult, rResult; private Button winBtn; private Random ran ; private ArrayList winningWhiteBalls; private int winningRedBall; //new button to play again private Button playAgain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ran = new Random(); winBtn = findViewById(R.id.winBtn); wBall1 = findViewById(R.id.wBall1); wBall2 = findViewById(R.id.wBall2); wBall3 = findViewById(R.id.wBall3); wBall4 = findViewById(R.id.wBall4); wBall5 = findViewById(R.id.wBall5); rBall = findViewById(R.id.rBall); wResult = findViewById(R.id.wResult); rResult = findViewById(R.id.rResult); winBtn.setOnClickListener(this); //play again button playAgain=findViewById(R.id.playAgainBtn); //when it is clicked //clear all user input and game output //also hide play again button and //show winning number button playAgain.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //clear everything //clear the white ball text box wBall1.setText(""); wBall2.setText(""); wBall3.setText(""); wBall4.setText(""); wBall5.setText(""); //clear the red ball text box rBall.setText(""); //clear the results wResult.setText("White Balls : "); rResult.setText("Red Balls : "); //hide play again button playAgain.setVisibility(View.GONE); //show the winning number button winBtn.setVisibility(View.VISIBLE); } }); } private void generateWinningNumbers() { winningWhiteBalls = new ArrayList<>(); for(int i=0;i<5;) { int num = ran.nextInt(70)+1; if(!winningWhiteBalls.contains(num)) { winningWhiteBalls.add(num); i++; } } winningRedBall = ran.nextInt(25)+1; } @Override public void onClick(View v) { int wball1,wball2,wball3,wball4,wball5, rball; ArrayList allowedNumbers = new ArrayList<>(); for(int i=1;i<70;i++) allowedNumbers.add(i); //check first , whether the edit text box has any value //to avoid application crash on clicking the winning numbers button when no value is entered if(wBall1.getText().toString().length()==0|| wBall2.getText().toString().length()==0|| wBall3.getText().toString().length()==0|| wBall4.getText().toString().length()==0|| wBall5.getText().toString().length()==0|| rBall.getText().toString().length()==0){ //Inform user that the boxes are empty Toast.makeText(this,"Please enter the values",Toast.LENGTH_LONG).show(); } else { wball1 = Integer.parseInt(wBall1.getText().toString()); wball2 = Integer.parseInt(wBall2.getText().toString()); wball3 = Integer.parseInt(wBall3.getText().toString()); wball4 = Integer.parseInt(wBall4.getText().toString()); wball5 = Integer.parseInt(wBall5.getText().toString()); rball = Integer.parseInt(rBall.getText().toString()); boolean validNumbers = true; if (!allowedNumbers.contains(wball1)) validNumbers = false; else allowedNumbers.remove(new Integer(wball1)); if (!allowedNumbers.contains(wball2)) validNumbers = false; else allowedNumbers.remove(new Integer(wball2)); if (!allowedNumbers.contains(wball3)) validNumbers = false; else allowedNumbers.remove(new Integer(wball3)); if (!allowedNumbers.contains(wball4)) validNumbers = false; else allowedNumbers.remove(new Integer(wball4)); if (!allowedNumbers.contains(wball5)) validNumbers = false; else allowedNumbers.remove(new Integer(wball5)); if (rball < 1 || rball > 25) validNumbers = false; if (!validNumbers) Toast.makeText(getApplicationContext(), "Invalid number(s) selected", Toast.LENGTH_SHORT).show(); else { generateWinningNumbers(); for (int i = 0; i < winningWhiteBalls.size(); i++) wResult.setText(wResult.getText().toString() + " " + winningWhiteBalls.get(i)); rResult.setText(rResult.getText().toString() + " " + winningRedBall); //make the play again button visible playAgain.setVisibility(View.VISIBLE); //make the winning numbers button invisible winBtn.setVisibility(View.GONE); } }//end of else } }
Output
//output when no values are entered
//output when values are entered
//Output when play again button is clicked
Screenshots
Need help to edit the code below. Am using it play a lottery game in Android...
Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game. Here is my activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/>...
Need a help with this app. when I run this app, it is stopped. Here a source code for Tip calculator application. activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFF" android:id="@+id/tableLayout" android:stretchColumns="1,2,3" android:padding="5dp"> <!-- tableRow0 --> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow0"> <TextView android:id="@+id/billTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/billTotal" android:textColor="#000" android:gravity="right" android:paddingRight="5dp"> </TextView> <EditText android:layout_width="wrap_content" android:id="@+id/billEditText" android:layout_height="wrap_content" android:layout_span="3" android:inputType="numberDecimal" android:layout_weight="1"> </EditText> </TableRow> <!-- tableRow1 --> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableRow1"> <TextView android:id="@+id/tenTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10%" android:textColor="#000" android:layout_column="1"...
Why is my app crashing? Following is an app that takes notes from users and shows it's encrypted string. It also decrypt and encrypted strings. It saves the note in a file and also loads it back. Please help me identify what I am doing wrong? activity_krypto_note.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".KryptoNoteActivity"> <EditText android:id="@+id/key" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:ems="10" android:hint="Cryptographic Key" android:inputType="number" app:layout_constraintEnd_toStartOf="@+id/encrypt" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/encrypt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:onClick="enryptClicked" android:text="ENCRYPT" android:textSize="9sp"...
If anyone here is a Java and Android Studio expert please I really need help fulfilling the requirements needed to make a Rock Paper Scissors game. I mainly need Java code to complete it I will post what I have so far I don't have much time left please if anyone can help me I would really appreciate it. Here's what the app is supposed to do... The player should be able to select either Rock, Paper, or Scissors.The app...
need help to build a fingerprint app that we have sslserversocket to tell a file use finger print before open the file what have been done below Here is my mainactivity.java and fingerprintHandler.java below <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:padding="20dp" android:text="Fingerprint Authentication" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/paraMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Place your finger on the fingerprint scanner to...
Exercise 11-4 Create a Reminder app In this exercise, you’ll add a service to a Reminder app that displays a notification every hour that says, “Look into the distance. It’s good for your eyes.” Test the app 1. Start Android Studio and open the project named ch11_ex4_Reminder. 2. Review the code. Note that it contains a layout and a class for an activity, but no class for a service. 1. Run the app. Note that it displays a message that...
Android Development: Help convert code to RecyclerView import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; public class RolodexDataAdapter extends BaseAdapter { private ArrayList<Rolodex> rolodexArrayList; private LayoutInflater layoutInflater; public RolodexDataAdapter(Context context, ArrayList<Rolodex> dataList) { this.rolodexArrayList = dataList; this.layoutInflater = LayoutInflater.from(context); } private TextView getTextView(View view, int textViewId) { return (TextView) view.findViewById(textViewId); } @Override public int getCount() { return ((null != this.rolodexArrayList) ? this.rolodexArrayList.size() : 0); } @Override public Object getItem(int i) { if ((null !=...
I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...
How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...
Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...