Results 1 to 3 of 3
  1. #1
    jameshl2008 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Can't do ANYTHING after I used Scanner class

    After I use my readPolynomial method in the demo class, I simply cannot do anything else after it. I know the implementation isn't right for the toString method but the fact is even if I got it right I can't do ANYTHING in my main class except readPolynomial.

    The readpolynomial method takes in my input but after I choose to end the while loop by answering "n" to continue, everything just stops, there is no way for me to include anything else that I need to do in my main class.

    I have to use the scanner class, but it seems impossible to do anything else because I need to use the readPolynomial method before any other method. The problem is that readPolynomial is the only thing it well let me do, it simply ignores my other statements.

    Let me know if this is confusing. TL;DR: can't call other methods/do other things in main class after I call readPolynomial, simply doesnt do anything.

    Thanks

    Java Code:
    import java.util.Scanner;
    public class SamplePolynomial implements PolynomialInterface
    {
    	private int degree;
    	private int[] polyArray = new int[1000];// use a large array
    	private String polyString;//polynomialString;
    	private String polynomial;
    	public SamplePolynomial()
    	{
    
    	}
    	public SamplePolynomial(String polyString)
    	{
    		this.polyString = polyString;
    		//setArray();// calls private method to fill the array
    		// this is if you use an explicit constructor
    	}
    	
    	// the add method requires an instance of the interface as 
    	// the parameter and return value.
    	public PolynomialInterface add(PolynomialInterface other)
    	{
    		SamplePolynomial answer = new SamplePolynomial();
    		SamplePolynomial parameter = (SamplePolynomial)other;
    		return answer;
    	}
    	// the subtract method requires an instance of the interface as 
    	// the parameter and return value.
    	public PolynomialInterface subtract(PolynomialInterface other)
    	{
    		SamplePolynomial answer = new SamplePolynomial();
    		SamplePolynomial parameter = (SamplePolynomial)other;
    		return answer;
    	}
    
    	public String toString()
    	{
    		
    		
    		return polynomial;
    	}
    	public void readPolynomial()
    	{
    		int coefficient, constant, exponent;
    		Scanner scan = new Scanner(System.in);
    		String answer = "";
    		
    		do {
    		
    		System.out.println("Enter a coefficient");
    		coefficient = scan.nextInt();
    		
    		System.out.println("Enter an exponent");
    		exponent = scan.nextInt();
    		
    		scan.nextLine();
    		System.out.println("Continue?");
    		answer = scan.nextLine();
    		
    		}
    		while (answer.equalsIgnoreCase("y"));
    		scan.next();
    		int count = 0;
    		while (scan.hasNextLine()) {
    			count++;
    		}
    		
    		for (int i = 0; i < count / 3; i ++) {
    			polynomial = coefficient + "x";
    		}
    		//if (answer.equalsIgnoreCase("n")) {
    		//	scan.close();
    		//}
    		//scan.nextLine();	
    	
    	}
    	}
    Java Code:
    public class Demo {
    public static void main (String[] args) {
    	PolynomialInterface p1 = new SamplePolynomial();
    	PolynomialInterface p2 = new SamplePolynomial();
    	System.out.println("enter polynomial");
    	p1.readPolynomial();
    	System.out.println("wont work cuz java is a cunt");
    	p2.readPolynomial();
    
    }
    }
    Java Code:
    //----------------------------------------------------------------------------
    // Interface for a class that implements addition and subtraction on polynomials
    //----------------------------------------------------------------------------
    
    
    
    public interface PolynomialInterface
    {
    	PolynomialInterface add(PolynomialInterface other);
    	// Effect:        Adds value to owner of addPolynomial method.
    	// Postcondition: Return value =  this + value.
    	PolynomialInterface subtract(PolynomialInterface other);
    	// Effect:        Subtracts value from owner of addPolynomial method.
    	// Postcondition: Return value = this - value.
    	void readPolynomial();
    	// Postcondition: polynomial read.
    	String toString();
    	// Postcondition: polynomial converted to string.
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default Re: Can't do ANYTHING after I used Scanner class

    Your problem could be with the way the Scanner class handles line end characters when using nextLine() after nextInt().
    I don't have a tutorial on this. Do a Search on the internet for an explanation.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Can't do ANYTHING after I used Scanner class

    Java Code:
    while (scan.hasNextLine()) {
       count++;
    }
    That's a possible problem.
    If there is a next line, then there will always be a next line, since you never actually read the line.
    You'll need to stick some debugging in there to see if that's the case.

    However, Norm's suggestion is also a possible cause of problems. In general you shouldn't mix calls to nextInt with calls to nextLine as they work in mysterious ways. Use next() instead of nextLine().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Using the Scanner Class with jdb
    By flubbernugget in forum New To Java
    Replies: 4
    Last Post: 07-05-2011, 02:17 AM
  2. scanner Class
    By vibaviattigala in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 07:31 PM
  3. Help with Scanner Class
    By VisionIncision in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 02:53 PM
  4. Using the scanner class
    By danielwestjr in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 10:49 AM
  5. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •