• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anagram program in Java  RSS feed

 
Birel Chowdhury
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to read two pair of words as array from keyboard and then check corresponding words are anagram are not. If they are print 1 otherwise 0. Total number of words in the pair cannot exceed 100, the word length cannot exceed 100 and only lower case letters are allowed.

My code is the following:





How can I improve the code? Please advise.
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can I improve the code?

Improved? What aspect of your program does not meet your satisfaction?
Does it not work as you expect? Does it fail to compile at a certain line? If so which one?
I am trying to help you, but I don't know what you need


Could I ask you to please rephrase your question. While you are at it, please repost your code but surround it with the Code tags. To do that, copy and paste your code as you did before, then select it in the editor and click the "Code" button.

The advantages of that are 2 fold. First, all your code formatting will remain visible. Second, it adds line numbers so that we can say something like "In line xx, you need to....".
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the code tags for you. You can read our FAQ on them here.
 
Birel Chowdhury
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my code tags are added and it is also showing the line numbers. Please suggest some improvement specially how can I read two pairs of words efficiently from keyboard or user input? Thanks.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Birel Chowdhury wrote:Please suggest some improvement specially how can I read two pairs of words efficiently from keyboard or user input? Thanks.

A pair is two words, two pairs is four words. Your code will be able to read up to 100 pairs. Is that not what you want? I don't see anything wrong with your code. Are you not getting the result that you expect?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Birel Chowdhury wrote:I think my code tags are added and it is also showing the line numbers. Please suggest some improvement specially how can I read two pairs of words efficiently from keyboard or user input? Thanks.


For starters, it is completely non-intuitive. Put yourself in the mindset of your users. Does it makes sense to enter the full list of one set of input, and then, to enter the full list of the other set? Or does it make more sense to enter them in pairs?

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Birel Chowdhury wrote:Please suggest some improvement specially how can I read two pairs of words efficiently from keyboard or user input? Thanks.

Well, I'd say that the easiest is to get them to enter one word at a time, and then build any other logic you need around that.

User input is quite tricky because, for every piece of "validation" that you do, you usually need a nested loop (or a wrapper method); so the main idea is to keep it as simple as possible. However, this page may give you some ideas.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!