Jump to content

Hey guys,

 

I am learning very basic Java right now and I was wondering if there was some website where I could post my code and a question I have so that someone could help me with it. Something like Yahoo answers but more programming oriented? Or even better, is there a section/thread on LTT where I could do this? Thanks.

Delltopia

Case & Mobo: Stock Dell Optiplex 7010, CPU: i5 3470, RAM: 16gb 1333 DDR3 (1x8gb Corsair Vengence, 2x4gb Random), GPU: Diamond Radeon HD 7970,

PSU: EVGA GQ 650W, SSD: Kingston v300 128gb (OS), HDD: 700gb Seagate 7200rpm (Storage)

Link to comment
https://linustechtips.com/topic/41675-where-to-get-help/
Share on other sites

Link to post
Share on other sites

Ummm... This one.

 

oh okay, I wasn't sure so i didn't want to create too many threads.

 

So here is the code of a simple program to evaluate a polynomial. I believe everything is right but I don't understand why the loop ends after asking the user if they want to continue.

why doesn't the scanner pick up y or n?

is there somehting wrong with the line response = scan.nextLine();

 

 

import java.util.Scanner;
 
class EvalPoly
{
  public static void main (String[] args )  
  {
    Scanner scan = new Scanner ( System.in );
 
    double x;                      // a value to use with the polynomial
    double result;                 // result of evaluating the polynomial at x
    String response = "y";         // "y" or "n"
 
    while ( response.equals( "y" ) )    
    {
       // Get a value for x.
       System.out.println("Enter a value for x:")  ;
       x = scan.nextDouble();
 
       // Evaluate the polynomial.
       result =7*(x*x*x) - 3*(x*x) + 4*x - 12;
 
       // Print out the result.
       System.out.println("The value of the polynomial at x = " + x + " is :" + result + "\n" ) ;
 
       // Ask the user if the program should continue.
       System.out.println("continue (y or n)?");
       response = scan.nextLine();      
    }
 
  }
}

thanks

Delltopia

Case & Mobo: Stock Dell Optiplex 7010, CPU: i5 3470, RAM: 16gb 1333 DDR3 (1x8gb Corsair Vengence, 2x4gb Random), GPU: Diamond Radeon HD 7970,

PSU: EVGA GQ 650W, SSD: Kingston v300 128gb (OS), HDD: 700gb Seagate 7200rpm (Storage)

Link to comment
https://linustechtips.com/topic/41675-where-to-get-help/#findComment-541172
Share on other sites

Link to post
Share on other sites

oh okay, I wasn't sure so i didn't want to create too many threads.

 

So here is the code of a simple program to evaluate a polynomial. I believe everything is right but I don't understand why the loop ends after asking the user if they want to continue.

why doesn't the scanner pick up y or n?

is there somehting wrong with the line response = scan.nextLine();

 

thanks

 

The first time the scanner asks for input it will only read as far as the input can be transformed to a double. So, the next time you call nextLine it will read the rest of the line which is the \n.

You can solve this by adding scan.skip("\n"); after reading the double.

Link to comment
https://linustechtips.com/topic/41675-where-to-get-help/#findComment-542469
Share on other sites

Link to post
Share on other sites

When you post code, can you put it in code tags ("<>" button under the :) to make it easier to read.

Another good place to ask for coding help is stack overflow.

And I'm not a java guy either. (yet possibly...)

Thanks, I didn't know about the code tags, I'm still new to programming.

And StackOverflow looks good, I'll check it out.

 

The first time the scanner asks for input it will only read as far as the input can be transformed to a double. So, the next time you call nextLine it will read the rest of the line which is the \n.

You can solve this by adding scan.skip("\n"); after reading the double.

OK thanks! I understand the problem now.

Delltopia

Case & Mobo: Stock Dell Optiplex 7010, CPU: i5 3470, RAM: 16gb 1333 DDR3 (1x8gb Corsair Vengence, 2x4gb Random), GPU: Diamond Radeon HD 7970,

PSU: EVGA GQ 650W, SSD: Kingston v300 128gb (OS), HDD: 700gb Seagate 7200rpm (Storage)

Link to comment
https://linustechtips.com/topic/41675-where-to-get-help/#findComment-543481
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×