1 Replies - 8097 Views - Last Post: 30 October 2010 - 08:01 PM Rate Topic: -----

#1 cis170  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 10

The operator / is undefined for the argument type(s) String, int

Posted 30 October 2010 - 07:15 PM

This is a beginning Java Program

Write an application called PracticeArithmeticOperators.java that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by five spaces each. For example, if the user types in the number 49872, the program should print

4 9 8 7 2

The errors exist on all the lines with calculation, I continue to get :The operator % is undefined for the argument type(s) String, int : I have tried changing number to an integer but when I do this I get an error in my JOption Pane statement can someone please assist me with this.
Thank you
number/10000;
(number%10000)
(number%10000)
(number%10000)
(number%10000)



/***********************************************************************
   Program Name: PracticeArithmeticOperators 
   Programmers Name: Audrey McCants
   Program Description: This program will show a statement that displays each digit separated by Five spaces ***********************************************************************/

import javax.swing.JOptionPane;

public class PracticeArithmeticOperators {

	
	public static void main(String[] args) {
		String number;   // number input by user
		int dig1; // first digit
		int dig2; // second digit
		int dig3; // third digit
		int dig4; // fourth digit
		int dig5; // fifth digit
		
		
		dig1 = number/10000;
		dig2 = (number%10000)/1000;
		dig3 = (number%10000)/1000/100;
		dig4 = (number%10000)/1000/100/10;
		dig5 = (number%10000)/1000/100/10/1;
		System.out.print( "Enter five digit integer: " );
		
		dig1 = 4;
		dig2 = 9;
		dig3 = 8;
		dig4 = 7;
		dig5 = 2;
		
		number = JOptionPane.showInputDialog("Enter a number");
			
		String result = "";
		result = dig1 + " " + dig2 + " " + dig3 + " " + dig4 + " " + dig5;
		
		System.exit(0);
	}



		
		 


				
			
		
		
	}


This post has been edited by macosxnerd101: 30 October 2010 - 07:25 PM
Reason for edit:: Title renamed to be more descriptive.


Is This A Good Question/Topic? 0
  • +

Replies To: The operator / is undefined for the argument type(s) String, int

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: The operator / is undefined for the argument type(s) String, int

Posted 30 October 2010 - 08:01 PM

You cannot truncate a string using the modulus operator. That's for numerical data types only.
You only need one variable to do this assignment. Also, the assignment wants you to take user input, not hard code it in the program.

The Scanner class will come in handy here:
Scanner in = new Scanner(System.in);
// input the user's number as a string
String n = in.nextLine().trim();


Now you have a number in string format.
Next you want to output each digit using the substring() method of the String class.
// Since you probably haven't learned loops yet:
String space = "     ";
System.out.println(n.substring(0,1) + space);
// continue...
System.out.println(n.substring(4,5));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1