For Loop - Integers Stuck At 0
Oct 6, 2014
I have this code running correctly in Eclipse all except that it seems no matter where I declare, highest, lowest, average they always seem to stay at "0". I have a feeling the location of the Initialization is not the error since I have tried moving it to inside differ loops.
Stipulations on writing this code are:
Note: You do not need to use array to save each input score for this lab. A Single loop to read in each grade, compare with the current highest grade, lowest grade, and calculate the running sum at the same time.
import java.util.Scanner;
/*Write a program that prompts the user to enter the total number of students first.
*Then ask the user to input each student’s grade and use loop statements to read in each grade.
*Check input grade to make sure 0<=grade<=100, if the user input any other number, print out warning message and ask the user to input a new grade. Display the highest score, the lowest score and the average.
*/
public class LoopStatements {
// Main Method
public static void main(String[] args) {
// Initialize
int grade = 0; // grade value
[Code] ....
View Replies
ADVERTISEMENT
Mar 16, 2014
I'm trying to do the following and my code is putting me in a loop and not allowing me to input a valid entry after using an invalid #.I ask the user for input which contains a loop if the user doesnt use the correct information, however the loop doesnt allow the user to input if the entry fails the which statement
import javax.swing.JOptionPane; // Needed for JOptionPane class
public class LoanQualifier
{
public static void main(String[] args)
{
double salary; // Annual salary
double yearsOnJob; // Years at current job
String input; // To hold string input
// Get the user's annual salary.
input = JOptionPane.showInputDialog("Enter your " +
[code]....
View Replies
View Related
Mar 14, 2014
The problem i believe is that I have the max value set to the first value given out, but every time the loop repeats the Max value gets reset to the next value in the file.
Essentially the program is supposed to read a list of credit card transactions and tell the user which transaction is the highest.
Here's the list of the sample file, note that the number of transactions could change that's why i haven't tried simply passing the values to method.
Bicycle 562.90
Groceries 138.43
Hotel 612.00
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class CreditCards {
public static void main(String[] args) throws IOException {
String fileName = "C:/Users/David/Desktop/CreditCard.txt";
File inputFile = new File(fileName);
[Code] ....
View Replies
View Related
Apr 14, 2014
While analyzing the thread dumps for a performance issue in our java ee web application, I see many thread dumps stuck on a closing brace of a while loop. Here is the code block of a third party library bitronix (version 1.3.3 SNAPSHOT)
public XAResourceHolderState findXAResourceHolderState(XAResource xaResource) throws BitronixSystemException {
Iterator it = resources.iterator();
while (it.hasNext()) {
XAResourceHolderState xaResourceHolderState = (XAResourceHolderState) it.next();
if (xaResourceHolderState.getXAResource() == xaResource)
return xaResourceHolderState;
}
return null;
}
The thread dumps indicate that many threads are stuck in RUNNABLE state on line number 07. What could be the possible reasons on why a thread could get stuck on a closing brace of a while loop? The iterator of the while loop is a custom implementation.
View Replies
View Related
Nov 13, 2014
My issue is that when I run my search, it does find a goal. However, when I try and print the route using my print route method, it just gets stuck in a loop of printing the same two nodes. What is wrong with My A* implementation?
package search;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
[code]....
View Replies
View Related
Mar 11, 2014
Create a loop where you add the integers from 1 to 50.
public class Sum50 {
public static void main(String[] args) {
int sum = 0;
int max = 50;
for(int i = 0;i <= max; i++){
sum=sum+i;
}
System.out.println("Sum is " + sum);
}
}
View Replies
View Related
Sep 28, 2014
I need to write a class in that uses the For loop and does the following things: asks user to input two integers, the second larger than the first. Next, use a for loop to sum the numbers between the two integers, including the original integer. For example: 5&8 would be 5+6+7+8 and lastly prints out the sum of this.
I have successfully been able to do the first part but when it comes to the For Loop I am a bit lost here is what I have so far
import java.util.Scanner;
public class Question3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter an integer");
int num1 =sc.nextInt();
System.out.println("please enter a larger integer");
int num2=sc.nextInt();
int sum=0;
for(int i=num1; i<=num2;i++);
}
}
View Replies
View Related
Oct 9, 2014
My instructions are to:
1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smaller than secondNum).
2. Output all the even numbers between firstNum and secondNum inclusive.
3. Output the sum of all the even numbers between firstNum and secondNum inclusive.
4. Output all the odd numbers between firstNum and secondNum inclusive.
5. Output the sum of all the odd numbers between firstNum and secondNum inclusive.
*Use while loop
int firstNum, secondNum;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer: ");
firstNum = keyboard.nextInt();
[Code] ....
What to do with the while loop and how to find even and odd numbers.
View Replies
View Related
Jul 16, 2014
working on assignment to count the total integers and have a seperate system.out if a non integer.
import java.util.Scanner;
public class CountInteger {
public static void main(String[] args) {
int correctCount = -1 ; // count number of integers
int data;
int sum = 0;
[code]...
View Replies
View Related
Sep 25, 2014
In attempting to find the greatest common divisor (gcd) of two integers, n1 and n2, whereas k is a possible gcd and gcd is initialized to 1, the following code has been entered:
for (int k = 2; k <= n1 && k <= n2; k++) {
if ( n1 % 2 == 0 && n2 % 2 == 0)
gcd= k;
}
When asked to change the previous line of code to this:
for (int k = 2; k <= n1 / 2 && k < n2 / 2; k++){
the questions states this revision is wrong and to find the reason.....well, I've changed it, entered "3" (per the answer key) for n1 and n2....
now I can see logically where k (2 in this example) is not <= n1/2, which is 3/2 or 1, since we're dealing w/integers, yet when I compile and run, my answer is indeed, gcd = 1. However, since this is a Boolean expression where && is being used, since the first portion evaluates to "false", the 2nd portion isn't executed and thus my result of 1?...... loops are throwing me for one, for sure....
View Replies
View Related
Feb 24, 2014
import java.util.Scanner;
public class Project_5
{
public static void main (String[] args)
{
Scanner input= new Scanner (System.in);
[code]....
So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. If the user types in "6" then presses enter and types in "3" , the output is:
There were 3 valid numbers entered.
The sum of the valid numbers was --- and the average was ---.
There were 0 invalid numbers entered.
It then continues on allowing the user to enter more values. Here is the example output for correct code"
Enter a positive value (EOF to quit): 4
Enter a positive value (EOF to quit): 7
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 2
Enter a positive value (EOF to quit): -1
The number "-1" is invalid.
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 0
Enter a positive value (EOF to quit): -4
The number "-4" is invalid.
Enter a positive value (EOF to quit): CTRL-D
There were 6 valid numbers entered.
The sum of the valid numbers was 29 and the average was 4.83.
There were 2 invalid numbers.
View Replies
View Related
May 18, 2014
I'm doing a project in netbeans. I have to make a on-line bank with all the GUI. In the middle i got stuck with the classcastexception..
public class Banca {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// dati dei clienti
Anagrafica c = new Anagrafica();
[code]...
And here's the class of the activities of an account.. I'm not posting my classes Conto( Bankaccount) & Movimento (Activity) because those two already do what they should..
View Replies
View Related
Mar 19, 2014
How to use a constructor with parameters where the user inputs the information? I'm doing a problem where I create a Delivery class that accepts arguments for the year, delivery number within the year, distance code (1 for short distance, 2 for long), and weight of package. The constructor is supposed to also determine the eight digit delivery number (combining the year and delivery number, like 20140054 for this year, package #54).
I know I'm not close to being done but I'm struck on the application with the constructor parameters. If I'm asking the user to input the information, does that mean I have to create a no argument constructor so it will compile? Right now it won't compile because it's asking for the parameters but I can't put them.
This is the class:
public class Delivery {
int year;
int delNum;
double weight;
int code;
[Code] .....
And the error is:
CreateDelivery.java:22: error: constructor Delivery in class Delivery cannot be applied to given types;
Delivery firstDelivery = new Delivery();
^
required: int,int,int,double
found: no arguments
reason: actual and formal argument lists differ in length
1 error
View Replies
View Related
Sep 26, 2014
I'm writing a temperature converter for class, and I'm done but one piece that I can't wrap my head around.My input scale radiobuttons work fine, but the output scale radiobuttons don't deselect automatically. They're two different button groups and I wrote them identical save for the variable names.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
[code]....
View Replies
View Related
Apr 13, 2014
I am having trouble grasping 2 dimensional arrays .I have a certain problem:
String[][] words = {
{"Red", "Blue", "Green"},
{"Orange", "Purple". "Yellow"}
[code]....
I ran the code and got OrangePurpleYellowRedBlueGreen.
View Replies
View Related
Jan 23, 2015
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
[code]....
Total IS those numbers. But question[10] will not cooperate with me and it's getting a bit aggravating as I can't find a logical reason for it to be doing that.how to perhaps lock the value of total and sTotal after 10 button clicks from any?
View Replies
View Related
Jul 1, 2014
Write a method called isSorted that accepts an array of real numbers as a parameter and returns true if the list is in sorted (nondecreasing) order and false otherwise. For example, if arrays named list1 and list2 store {16.1, 12.3,22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls is Sorted(list1) and isSorted(list2)should return false and true respectively. Assume the array has at least one element. A one-element array is considered
to be sorted. public class thirfd {
public static void main(String[] args) {
double[] arr1 = {16.1, 12.3,22.2, 14.4};
double[] arr2 = {1.5, 0.3, 7.0, 19.5, 25.1, 46.2};
isSorted(arr2);
System.out.println(isSorted(arr2));
[code]...
View Replies
View Related
Feb 13, 2014
I have spent some time thinking about the program, and realize I need two loops-- 1 that keeps track of bricks in a row and 1 that keeps track of the number of rows as the Pyramid rises in height. The Pyramid is supposed to be centered on the bottom of the screen. Three CONSTANTS have been required:
BRICK_WIDTH=30, BRICK_HEIGHT=12, BRICKS_IN_BASE=14.
I tried breaking the scope of things down by trying step-wise refinement. But, I got bogged down. When I tried a method where both loops were included, I got limited success. For some reason, my equation to center along the x axis seems to be doubling the number of bricks. But, the correct number of total rows of bricks was a success. Also, the Pyramid was centered on the bottom of the screen. Another frustrating failure was that the number of bricks would not decrease as the new layers were created. My thought process was instead of using a loop that counts up (i++) I would use a loop that counts down (j--) which I thought would reduce the number of brick in each row as the "y" coordinate was reduced by each count of the BRICK_HEIGHT.
This is the code that "works". Whenever I add the (j--) the results are NOT what I expect.
/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* This file is the starter file for the Pyramid problem.
* It includes definitions of the constants that match the sample run in the assignment,
* but you should make sure that changing these values causes the generated display to change accordingly.
*
[code].....
View Replies
View Related
Feb 18, 2014
here's my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Milimetri extends JFrame {
private JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
[code]...
When i write a value in meters ..and press the button b1 to show me a value in milimeters..
View Replies
View Related
Feb 2, 2015
I am trying to make a custom table for a DB and i am stuck.
I want only the first column to have a checkbox and others not.
Is there a better way to insert the values instead using the getValueAt ?
@Override
public Object getValueAt(int row, int col)
{
if (col == 0)
[Code].....
View Replies
View Related
Nov 4, 2013
my application (java 6 RedHat 5.3 x64 ) using clean content jar for extracting data from email messages with attachments (doc, xls, ptt ...etc) After update from CleanContent-2009.1.1.jar to CleanContent-2011.1.1.jar (and any newer version) CompilerThreads will stuck in some cases (pretty often) In that case CPU usage is 100% all time, even when all activities on application are finished.
I was able to isolate several messages and every time when I used CleanContent-2009.1.1.jar it works ok but always stuck with CleanContent-2011.1.1.jar or newer versions!
View Replies
View Related
Aug 1, 2014
The goal of this code was to create a program using main method java to analysis a piece text which has been entered from a user.
They do this by entering the text into a scanner which is then analysed by the program. The analysis is to produce word frequency, for example " This is a test" produces this results:
This is a test
1 letter words: 1
2 letter words: 1
3 letter words: 0
4 letter words: 2
5 letter words: 0
The bit that I'm stuck on is producing a mean/average, My mind telling to divide
counts.length
by
str.length
But I'm not the Best at java and I've tried to implement this but all I get are errors.
Code:
import java.util.Scanner;
public class While_Loop {
public static void main (String[] args) {
while(true){
[Code] .....
View Replies
View Related
Apr 13, 2014
I think I'm about 90% complete with this program but I'm stuck on my adding entries method. It gives me a runtime error which says:
Enter the name:
Bill
Exception in thread "main" java.lang.NullPointerException
at Phonebook.addEntry(Phonebook.java:70)
at Phonebook.main(Phonebook.java:49)
Here is my code:
/*
* This program asks the user for input for a name, phone number and notes up
* to 200 entries. It stores every contact in a file. Type 'h' for help while
* running this program.
*/
import java.util.Scanner;
import java.io.*;
[code]....
View Replies
View Related
Jul 19, 2014
Basically I have a class called Book. Its supposed to have a few simple methods that set the title of the book, mark it as returned or borrowed. So specifically my problem lies in the implementation of the method that decides if the book isBorrowed. I was thinking i'd use a simple if/else statement but the way i've been trying hasn't been successful. This is what i've tried so far.
public class Book {
String title;
boolean borrowed;
boolean returned;
boolean isBorrowed;
// Creates a new Book
public Book(String bookTitle) {
[Code] .....
View Replies
View Related
Oct 3, 2014
public class TicTacToeGame {
public static void main(String[] args) {
char [][] board = new char [3][3];
displayboard(board);
int count = 0;
boolean loop = true;
[Code] ....
I also try with check if the move is vaild but no luck with that.
View Replies
View Related
Jan 31, 2014
I have a tooltip implemented on a main page, but when you click the link tooltip box gets stuck on next page
<h:outputText value="#{it.get(newItem.strCampoNombre)}" rendered="#{newItem.strCampoTipo == '1' or newItem.strCampoTipo == '5' or newItem.strCampoTipo == '4' or newItem.strCampoTipo == '9' or newItem.strCampoTipo == '10' or newItem.strCampoTipo == '11'}" />
<h:outputText value="#{it.get((newItem.strCampoToolTip))}" rendered="#{newItem.strCampoTipo == '8'}" />
<rich:tooltip styleClass="tooltip" layout="block" rendered="#{newItem.strCampoTipo == '8'}" showDelay="500" onhide="false" hideEvent="mouseleave">
<span class="wrap"> <h:outputText value="#{it.get(newItem.strCampoNombre)}" /> </span>
</rich:tooltip>
problem.png
I tried to change the tooltip parameter but didnt find the solution.
View Replies
View Related