• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Anagram program in Java

 
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.
 
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....".
 
lowercase baba
Posts: 12781
51
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.
 
Saloon Keeper
Posts: 6579
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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?
 
author
Posts: 23850
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!