Trying To Make A Table, Strange Tab Spacing - java

I am attempting to write a program that creates a table based on parameters given by the user that describe a graph. I am merely a beginner in Java and am just starting out, so if I am using inefficient methods then that might be the reason for that. The problem that I am encountering while writing the code is that there seems to be a strange occurrence when I run the program that causes one row to have a different spacing between columns than other rows. This is extremely infuriating and I would love to have a solution to this. Again, I am an extreme beginner and am still wrapping my mind around writing mid sized programs. T
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
Scanner input5 = new Scanner(System.in);
double xjumps;
double yjumps;
double start;
double length;
double intercept;
System.out.printf("Please input the length of your table: ");
length = input.nextDouble();
System.out.printf("\n");
System.out.printf("Please input the x-axis incrementation: ");
xjumps = input2.nextDouble();
System.out.printf("\n");
System.out.printf("Please input the y-axis incrementation: ");
yjumps = input3.nextDouble();
System.out.printf("\n");
System.out.printf("When do you want the table to start: ");
start = input4.nextDouble();
System.out.printf("\n");
System.out.printf("Please input the y intercept: ");
intercept = input5.nextDouble();
double x = start;
double y = (((start/xjumps) * yjumps) + intercept);
System.out.printf("\n");
System.out.println("X\t\t\tY");
System.out.println(x + "\t\t" + y);
for(double z = -1;z <= length;z++){
x += xjumps;
y += yjumps;
System.out.println(x + "\t\t" + y);
}

Related

Java - sc.nextDouble() - make another line

Sorry for my English but it is not my native language. I have to create program to calculate some math equations. And I have find little annoying problem. I need to get my input number in one line, but when i use
sc = new Scanner(System.in);
sc.useLocale(Locale.US);
System.out.print("a=");
double a = sc.nextDouble();
System.out.print(", b=");
double b = sc.nextDouble();
I get something like this:
a=Some Number
, b=Some Number
But I need this(to be an the same line):
a=Some Number, b=Some Number
I have tried to find the answer but after 4 hours I can't find it or I don't understand it. Thanks for help.
You would do so:
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.print("a=" + a + ",b=" + b);
double a = sc.nextDouble();
double a = sc.nextDouble();
System.out.print("a ="+ a + ","+ "b ="+b );
Scanner sc = new Scanner(System.in);
sc.useLocale(Locale.US);
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.println("a="+a+"b="+b);

Polynomial Code 22

I am trying to write a program to allow the user to input two polynomials with different degrees, I believe that my do while loop is incorrect and I cannot figure out how to let the user input the second polynomial.
{
int i, coef, x, deg;
double total=0.0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter Degree");
deg=sc.nextInt();
System.out.println("Enter x");
x=sc.nextInt();
do
{
for (i=0; i<=deg; i++)
{
System.out.println("Enter Coefficent for " +i);
coef=sc.nextInt();
total=total+coef*Math.pow(x,i);
}
}
while ( deg != 0);

java - end loop when user types "N"

When the user selects yes, it loops and starts again. When the user selects N, it should end the program but I am not sure what I am missing here. This is a program to tell you the x and y values when giving the slope and y-intercept to the program.
Java file
int slope;
int yintercept;
String newEquation;
boolean play = true;
System.out.print("Enter the slope: ");
slope = input.nextInt();
System.out.print("Enter y-intercept: ");
yintercept = input.nextInt();
System.out.printf("The equation of the line is: y = %dx + %d", slope, yintercept);
System.out.print("\nWould you like to create a new equation... Y or N? ");
newEquation = input.next();
while (play)
{
if (newEquation.equals("Y"))
{
System.out.print("Enter the slope: ");
slope = input.nextInt();
System.out.print("Enter y-intercept: ");
yintercept = input.nextInt();
System.out.printf("The equation of the line is: y = %dx + %d", slope, yintercept);
System.out.print("\nWould you like to create a new equation... Y or N? ");
newEquation = input.next();
}
if (newEquation.equals("N")){
play =false;
}
else{
System.out.print("Enter the slope: ");
slope = input.nextInt();
System.out.print("Enter y-intercept: ");
yintercept = input.nextInt();
System.out.printf("The equation of the line is: y = %dx + %d", slope, yintercept);
System.out.print("\nWould you like to create a new equation... Y or N? ");
newEquation = input.next();
}
}
}
}
Why do you have the same code in if (newEquation.equals("Y")) and else part? If you expect user only to enter "Y" or "N", then you can put else in fron, like this:
else if(newEquation.equals("N"))
and delete else part.
Because the way how you wrote it, it tests if input is "Y", and then second time in the same loop iteration it is going to test if input is "N", so that means that your program take the slope info twice once when it goes trough loop, because else refers only to "N".
Try a do-while construct, as well as a equalsIgnoreCase (to make "y" and "Y" both tested against "Y").
int slope;
int yintercept;
String newEquation;
boolean play = true;
do
{
System.out.print("Enter the slope: ");
slope = input.nextInt();
System.out.print("Enter y-intercept: ");
yintercept = input.nextInt();
System.out.printf("The equation of the line is: y = %dx + %d", slope, yintercept);
System.out.print("\nWould you like to create a new equation... Y or N? ");
newEquation = input.next();
} while newEquation.equalsIgnoreCase("Y")
(I have only cut-and-pasted your lines, but have not compiled and tested. My apologies if I missed something.)
The do-while tests if the user has typed a Y/y after the first round. Note that the user doesn't have to type N/n, but instead could type, say, q in order for the loop to terminate.

