Variable Value Not Printing

Nov 6, 2014

I'm making a program where the user enters an ID number and can submit the amount of sales that person made for a specific product and write to a file. One of the things I have to do is making a running sum of the sales for each product for all people combined as well as an accumulation of the a persons sales every time he enters data. Everything seems to be working execpt my values like person1Total, person2Total...etx and product2Total.

When I try to write them to the file, they always come out at 0.0 even though they should be the sum of the indexes of the array sales.

import java.io.*;
import java.util.Scanner;
public class SalesReport {
// private static double p1Total, p2Total, p3Total, p4Total;
private static double product1Total, product2Total, product3Total, product4Total, product5Total;
private static double person1Total, person2Total, person3Total, person4Total;
public static void main(String[] args) throws IOException

[code]....

View Replies


ADVERTISEMENT

Printing Default Value Of Int - When Is Variable Initialized

Nov 12, 2014

In the following code the print method prints the default value of int(zero) for the first time even when the variable i has been assigned a value of 4. Why?

class A1{
A1()
{
System.out.println("Inside constructor of A1()");
print();
}
void print()
{
System.out.println("A");

[Code] ....

Output:
Inside constructor of A1()
0
Inside constructor of B1()
4

View Replies View Related

Printing A Variable Using Loops - Algorithm

Mar 10, 2014

I have 3 for loops here that do what I need them to do an individual basis. But, I can't seem to get them to work together. I need to calculate y=LN(E-k), compute the error and error squared value for y and print the variable when the value for y gives the lowest error squared value.

Java Code:
//Calculating y=LN(E-k) and Initializing the Array
for(int x=0; x<LNValues.length; x++)
{
//for(int y=0; y<9; y++)
//{
double i = Math.log(eValues[x][0] - kValue);
if(i > 0)

[code]....

View Replies View Related

Difference In Variable Assignment Between Constructor And Variable Section

May 21, 2014

Given the case I have an object which is assigned only once. What is the difference in doing:

public class MyClass {
private MyObj obj = new MyObj("Hello world");

private MyClass(){}
//...
}

and

public class MyClass {
private MyObj obj;
private MyClass(){
obj = new MyObj("Hello world");
}
//...
}

Is there a difference in performance or usability? (Given also the case there are no static calls expected) ....

View Replies View Related

Are Terms Local Variable And Member Variable Comparable

Oct 27, 2014

The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.

I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.

Java Code: public void myFunction () {
int [] myInt; // A local, member variable (because "static" keyword is not there) declared
} mh_sh_highlight_all('java');

So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?

View Replies View Related

Reference Variable - Create Another Variable And Set It Equal To First

Jan 11, 2015

Given a reference variable : Car c1 = new Car();

when we create another variable and set it equal to the first : Car c2 = c1;

we're pointing c2 at the same car object that c1 points to (as opposed to pointing c2 at c1, which in turn points at the car). So if we have code like,

Car c1 = new Car();
Car[] cA = {c1, c1, c1, c1};

are we doing the same? Are we creating four *new* reference variables, each of which points at the same car (again as opposed to pointing them at c1 itself)? I think so, but want to make sure I'm understanding this correctly.

View Replies View Related

How To Use Value Of String Variable Cel1 As Variable Name

May 23, 2014

I have a JFrame jf and JPanel jp on it. jp has five TextFields named cel1, cel2.. cel5. I wish to construct a String Cel + for loop index and run a for loop to reset the values of all the text fields using a single statement such as cel1.SetText("abc"). Similar things can be done in foxfro. How does one do it in java?

View Replies View Related

Printing Out Usernames

Mar 11, 2014

I am trying to print out a list of usernames for my chat program. It currently works except for the username portion. The Server sends a specific line that tells the client to print it as a username. That line is name//name. For example, if the name of the client was Eric, then the server would send Eric//Eric to the client. The client will then receive the line through a buffered reader. However, my current code keeps getting an error at the i=input.substring... line. Its an IndexOutOfBoundsException: String index out of range: -1.

class ChatThread extends Thread {
ChatServer cServer;
String[] sentence;
int i;
public void run() {
String input = "Eric";
try {

[code]....

View Replies View Related

Printing Array To TXT

Apr 15, 2014

So for fun I've decided to write a program that can keep track of a simple card game my friends and I designed. I've built a class for an array, that stores the basic stats of each card. I need to be able to access this array from another class, that will print the array information to a file, so we can easily keep track of who's cards have leveled up and their stats. The main class will also be using a random number generator to determine how much damage the attacker/defender deals and takes respectively. So far I have the random number generator built as well. I'm just having issues creating a print to file class and what I need to change in my array class to make it accessible by the other class that prints it to a file.

public class StartingStats {
public static void main(String[] args)
{
//Variable to use for how many spots in a stat array there are
final int statArray = 2;

[code]...

what I need to change so that a print to file class can access EACH array in this array folder. Also if any cookie cutter printToFile class I could use/borrow/change that'd be really useful, as I've never done any printing to file before. Also, the levels of each stat and exp will be changing so that's why I need a separate class to print so that I can call it when it's been updated so we always have the most up to date stats saved.

View Replies View Related

Array Isn't Printing?

Nov 17, 2014

My array isn't printing. I have tried everything. I have tried putting a nested for loop in different methods but still no luck. I have also tried System.out.print but that prints random characters/numbers.

import java.util.Scanner;
public class Square
{
public static void main(String[] args)

[Code].....

^For some reason the site doesn't show the spaces between the *. It's supposed to be a 5x5 square.

View Replies View Related

Printing A Key And A Value From HashMap

Mar 28, 2014

I have a hashmap of the form HashMap <String, Set<String>>I am trying to create a method with one argument. The argument is a key for the hashmap, if it exists it should print out the key and the associated values. I'm falling over at even getting it to print the key, it keeps printing all the keys from within the hashmap as I don't know how to load the argument into it. I have this so far

Java Code:

public void printValue(String club)
{
boolean result = clubMap.containsKey(club);
if (result)
{
String key = clubMap.keySet(club).toString();
System.out.println(key );

[code]....

View Replies View Related

Printing Same Line Twice?

Sep 24, 2014

I've been working on the below code for a couple days in my spare time. Just a simple little text-based RPG. I'm brand new at programming and Java, so excuse the bad syntax. There's probably easier ways to go about what I'm trying to accomplish, but all we've learned so far in class is while loops and if statements. Anyways, the problem is that the statement "Really? Ten health points[...]yes or no?" in my code is printing twice.

int health = 0;
boolean enterHealth = false;
String verifyHealth = "";
boolean test6 = false;
boolean test7 = false;
boolean test8 = false;

[code]....

View Replies View Related

I/O Printing From Two Methods

May 3, 2015

Lets say in method 1 I want to print the numbers 1 - 10. Easy stuff. I then print that to the

PrintStream outFile = new PrintStream(new File ("output5_01")); I declared.

Now I'm in another methods, How do I print lets say 11- 20 without erasing the first methods printed results?

so my desired results would be

outFile
-------------------------------
(From 1st method) 1- 10
(From 2nd method) 11-20

As of now the second method always over writes the first. None of my books cover this and its very frustrating when the entire code is done but this won't let me write to the file

View Replies View Related

Strings Are Not Printing As They Should?

Jun 12, 2014

Im trying to make a question game, much like a spin off from Trivial Pursuit. In this code, I call classes to get a random number. This number determines what category the question will be from. Coinciding with this number, the "if" statements go and pull the questions and answers from an alternate class. My problem is that when I try and output what should be the question and the 3 answers, its outputs "null" for each String?

This is my first class, which is just the class for the player.

Java Code: /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

[Code].....

View Replies View Related

Printing Out Value Of Each Number

Jan 10, 2015

I finally got this to work but only by printing out the value of each number to debug as such, was there an easier way to do it that I missed?

public class Statistics { 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// variables used
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
Scanner input = new Scanner(System.in);
//receive number and add it separately

[Code] ....

View Replies View Related

Printing Pattern When Name Is Given

Mar 10, 2014

I want to know how to print this type of pattern when a name is given ???

Example:

name given TEJA

Capture.PNG

View Replies View Related

Replace Negative Zero With Zero When Printing?

Mar 2, 2015

How do you replace negative zero value with a zero value when Printing results:

I want to remove the minus sign in -0.0000 and instead have 0.0000.

I am Printing coordinates and I do not want to have negative zero.

the coordinates are defined as double.

System.out.format(java.util.Locale.US," %.4f %.4f %.4f %.4f%n"xCur ,yCur,xNext,yNext);

0.0000 1.0000 -0.0000 0.0000

View Replies View Related

Printing Out String Out Of Index?

Jan 24, 2014

I am writing a Java permutation program, it takes in N and k and gives k amount of combinations based on N length.

That portion works, but the portion I have that does not work is when k is greater then 1, the array is then then printing strings out of index.

The perm algorithm ends on index - 1 for moving characters but then I push all those characters into an Array List, I would think I could print them off however I want from there but that is not the case. I'll include the minimum amount of code possible.

//permString = 1234 or something, it doesn't matter
//Use Case N = 4 k = 3, prints out 123,124,132,134 ect
//Use Case N = 4 k = 2, error index out of range but only on the printing function; the perm function doesn't take k and
//is based on length
//the "" is just an empty string
permNow("", permString);
}

[code].....

View Replies View Related

Printing All Detail On A Receipt?

Apr 27, 2014

I have a project to create an application where you can print all the detail to be written on a receipt (no more manual writing). I already have blank receipts. All I want to know is how to do that printing the details at exact position in my blank receipt. I'm starting to read about Jasper Reports but I don't know how it will work since most of its example are creating template.

View Replies View Related

Why Code Is Not Printing Matrix And Sum

Feb 28, 2014

why my code is not printing the matrix and sum?

The Java-program Matrix below first asks the user for the number of rows and columns in a matrix. After this the program asks for the values of the elements. Finally, program prints the elements of the matrix and their sum on screen. Your task is to create the missing methods. Check the example print to see how to modify the print. When printing the matrix, values on the same row are separated using tabulator.

Program to complete:

import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
int rows, columns;
Scanner reader = new Scanner(System.in);
System.out.print("Type in the number of rows: ");

[code]...

Write the missing methods here / Methods are written in the text box below.

Example output

Type in the number of rows: 3
Type in the number of columns: 4
Type in the element 1 of the row 1: 1
Type in the element 2 of the row 1: 2
Type in the element 3 of the row 1: 3
Type in the element 4 of the row 1: 4
Type in the element 1 of the row 2: 5

[code]...

Matrix:

1 2 3 4
5 6 7 8
9 10 11 12

Sum of the elements of the matrix: 78

my code

import java.util.Scanner;
public class apples {
public static void main(String[] args) {
int rows, columns;
Scanner reader = new Scanner(System.in);
System.out.print("Type in the number of rows: ");

[code]...

View Replies View Related

Not Printing Output When Using While Loops?

May 17, 2015

I am learning while loops but the output I am getting is not what I want? My class:

public class BottlesOnWall {
public static void main (String[] args) {
int x = 99;
while (x > 1) {
System.out.println(x + " bottles on the wall!");

[Code] .....

The output I am getting is this:

99 bottles on the wall!
98 bottles on the wall!
97 bottles on the wall!
96 bottles on the wall!
95 bottles on the wall!

[Code] ....

I want it to stop on "2 bottles on the wall!" and then print "Only 1 bottle left on the wall!". How do I go about doing that? I have tried different variations of the condition (x>1), I have tried (x>5) but it still goes down to 1 bottle?

View Replies View Related

Alphabets And Numbers - Printing Out In A Row

Dec 1, 2014

The code below gives me an alphabet and the numbers 0-9 printed out in column, the job is to print it in line (in a row).
 
/**
* Alphabet and numbers
*/
public class Alphabet {
public static void main() {
char letter;
char number;

[Code] ....

View Replies View Related

Reading 1 Column And Printing

Oct 6, 2014

i have to read a text doc that has 149392014080186 (station# year/month/day temp) many more following but i have to read the temp and determine if it is under 80 degress my code so far is the second scanner reads the text file i need the 1st scanner reads the user info

/******************************************************************************
*
* Filename : ClimateSummary.java.
* Description: This program computes summaries of climate data and reads if it is rainy/not rainy or cold/hot.
*
******************************************************************************/
//imports everything needed to run the program
import java.util.Scanner;
import java.io.*;

[code]....

View Replies View Related

Printing Product Of Array

Apr 6, 2015

I was having trouble printing the product of the array I am getting runtime errors. Here is part of my code

int [] grades;
grades = new int [10];
grades [0] = 100;
grades [1] = 100;
grades [2] = 100;
grades [3] = 100;
grades [4] = 83;

[Code] ......

View Replies View Related

JOptionPane Printing From A For Loop?

Feb 13, 2015

I am writing a program that has to do with ciphers and cipher shifts. The program itself works fine, but I am having trouble printing out the answers to JOptionPane. I know the basics of JOptionPane, but my problem is this:

Majority of my program takes place in a for loop, and resolves the cipher (it is a basic cipher program) 1 digit at a time. So, only the last DIGIT (I don't know how to convert a digit to a CHAR in JOptionPane) is printed to JOptionPane. Here is my code:

public static void main(String[] args) {
String cipher = JOptionPane.showInputDialog(null,
"Please enter a sentence or word that you wish to encode or decode. This program uses " + " a basic cipher shift.");
int answer = 0;
String upperCase = cipher.toUpperCase();
char[] cipherArray = cipher.toCharArray();

[Code] .......

View Replies View Related

Printing Original Byte

Nov 20, 2014

Why when I do this:

System.out.println((byte) 99);

I still get: 99 ... not the hex representation of the byte!!

Why is that?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved