3 Replies - 811 Views - Last Post: 21 February 2013 - 08:38 AM Rate Topic: -----

#1 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

The method isPrime() is undefined for the type String

Posted 21 February 2013 - 08:14 AM

Code:
class MyInteger {
	//Intialize value
	int value;
	
	//Value Constructor
	MyInteger(int number) {
		value = number;
	}
	
	//GetValue Method
	public int getValue() {
		return value;
	}
	
	//isEven Method
	public static boolean isEven(int n) {
		return (n % 2 == 0);
	}
	
	//isOdd Method
	public static boolean isOdd(int n) {
		return !isEven(n);
	}
	
	//isPrime Method
	public static boolean isPrime(int n) {
		for(int f = 2; f < n/2; f++) {
			if(n % f == 0) {
				return false;
			}
		}
		return true;
	}
	
	//isEven
	public static boolean isEven(MyInteger n) {
		return n.isEven();
	}
	
	//isOdd
	public static boolean isOdd(MyInteger n) {
		return n.isOdd();
	}
	
	//isPrime
	public static boolean isPrime(MyInteger n) {
		return n.isPrime();
	}
	
	//Method isEven checks
	public boolean isEven() {
		return isEven(value);
	}
	
	//Method isOdd checks
	public boolean isOdd() {
		return isOdd(value);
	}
	
	//Method isPrime checks
	public boolean isPrime() {
		return isPrime(value);
	}
	
	//Method equals checks
	public boolean equals(int n) {
		return (value == n);
	}
	
	public boolean equals(MyInteger n) {
		return equals(n.getValue());
	}
	
	//Reads string and returns
	public static int parseInt(String s) {
		return Integer.parseInt(s);
	}
	
	public static int parseInt(char[] s) {
		return parseInt(new String(s));
	}
} // Class end


Test Code:
import java.util.Scanner;
public class TestMyInteger {
public static void main(String[] args) {
	
	//User input
	Scanner input = new Scanner(System.in);
	System.out.println("Enter a number: ");
	int a = input.nextInt();
	
	//Create object
	MyInteger i1 = new MyInteger(a);
	
	//Display output
	System.out.println(a + " is even? " + i1.isEven());
	System.out.println(a + " is odd? " + i1.isOdd());
	System.out.println(a + " is prime? " i1.isPrime());
	System.out.println("17 is prime? " + MyInteger.isPrime(17));

	char[] c ={'1', '2', '3', '4'};
	System.out.println("Characters " + MyInteger.parseInt(c));
	String s ="123456789";
	
	System.out.println("String is " + MyInteger.parseInt(s));
	System.out.println("19 is odd? " + MyInteger.isOdd(19));
	System.out.println("10 is even? " + MyInteger.isEven(10));
	System.out.println(a + " is equal to 24? " + i1.equals(24));
}
}


Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Syntax error on token "i1", delete this token
	The method isPrime() is undefined for the type String

	at TestMyInteger.main(TestMyInteger.java:16)


Is This A Good Question/Topic? 0
  • +

Replies To: The method isPrime() is undefined for the type String

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7739
  • View blog
  • Posts: 13,075
  • Joined: 19-March 11

Re: The method isPrime() is undefined for the type String

Posted 21 February 2013 - 08:25 AM

System.out.println(a + " is prime? " i1.isPrime());


Probably an issue with the missing + in this line. Try fixing that and let us know if that takes care of it.
Was This Post Helpful? 0
  • +
  • -

#3 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 10-September 12

Re: The method isPrime() is undefined for the type String

Posted 21 February 2013 - 08:35 AM

View Postjon.kiparsky, on 21 February 2013 - 08:25 AM, said:

System.out.println(a + " is prime? " i1.isPrime());


Probably an issue with the missing + in this line. Try fixing that and let us know if that takes care of it.


And here I was looking for it to be a problem with is reading as a string somewhere... Ugh... The + fixed it, thanks Jon!
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7739
  • View blog
  • Posts: 13,075
  • Joined: 19-March 11

Re: The method isPrime() is undefined for the type String

Posted 21 February 2013 - 08:38 AM

Glad I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1