Question on basic programming.

Hi everyone, im still new to programming, i got a very simple homework but i dont know where to start. The program that im needed to do is firstly ask the user to type any two words ( the program are not needed to check if the word exit ) secondly the program shud be able to tell if the 2 words are anagram of each other ( example user input: cat and act, the program will then print a msg saying that the 2 words are anagram). Anyone can give me any suggestion on where to start ?

[489 byte] By [MetalFanga] at [2007-11-26 16:26:56]
# 1

First: create a class with a main(...) method:

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html

> Hi everyone, im still new to programming, i got a

> very simple homework but i dont know where to start.

> The program that im needed to do is firstly ask the

> user to type any two words ( the program are not

> needed to check if the word exit )

then read two Strings from the standard input:

http://www.exampledepot.com/egs/java.io/ReadFromStdIn.html

> secondly the

> program shud be able to tell if the 2 words are

> anagram of each other ( example user input: cat and

> act, the program will then print a msg saying that

> the 2 words are anagram). Anyone can give me any

> suggestion on where to start ?

now convert the two Strings entered by the user into char-arrays [1], sort the arrays [2] and then use a for-statement [3] to loop through the arrays and check if they're equal.

[1] Look at the methods from the String class to see if there exists a method which will let you create a char-array from it:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

[2] Look for a method which sorts a char-array:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html

[3] How to use a for-statement:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

prometheuzza at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 2
> First...What an answer!
CaptainMorgan08a at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 3
> > First...> > What an answer!; )
prometheuzza at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 4
you must be attending a school in Florida...
CarrieHunta at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 5

hey thanks for the reply,this is what i have come up with, i have found the methods to sort the array i created http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(char[]), but i still run into error because i have no idea how to use the sort(char[] a) command, anyone can enligthen me please? Thanks in advance .

import java.util.Scanner;

public class AnagramChecker {

public static void main(String[]args){

Scanner sc = new Scanner(System.in);

System.out.print("Please enter the first word.");

String firstWord = sc.nextLine();

System.out.print("Please enter the second word.");

String secondWord = sc.nextLine();

char [] wordOne = firstWord.toCharArray();

char [] wordTwo = secondWord.toCharArray();

}

}

Message was edited by:

MetalFang

Message was edited by:

MetalFang

MetalFanga at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 6

> hey thanks for the reply,

You're welcome.

> ...

> because i have no idea how to use the sort(char[] a)

> command, anyone can enligthen me please? Thanks in

> advance .

Like this:char[] c = {'b','d','c','a','e'};

System.out.println(java.util.Arrays.toString(c));

java.util.Arrays.sort(c);

System.out.println(java.util.Arrays.toString(c));

prometheuzza at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 7
oh i see, sorry for asking some noob question, does that mean i have to import java.util.Arrays.sort; like what i did with the scanner in order for the sort(char[] a) command to work? Thanks in advance !
MetalFanga at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 8

> oh i see, sorry for asking some noob question, does

> that mean i have to import

> java.util.Arrays.sort; like what i did with

> the scanner in order for the sort(char[] a) command

> to work? Thanks in advance !

You have a couple of options:

1 - statically import the method sort(...) and use it directly in your code:import static java.util.Arrays.sort;

public class Test {

public static void main(String[] args) {

char[] array = {'b','d','c','a','e'};

sort(array);

for(char ch : array) { System.out.print(ch+" "); }

}

}

2 - import the class java.util.Arrays, and call Arrays.sort(...) in your code:import java.util.Arrays;

public class Test {

public static void main(String[] args) {

char[] array = {'b','d','c','a','e'};

Arrays.sort(array);

for(char ch : array) { System.out.print(ch+" "); }

}

}

3 - don't import anything and call java.util.Arrays.sort(...) in your code:public class Test {

public static void main(String[] args) {

char[] array = {'b','d','c','a','e'};

java.util.Arrays.sort(array);

for(char ch : array) { System.out.print(ch+" "); }

}

}

I suggest going with number 2.

Good luck.

prometheuzza at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 9

ok this is what i come up with, it is consider somewhat done and it is somewhat working, will really like to thanks prometheuzz for his/her help...btw is there a way to ignore the case because for example ACT is not an anagram of cat even though they are...

import java.util.Scanner;

import java.util.Arrays;

public class AnagramChecker {

public static void main(String[]args){

Scanner sc = new Scanner(System.in);

System.out.println("Enter two Words to check if they are Anagrams.");

System.out.println("");

System.out.println("Please enter the first word.");

String firstWord = sc.nextLine();

System.out.println("Please enter the second word.");

String secondWord = sc.nextLine();

char [] wordOne = firstWord.toCharArray();

char [] wordTwo = secondWord.toCharArray();

Arrays.sort(wordOne);

Arrays.sort(wordTwo);

if (Arrays.equals(wordOne,wordTwo)){

System.out.println("The words " + firstWord + " and " + secondWord + " are Anagrams.");

}

else{

System.out.println("The words " + firstWord + " and " + secondWord + " are NOT Anagrams.");

}

Message was edited by:

MetalFang

MetalFanga at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 10

> ok this is what i come up with, it is consider

> somewhat done and it is somewhat working, will really

> like to thanks prometheuzz for his/her help...

You're most welcome.

> btw is there a way to ignore the case because for example

> ACT is not an anagram of cat even

> though they are...

Yes, there is such a method. It's somewhere in the String class. So have a look at the API docs of the String class and browse through the available methods to see which one it is:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Good luck.

prometheuzza at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 11

There is one thing you could do before converting the stings to character arrays and sorting them. Before you do this you should check the length of the 2 inputted strings, if the lengths are unequal, the cannot be an anagram of each other.

String word1 =

String word2 =

if(word1.length() != word2.length(){

System.out.println("The two words are not anagrams of each other");

}else{

// convert to arrays, sort arrays & test arrays

}

s00bf0f8a at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...
# 12
metalfang, you can call a function .toUppercase(), you can use this function when the user inputs and make everything caps, unless your teacher requires you to output exactly what the user inputs...hope this helps!
Timm017a at 2007-7-8 22:51:06 > top of Java-index,Other Topics,Java Game Development...