JAVA using assigned values from an array in a calculation

This is a very basic question but I have just started out with JAVA and have hit a bit of a bump with regards to arrays.
What I am trying to do is populate an array with 6 pieces of information from the user:
Number of employees to be input,
An alphanumeric employee number,
A first name,
A last Name,
the number of hours they have worked,
a number input corresponding to Pay Scale.
So far I have gotten these inputs into an array in JAVA however what I wanted to do was use corresponding number input to select a constant within the Pay Scale array and then use that constant to calculate the wages of each employee.
for instance employee 1 worked 10 hours at scale 0 so that would be 10*4.50
and employee worked 10 hours at scale 1 which would be 10*4.62
import java.util.Arrays; //imports Array utility
import java.util.Scanner; //imports Scanner utility
public class test1 {
static Scanner keyb = new Scanner(System.in); //Adds a keyboard input
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of Employees: ");
int employees = scanner.nextInt();
String[] Employee = new String[employees];
String[] FirstName = new String[employees];
String[] LastName = new String[employees];
double[] HoursWorked = new double[employees];
double[] PayScale = {4.50,4.62,4.90,5.45,6.20};
for (int i = 0; i < Employee.length; i++) {
System.out.print("Enter Employee Number: ");
Employee[i] = scanner.next();
System.out.print("Enter Employee's First name: ");
FirstName[i] = scanner.next();
System.out.print("Enter Employee's Last name: ");
LastName[i] = scanner.next();
System.out.print("Enter Employee's Hours worked: ");
HoursWorked[i] = scanner.nextDouble();
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale[i] = scanner.nextDouble();
}
for (int i = 0; i < HoursWorked.length; i++) {
System.out.println("Employee " + Employee[i] + " " + FirstName[i] + " " + LastName[i] + " has "
+ HoursWorked[i] * PayScale[0]);
}
}
}
}
Am I even close to a solution on this?
Is what I'm asking possible in JAVA?
Maybe I'm just looking at this the wrong way, but any help regarding this would be greatly appreciated.
edit
OK I added the extras array into the code
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of Employees: ");
int employees = scanner.nextInt();
String[] Employee = new String[employees];
String[] FirstName = new String[employees];
String[] LastName = new String[employees];
double[] HoursWorked = new double[employees];
int[]PayScale2 = {0,1,2,3,4};
double[] PayScale = {4.50,4.62,4.90,5.45,6.20};
I'm just unsure as to where I'd index the original PayScale array with the
PayScale[PayScale2[i]]
would it go into the for statement codeblock? (I have tried putting it in there however I get an error that it's not a statement :/
change
+ HoursWorked[i] * PayScale[0]);
to
+ HoursWorked[i] * PayScale[i]);
apart from that seems to me like you're doing what you're saying you should be doing..
you already have the payscales from here: double[] PayScale = {4.50,4.62,4.90,5.45,6.20}; so the following doesn't make a lot of sense:
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale[i] = scanner.nextDouble();
First of all, if you want to keep this number (Number 0 to 4) separately, you should use another Array, not the one where you keep the Payscales, then you could index to the first array which keeps the different rates.. or else you could directly use the first array if you know the pay scale for every employee.. in the end it has to do with what you want to do and how you want to do it, but the logic and the tools are there. If you call the 2nd array PayScale2 for example:
System.out.print("Enter Employee's Payscale (Number 0 to 4): ");
PayScale2[i] = scanner.nextDouble();
then you can index to the first array for example:
PayScale[PayScale2[i]]
in which case if the user inputs 0 then PayScale2[i] would be 0 then PayScale[PayScale2[i]] would be PayScale[0] or 4.5 or whatever you set the value equal to at the first array

How to put Input on next line in Java's Scanner utility

I have a basic java question about the scanner utility. Let's say i have a simple program that takes the user input and stores it in a variable. My question is when i run the program that asks for multiple inputs, the cursor starts at the beginning of the question and not after it.
My code is:
public class question3 {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number:");
Float a = s.nextFloat();
System.out.println("Enter the second number:");
Float b = s.nextFloat();
System.out.println("Sum = " + (a+b));
System.out.println("Difference = " + (a-b));
System.out.println("Product = " + (a*b));
}
}
When I run this program it will look like Enter First Number then i type the number, and then |Enter Second Number. "|" meaning where the blinking cursor is. When I type it'll show up underneath, but it could confuse the user so I was wondering what the solution could be.
It is an IDE problem, since nothing else is wrong with the code.
Instead of println(String) before each input, change it to print(String). So it would look something like this:
public class question3{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.print("Enter the second number:");
Float b = s.nextFloat();
System.out.println("Sum = " + (a+b));
System.out.println("Difference = " + (a-b));
System.out.println("Product = " + (a*b));
}
}
Also, just a note, you should use proper/appropriate naming conventions for your variables. Like for your Scanner, you should call it reader or input; something which represents its function. The same idea goes for the rest of your variables. Also, class names start with a capital.
Here is what the finished result looks like:
System.out.println prints out string then a new line, so your input is being placed on a new line. Try making it read
System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.println();
System.out.print("Enter the second number:");
Float b = s.nextFloat();
System.out.println();
This can save you some seconds, by typing few lines:
System.out.print("Enter the first number:");
Float a = s.nextFloat();
System.out.print("\nEnter the second number:");
Float b = s.nextFloat();
System.out.println("\nSum = " + (a+b)
+"\nDifference = " + (a-b)
+"\nProduct = " + (a*b));

Categories

Resources