How To Replicate Same Variable Using For Loop
Jan 7, 2015
The concept is simple. I wish to know how I could replicate a variable using a for loop which will have a new number assigned. Essentially, as the loop progresses it would create a copy of the same variable with a number at the end. Once
Something like this, variable1, variable2, variable3, variabl4, variable5
Once I have my five variable I will then be able to print them out
import java.util.Scanner;
public class replicating_variable {
public static void main(String[] args) {
String variable1;
Scanner input = new Scanner(System.in);
for(int i = 0; i < 5; i++){
[Code]...
View Replies
ADVERTISEMENT
Feb 25, 2014
Here's the code: it's while loop inside a for loop to determine the proper length of a variable:
for (int i = 0; i < num; i++) {
horse[i]=new thoroughbred();
boolean propernamelength = false;
while (propernamelength==false){
String name = entry.getUserInput("Enter the name of horse "
[code]....
I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses.
View Replies
View Related
Jan 30, 2015
Haven't used java in a long long time and firstly need to know if its possible to build an app to replicate my ipad touch so that I can loop it between two different points on the screen. Friend recommended I got netbeans to get me started but wanted to ensure its possible before I get started.
View Replies
View Related
Jul 18, 2014
There's loads of problems with this. What I'm trying to do.
1) get a program to add the contents of an array together, preferably with a for loop and not the heavy handed version I've tried to use here.
2) get the for loop's output just once, since it won't compile or recognise the variable outside of the loop. How do I make the loop's 'counter' variable available everywhere?
public class retint {
public static void main(String[] args){
int[] onetoTen = {1,2,3,4,5,6,7,8,9,10};
for (int i=0; i<10; i++) {
int counter = (onetoTen[0] + onetoTen[1] + onetoTen[2] + onetoTen[3] + onetoTen[4] + onetoTen[5] +
onetoTen[6] + onetoTen[7] + onetoTen[8] + onetoTen[9]);
System.out.println(counter);
}
}
Terrible code, I know. There has to be a more efficient way.
View Replies
View Related
Jan 22, 2015
I am trying to create program that will append letters in "sb" variable using StringBuilder until num "0" isn't entered
But something is strange. Every letter I need to enter twice. Tried with "while" and "do while" loop, but same result.
package v0502;
import java.util.Scanner;
public class stringMain {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
[Code] ....
View Replies
View Related
May 28, 2014
I am trying to increment a variable named counterVariable each time it goes through a for enhanced loop. It is stuck on 0.
Java Code:
valuesProcessor:
for(String[] row : values) {
for(String col : row) {
int counterVariable = 0;
if(col == null)
break valuesProcessor;
[Code] .....
View Replies
View Related
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
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
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
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
Jan 27, 2015
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
View Replies
View Related
Nov 2, 2014
I have everything else working. My problem is that when i type "quit" to close the outer loop. It still runs the inner loop. The National Bank manager wants you to code a program that reads each clients charges to their Credit Card account and outputs the average charge per client and the overall average for the total number of clients in the Bank.
Hint: The OUTER LOOP in your program should allow the user the name of the client and end the program when the name entered is QUIT.In addition to the outer loop, you need AN INNER LOOP that will allow the user to input the clients charge to his/her account by entering one charge at a time and end inputting the charges whenever she/he enters -1 for a value. This INNER LOOP will performed the Add for all the charges entered for a client and count the number of charges entered.
After INNER LOOP ends, the program calculates an average for this student. The average is calculated by dividing the Total into the number of charges entered. The program prints the average charge for that client, adds the Total to a GrandTotal, assigns zero to the Total and counter variables before it loops back to get the grade for another client.Use DecimalFormat or NumberFormat to format the numeric output to dollar amounts.
The output of the program should something like this:
John Smith average $850.00
Maria Gonzalez average $90.67
Terry Lucas average $959.00
Mickey Mouse course average $6,050.89
National Bank client average $1,987.67
Code:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "";
int charge = 0;
int count = -1;
int total = 1;
int grandtotal = 0;
int average = 0;
[code]....
View Replies
View Related
Mar 8, 2014
How to convert this program from a while loop to a for loop.
import java.util.Scanner;
public class LongDivision {
public static void main(String arguments[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the dividend: ");
[Code] ....
View Replies
View Related
Feb 17, 2014
How do I convert my for loop below to a while and do while loop?
for(int a=1;a<=9;a++){
for(int b=1;b<=a;b++){
System.out.print(a);
}
System.out.println();
[Code] .....
View Replies
View Related
Feb 9, 2015
I am trying to make a program add values from a loop. So what its supposed to do is search through tokens on an imported file. The file lists State, Capital, and then capital population. Then take the population string, turn it into numbers, and then do stuff with the numbers. First I'm supposed to find the Highest and lowest population of the places in the file (which I did without problem), but the finally thing is I'm supposed to add each found population to the last so I can find the average of the populations.
I just cannot seem to grasp how to do that. I THINK I'm supposed to some how store the given value into a variable, but how do I get that variable to add to the new value?
like...?
Get token -> a
b = a
c = a + b
or wait no.....
Java Code :
import java.io.*;
import java.util.Scanner;
public class CapPopS
{
public static void main(String[] args) throws IOException
{
File stateCAP = new File("state-capital-2004population.txt");
if (!stateCAP.exists())
[Code] ....
View Replies
View Related
Feb 17, 2014
How do I use a variable in an if statement to return a public void method from another class? I've tried:
System.out.print("select 1 - 6: ");
Scanner myScanner = new Scanner(System.in);
myScanner.nextInt();
if (myScanner.nextInt()==1) {
carSelection colour = new carSelection();
colour.carSelection();
The method colour in class car selection is just 4 print line statements? I've // out the if statement to see if it is correctly calling the method and displaying the data and it is?
View Replies
View Related
Oct 6, 2014
What is variable shadowing?When does it occur?
View Replies
View Related
Feb 7, 2014
Regarding the code examples in Head First Java, this is from Chapter 5, regarding the beginning creation of the dot com game. There are two classes in quesiton
the first is the SimpleDotComTester class:
public class SimpleDotComTester {
public static void main(String[] args)
{
SimpleDotCom dot = new SimpleDotCom();
int[] locations = {2, 3, 4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourself(userGuess);
}
}
and the second one is the code for the checkYourself () method in the SimpleDotCom class
public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs)
[code]....
Now I noticed that both classes use a variable called result; the program runs fine, but assume from the example that you can use the same variable name two different classes;
View Replies
View Related
Mar 16, 2014
i have a datatable with id= tblId#{emp.id}
<c:set var="tableID" value="tblId#{emp.id}">
displaying employee details inside c:forEach
closing dataTable.
[code]...
For ex.Displaying 5 table dynamically.Need to validate check box selected or not.5 table are displayed.But it shows 5th table row id only.It is because of local varible declaration?
View Replies
View Related
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
View Related
May 2, 2015
I'm working on a program where the user inputs song information (name, artist, filesize, duration) which is stored in a database. Once the user adds a song, the details should be stored in the first empty song slot (4 song slots total) and then taken back to the menu interface.
What I'm having trouble with is that I can add the first song fine (song1), but when I try to add another song, the information entered into song1 is gone. I've worked out that this is because every time I call upon the addNewSong() method, I'm creating four Song objects that are brand new with the line:
Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();
How to fix this problem is where I'm stuck because if I move these lines elsewhere, I get a 'cannot find symbol - variable song1' error.
Here is my code:
public class SongDatabase {
Scanner console = new Scanner(System.in);
public void addNewSong() {
Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();
if (song1.isEmpty()) {
[code]....
View Replies
View Related
Jul 18, 2014
I trying to display the value of a variable from a method, but I don't know why it will I can't do it.
//Java extension package
import javax.swing.JOptionPane;
public class Palindrome
{
public static void main(String[] args) {
int number = 0;
int palindrome = 0;
[Code] ....
View Replies
View Related
Oct 24, 2014
I have code that displays 3 game scores the series total and average. For some reason game 3 outputs 0 and game 1 and 2 output correctly. I don't see anything different that game 3 is doing that game 1 and game 2 aren't so I'm not sure what the problem is.
java file
package com.valenciaprogrammers.mybowlingscores;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
public class BowlingScores implements Comparable<BowlingScores>{
private long id;
[Code] .....
View Replies
View Related
Apr 17, 2014
How can I make variable from table value? Example:
Java Code:
String table [] = {"vara","varb"};
for(int i = 0; i < table.length; i++)
{
int table[i] = i;
} mh_sh_highlight_all('java');
And vara = 0, varb = 1 here.
Is this even possible?
View Replies
View Related
Apr 23, 2014
i'm new withh java netbeans, and i want to make program where variable from database. how to get value from database to use in variable/
View Replies
View Related
Apr 30, 2014
I have a problem with my code. When, on strausParser class, i create the new object Beam i give him an array of Node object called "nodiestremi".
The problem is that when a Beam is created, the Beam created before take the Node of new Beam. Why it appends?
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException{
SetFile.setupWindows();
Structure structure = new Structure();
}
public class SetFile {
}
I get an output like this:
Import node complete
0 0 0 1
0 1 1 2
0 1 1 0
0 2 2 4
Import beam complete
and not like this:
Import node complete
0 0 0 1
0 0 1 2
0 0 1 0
0 0 2 4
Import beam complete
View Replies
View Related