🔒 Closed Help how to only get characters

Status
Not open for further replies.

Penpenqt69

Journeyman
JavaScript:
import java.util.Scanner;

public class LoveCalcu {

    public static void main(String[] args) {
        
        String yourname="";
        String crush="";
        Scanner heya=new Scanner (System.in);
        System.out.println("Enter Your Name:");
        yourname=heya.next();
        System.out.println("Enter Your Crush Name:");
        crush=heya.next();
        String yie=yourname+crush;
        System.out.println(yie);
        char[] characters = yie.toCharArray();
        int length = characters.length;
    for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (characters[i] == characters[j]) {
                    int temp = j;//set duplicate element index
 
                    //delete the duplicate element by shifting the elements to left
                    for (int k = temp; k < length - 1; k++) {
                        characters[k] = characters[k + 1];
                    }
                    j--;
                    length--;//reduce char array length
 
                }
            }
        }
    String yiee = new String(characters);
    yiee = yiee.substring(0, length);
    int a=yie.length();
    int b=yiee.length();
    int c=a-b;
    System.out.println(c+" letter r duplicated bish");
    //eto na start ng condition paayos nalang concatination
           if(c==2 || c==1){
        System.out.println("Enemy");
    } else if (c==3 ||c==4){
    System.out.println("Friends");
    } else if (c==5 ||c==6) {
        System.out.println("Yie Lovers");
    } else if (c==7 ||c==8) {
        System.out.println("Affection");
    } else if (c>=9){   
        System.out.println("Married");
    } else {
        System.out.println("Invalid");
    }   
    System.out.println("String after duplicates r removed : " + yiee);
}
}
OUT PUT:
Enter Your Name:
1
Enter Your Crush Name:
2
12
0 letter r duplicated bish
Invalid
String after duplicates r removed : 12

ANG GUSTO KO PO SANANG GAWIN IS ONLY CHARACTERS LANG ANG MAKUKUHA PERO PAG NAG INPUT NUMBERS NAGBIBILANG PADIN SYA.

THANK YOU!!!
 
Pleasr don't put everything under the main() section. It doesn't make the code readable and leads to unwanted bugs.

The way you wrote your code is like this:

WhatISYourName?howOldAreyou?ifuouareanIture?

Do you see how difficult it is to read such sentence? It's no different when writing code. I am not even talking about whether your logic is correct. It's a question of legibility and readability.
 
Java po yan boss, not JavaScript.
Next, aralin mo yung regex, or regular E×ρréššions. yan yung gagamitin mong pangfilter ng mga input.

sample code below. ikaw na bahala mag integrate sa program mo. bawal kasi spoonfeed dito:

import java.util.Scanner;
import java.util.regex.*;

public class JavaRegex {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in); //object that gets input

//get the input here
System.out.println("Enter your name: ");
String yourname = myScanner.nextLine();

//the pattern.it means alpha and space only. ie. san miguel, santa claus
String pattern = "^[A-Za-zñÑ\\s]+$";

//compile the pattern
Pattern regexPattern = Pattern.compile(pattern);

//fetch the pattern to the matcher object
Matcher matcher = regexPattern.matcher(yourname);

//actual line of code that tests whether the pattern is followed
boolean isMatch = matcher.matches();


if(isMatch)
{
//input has only alpha and space characters
System.out.println("Input contains only Alpha chars");
}
else
{
//input has characters other than alpha and space
System.out.println("Input contains non Alpha chars");
}

}

}
 
Status
Not open for further replies.

About this Thread

  • 5
    Replies
  • 747
    Views
  • 5
    Participants
Last reply from:
fazz12

Trending Topics

Online now

Members online
865
Guests online
815
Total visitors
1,680

Forum statistics

Threads
2,279,355
Posts
28,990,185
Members
1,226,831
Latest member
Qwertyq23
Back
Top