Esha Rice
Esha Rice

Reputation: 59

Im having a lot of trouble with randoms in Java and was wondering if someone could help? I am quite new to coding

Convert a random character to an upper case,Replace a random character with a random digit,Add a tilde ~ character to the middle of the passcode. Here is what I have got so far in my assignment.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
 {

String pw;
int pwl;

Scanner scan = new Scanner(System.in);

System.out.println("Enter your word passcode:");
pw = scan.next();
pw = (pw.toLowerCase());
int length = pw.length();
String firstChar = pw.substring(0, 1);
String lastChar = pw.substring(length - 1);
pw = lastChar + pw.substring(1, length-1) + firstChar;
System.out.println(pw);

math.random();

 }
}

Upvotes: 1

Views: 50

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78965

// Convert a random character to an upper case
int random = (int) (Math.random() * pw.length());
pw = pw.substring(0, random) + Character.toUpperCase(pw.charAt(random)) + pw.substring(random + 1);
System.out.println(pw);

// Replace a random character with a random digit
random = (int) (Math.random() * pw.length());
pw = pw.substring(0, random) + (int) (Math.random() * 10) + pw.substring(random + 1);
System.out.println(pw);

// Add a tilde ~ character to the middle of the passcode.
int len = pw.length();
pw = pw.substring(0, len / 2) + '~' + pw.substring(len / 2);
System.out.println(pw);

Upvotes: 2

Related Questions