Java

Moderators: zibadian
Number of threads: 7641
Number of posts: 17980

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Infinite Loop Problem Posted by bryan1991 on 3 Nov 2009 at 3:33 PM
I can't think of how to stop this infinite loop I have ran into. The assignment is to create a menu driven program and have the user enter a word into the dictionary. Then they have the choice to enter more words or check a word against the "dictionary" they have created.

import java.util.Scanner;

public class W11E1 {

public static void main(String[] args) {

Menu();

Scanner input = new Scanner (System.in);

String [] array = new String [100];

int choice = getChoice();

while (true){

if (choice == 1){

System.out.println("Enter a word into dictionary.");

for (int i = 0; i < array.length; i++){
array [i] = input.next();

if (i > array.length)
System.out.println("Limit reached.");}}

if (choice == 2){

System.out.println("Enter word to check against dictionary.");

String Word = CheckWord();

for (int i = 0; i < array.length; i++){
array [i] = Word;}}

if (choice == 3){
System.exit(0);}

Menu();
getChoice();
}
}

public static void Menu(){
System.out.println("Enter your choices corresponding number.");
System.out.println("1. Enter a dictionary word.");
System.out.println("2. Check a word against the dictionary.");
System.out.println("3. Quit.");
}

public static int getChoice(){
Scanner input = new Scanner (System.in);

int choice = input.nextInt();

return choice;
}

public static String CheckWord(){
Scanner input = new Scanner (System.in);

String Word = input.next();

return Word;
}

}

Attachment: W11E1.java (1371 Bytes | downloaded 42 times)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.