10 Replies - 435 Views - Last Post: 11 October 2011 - 11:19 PM Rate Topic: -----

#1 Alaskajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 11

Can't get loop to work

Posted 11 October 2011 - 05:32 PM

Hi, I have tried and tried again to make this program work, I seem to have a problem when it prompts the user to enter how many times he/she would like the program to generate a phone number in line 52.

It will then prompt to user to enter a word but then the program ends and does nothing, I need the program to loop for as many time the user wants it to.

help? What am I doing wrong?



import java.util.*; //imports util package
import java.io.*;
import javax.swing.*;
public class Phone2 {

public static int Number(char uppercase){ //names variables

	    int generate;
       int num = 0; //determines what the value of each letter is-------------|
       		 if(uppercase=='A' || uppercase=='B' || uppercase=='C'){
             num = 2;
         }
        else if (uppercase=='D' || uppercase=='E' || uppercase=='F'){
             num = 3;
         }
        else if (uppercase=='G' || uppercase=='H' || uppercase=='I'){
             num = 4;
         }
        else if (uppercase=='J' || uppercase=='K' || uppercase=='L'){
             num = 5;
         }
        else if (uppercase=='M' || uppercase=='N' || uppercase=='O'){
             num = 6;
         }
        else if (uppercase=='P' || uppercase=='Q' || uppercase=='R' || 
		  		 uppercase== 'S'){
             num = 7;
			}
		  else if (uppercase=='T' || uppercase=='U' || uppercase=='V'){
		  		 num = 8;
         }
        else if (uppercase=='W' || uppercase=='X' || uppercase== 'Y' || 
		  		uppercase== 'Z'){
             num = 9;
			}	 
		  else if (uppercase=='-'){
		  		 System.out.print("-"); 
				 //If the user inputs a "-" a "-" is outputted
			}
		  else if (uppercase==' '){
		  		 System.out.print(" ");//enters a space if a space in inputted
         }
       return num;
    }
	 //--------------------------------------------------------------------------|
      public static void main (String[] args) {
		
		String generate; 
		
	Scanner input = new Scanner(System.in);
	
	generate = JOptionPane.showInputDialog("How many phone numbers do you want to generate? ");
	int GENERATE = Integer.parseInt(generate);
	
		for (int l = 0; l <= GENERATE; l++){
		JOptionPane.showInputDialog(
	"Enter a string of characters to get its equivalent phone number: "); 
	//prompts user to enter a word
		String phone = input.nextLine();
        for (int i = 0; i < phone.length(); i++){
       System.out.print(Number(phone.toUpperCase().charAt(i)));
		  if (i == 6){//begin if
		  break;
		  //If the string of characters is over 7 the program will stop at 7
		  }//end if (i == 7)
		 			//changes all letters to uppercase if needed
          }//end for
			  }// end int l
			 
			 
			 System.exit(0);
	}//end main
}//end program



Is This A Good Question/Topic? 0
  • +

Replies To: Can't get loop to work

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1372
  • View blog
  • Posts: 3,023
  • Joined: 05-April 11

Re: Can't get loop to work

Posted 11 October 2011 - 05:45 PM

I know this doesn't solve your problem, but it was just something I stumbled upon.

Have you learned about classes yet? If you have, then I would suggest making a class which contains the letters and the number for a button on the phone.

It might not be faster, but in my opinion it gives cleaner code, and less work to do if they are going to be used in other contents (then you dont have to copy past a whole lot of code)

public class PhoneButton {
	private int number;
	private char[] letters;
	
	public PhoneButton(int number, char[] letters) {
		this.number = number;
		this.letters = letters;
	}
	
	public boolean contains(char c) {
		for (Character letter : letters)
			if (letter == c)
				return true;
		return false;
	}
}



And to make an instance of the phonebutton you can simply use the String.toCharArray() method
new PhoneButton(2, "ABC".toCharArray());


This post has been edited by CasiOo: 11 October 2011 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 Alaskajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 11

Re: Can't get loop to work

Posted 11 October 2011 - 10:09 PM

Thanks but I'm really looking for help with the question I asked, sorry.

Also, the program does compile, it just doesn't do anything.

This post has been edited by Alaskajava: 11 October 2011 - 10:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can't get loop to work

Posted 11 October 2011 - 10:17 PM

You did not get an input from the Scanner. After your message to ask for data, add

generate = input.nextInt();


Was This Post Helpful? 0
  • +
  • -

#5 Alaskajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 11

Re: Can't get loop to work

Posted 11 October 2011 - 10:24 PM

View PostfromTheSprawl, on 11 October 2011 - 10:17 PM, said:

You did not get an input from the Scanner. After your message to ask for data, add

generate = input.nextInt();



This is what I'm getting.

Phone2.java:61: error: incompatible types
	generate = input.nextInt();
	                        ^
  required: String
  found:    int
Phone2.java:63: error: cannot find symbol


Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can't get loop to work

Posted 11 October 2011 - 10:35 PM

Change your generate variable in your main method from String to int. That way you don't need to parse inputs from the scanner again. If you want to take a string as an input instead of nextInt do nextLine.
Was This Post Helpful? 0
  • +
  • -

#7 Alaskajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 11

Re: Can't get loop to work

Posted 11 October 2011 - 10:54 PM

View PostfromTheSprawl, on 11 October 2011 - 10:35 PM, said:

Change your generate variable in your main method from String to int. That way you don't need to parse inputs from the scanner again. If you want to take a string as an input instead of nextInt do nextLine.



The program compiles again but it still will only ask for how many numbers the user would like generated.
Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can't get loop to work

Posted 11 October 2011 - 11:02 PM

Curious. Can we see your new code this far?
Was This Post Helpful? 0
  • +
  • -

#9 Alaskajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 11

Re: Can't get loop to work

Posted 11 October 2011 - 11:04 PM

View PostfromTheSprawl, on 11 October 2011 - 11:02 PM, said:

Curious. Can we see your new code this far?


I'm trying to get it to work so at the moment it looks like this.



public static void main (String[] args) {
		
		String generate; 
		
	Scanner input = new Scanner(System.in);
	
	generate = JOptionPane.showInputDialog("How many phone numbers do you want to generate? ");
	int GENERATE = Integer.parseInt(generate);
	
	GENERATE = input.nextInt();
	for (int l = 0; l < GENERATE; l++){
		JOptionPane.showInputDialog(
	"Enter a string of characters to get its equivalent phone number: "); 
	//prompts user to enter a word
		String phone = input.nextLine();
        for (int i = 0; i < phone.length(); i++){
       System.out.print(Number(phone.toUpperCase().charAt(i)));
		 //changes all letters to uppercase if needed
		  if (i == 6){//begin if
		  break;
		  //If the string of characters is over 7 the program will stop at 7
		  		  }//end if (i == 7)
		 	 }//end for i = 0
		}//end for (int l)
			 System.exit(0);
			 
	}//end main
}//end program



Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Can't get loop to work

Posted 11 October 2011 - 11:15 PM

So you take the input from input box and overwritten it:
int GENERATE = Integer.parseInt(generate);
GENERATE = input.nextInt();//overwrite here

Also there is no need to mix input box and command line. Select one to use. Then wht is your specific problem? any error message any problem you get?
Was This Post Helpful? 1
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Can't get loop to work

Posted 11 October 2011 - 11:19 PM

Great point, what smohd said. Stick to the console.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1