If Else / Switch Not Outputting Correctly
Jan 27, 2014
Trying to learn switch statements. Can't figure out what I am doing wrong.
java Code:
import java.util.Scanner;
public class Ok1 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
[code]...
When I run, it makes me enter letter = null and it wont output correctly.
View Replies
ADVERTISEMENT
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
Oct 29, 2014
I have a loop and I was wondering why it is outputting my line twice. Here is the code:
while (x != -1) // closes the program if the user enters -1 for x
{
System.out.println("Which jar would you like to access? Please select a number between 1 and 3. Press -1 to exit.");
x = scan.nextInt();
ourpantry.Selection(x); // tells our pantry class what number the user selected
while ((response != "continue")) // loop continues until user gives a valid response
[Code] ....
This results in the Output:
Would you like to spread jam? Y or N.
Would you like to spread jam? Y or N.
View Replies
View Related
Oct 14, 2014
I created a calendar program so when the user enters a day number and a year it would show the calendar for the year . My issue was that I could not get the numbers to line up neatly and how they should look . Also if its not to much trouble I would like to know how to turn this in to a loop .
import java.util.Scanner;
public class Calendar {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a Year");
int Year = scanner.nextInt();
[Code] .....
View Replies
View Related
Apr 22, 2014
My current assignment involves me outputting these 2 classes. Yet I'm not really sure in what manner I should go about doing this. I have tried creating a separate class and outputting my toString methods there but for some reason I am getting an error. .
The error message is thus;
Exception in thread "main" java.lang.NullPointerException
at Vehicle.toString(Vehicle.java:91)
at Run.main(Run.java:17)
Process completed.
import java.util.Calendar;
public class HireContract {
String reference;
Calendar startDate = null;
Calendar endDate = null;
Vehicle vehicle = null;;
String customerRef;
double rate;
[Code] .....
View Replies
View Related
Mar 30, 2014
I am currently working towards taking input from the user and storing it in an excel document. However how can I test to make sure that I am trying to save it to an empty cell. Is there some type of method that will check to make sure the cell is empty, otherwise how can this be done?
View Replies
View Related
Nov 9, 2014
I was struggling to use BufferedReader to extract some data and then perform some calculations and then have the results as outputs.
I haven't quite solved that issue but in order to progress, I hard coded some values into my application and proceeded with the actual calculation loops etc.
Currently, the value out put from one of my calculations is given as:
1.1704926E7
How can I make the console show it in a natural way. I've performed the calculation manually and it should be 11704926.5 I don't want to lose that .5!
View Replies
View Related
May 12, 2014
I'm having a bit of trouble outputting the difference between two array list
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public static void main(String[] args) throws Exception {
[Code] ....
This prints out something like
Test1
Test1
Test2
Test2
Test2
Test3
Test3
Test3
Test4
Test4
Test5
Test5
Test6
Test6
Test6
Ideally, it's suppose to print out only Test2, Test3, Test6.. I've tried different combinations of loops and equals() but I never get the correct output.
View Replies
View Related
Sep 27, 2012
I am using SAXTransformerFactory to output an XML file but can't figure out how to output the stylesheet setting, for example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ectd:ectd SYSTEM "util/dtd/ich-ectd-3-2.dtd">
<?xml-stylesheet type="text/xsl" href="util/style/ectd-2-0.xsl"?>
I am using the following code to get the first two lines, I just can't see how to output the third line above.
SAXTransformerFactory mTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = mTransformerFactory.newTransformerHandler();
Transformer mTransformer = handler.getTransformer();
mTransformer.setOutputProperty(OutputKeys.VERSION, "1.0");
mTransformer.setOutputProperty(OutputKeys.ENCODING, IBF_UtilXML.XML_ENCODING_UTF8);
mTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "util/dtd/ich-ectd-3-2.dtd");
I believe I found an example using a DOM approach but I would much prefer using SAX.
<?xml-stylesheet type="text/xsl" href="util/style/ectd-2-0.xsl"?>
View Replies
View Related
Feb 15, 2014
This is sort of like the last problem I had but it's all about the formatting at the end. I have a file that reads something like:
Name
a bunch of text here
and perhaps a second
or even a third line
I need to output this as:
Name; "a bunch of text here and perhaps a second or even a third line"
Right now I find 'Name' and it outputs:
Namea bunch of text here and perhaps a second or even a third line
How would I add ";" and quotes in the middle? I can imagine that I may need to find name, then skip to the next line and just add name manually as the variable output of the search string.
This is where I am now
public static void main(String[] args) {
boolean output=false;
String name="";
String junk="CHAMBERS";
TextIO.putln("Search for a name");
name = TextIO.getln();
TextIO.readFile("doc.txt");
[Code] .....
So I'm almost there, but those dange double Names are killing me. I tried to add a bit of code where:
if (line.indexOf(name) >= 0){}
View Replies
View Related
Oct 17, 2014
I am Having trouble with my program to validate. It is outputting null into the validation statement then it brings back a run-time error to that validation Statement for the String.
public String validateData ()
{
if (nm == null)nm = "Error! Must enter at least one character";
else nm = name;
return name;
}//end validation method
Why is this happening, and then once that is completed, why is the validation Sentence in tests Scores not able to validate. I traced it back to out put "Error, a number between 1<100".
public void validateTests ()
{
String testschange;
if (test1 < 0 || test1 > 100) {
testschange = " You have entered an invalid number, between 1-100. Please restart!";
testschange = Integer.toString( test1 ) ;
[Code] .....
View Replies
View Related
Apr 24, 2014
At the moment I am studying it and the problem is Taking a range of names in an array and outputting them alphabetically, But for some reason I get an outOfBoundsException. And its starting to get to me. How to put the code in
import java.util.Scanner;
import java.util.Arrays;
public class lab241 {
public static void main(String[] args) {
Scanner kb= new Scanner(System.in);
String[ ] names = new String[4];
names[0]= ("Horatio");
names[1]= ("Anatoly");
names[3]= ("Evelyn");
names[4]= ("Anna");
Arrays.sort(names);
for(String N: names){
System.out.println(N);
View Replies
View Related
Apr 23, 2015
I am building a program that when you enter 1. it allows you to setup an item. However running my code my second if statement runs through.
import java.util.Scanner;
public class InventorySystem {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int count=0;
int inputEntered=0;
int numberOfItems=0;
double cost=0.00;
String item;
String description;
[code]...
View Replies
View Related
Feb 13, 2014
So i am creating a File object which has a text file passed to it. I then try to do logic with it using a BufferedReader. However, I get a FileNotFoundException on the using my code below. The Error is on the BufferedReader line. I
Java Code: System.out.println("--Reading text file--");
File file = getFile(c,fileName) // Returns a File object.
System.out.println(file); // Shows me the file is looking correct. Displays contents to console.
BufferedReader br = new BufferedReader(new FileReader(file));
System.out.println("BUFFERED");
while((line = br.readLine()) != null) {
try {
// Do Logic
}
catch(Exception ex){
ex.printStackTrace();
}
br.close(); mh_sh_highlight_all('java');
View Replies
View Related
Sep 27, 2014
I want to write simple game. Why if I press button UP and RIGHT it not move diogonally, may be it have special code. And how I need to write correctly border and gravity?
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Stickman");
Ground ground = new Ground();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(710, 480);
[code]....
View Replies
View Related
Mar 25, 2014
I am trying to implement a game using the ACM graphics. For the game, I am trying to make the main frame a Grid of Cells.I made a Grid class which extends GCanvas:
import acm.graphics.GCanvas;
public class Grid extends GCanvas{
private final static int WIDTH = 300;
private final static int HEIGHT = 300;
private final int DIMENSION = 5;
Cell[][] grid;
[code]....
View Replies
View Related
Sep 4, 2014
If I run this code. I can manipulate it so that X wins. When the third X button is pressed, there is no display saying that X has won and that the game is over. I have read over this code for days and still have not figured it out.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe extends JFrame implements ActionListener, WindowListener{
public JFrame window = new JFrame("window");
[Code] ....
View Replies
View Related
May 26, 2014
I am currently trying to make a method that will enable a jLabel to be dragged across the screen. I want to be able to drag the object pretty much anywhere on the screen(I hope to eventually make a destination for it where it can be dropped but I will get to that later). I have looked on various places on the internet on how to achieve this and the most simple solution does something like this:
Java Code:
jLabel6.setText("jLabel6");
jLabel6.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
jLabel6MouseMoved(evt);
}
});
private void jLabel6MouseDragged(java.awt.event.MouseEvent evt) {
x = evt.getX();
[Code]...
This actually works somewhat but it is very faulty. For example the jLabel keeps appearing and reappearing as I drag it. Furthermore when I drag it I can see a duplicate Jlabel being dragged as well in the top left hand screen. So how might I fix this?
View Replies
View Related
Nov 29, 2014
I am trying to write a date program. I have it written and running correctly, except one issue. I cannot get the numerical date to display correctly. I know it is an issue with printf. I don't know anything about printf. Here is my method to return the day
public int getDay() {
return day;
}
When it returns, it is literally returning just the numeric date. I need it to show up as 02 or 03 instead of 2 or 3, respectively.
View Replies
View Related
Sep 8, 2014
the dates are printing out of order. It should appear MM/DD/YYYY however with my code its printing DD/YYYY/MM.Here is my code.
public class DateTest {
public static void main(String args[]){
Date date1 = new Date( 7, 4, 2004 );
System.out.print( "The initial date is: " );
date1.displayDate();
[code]....
View Replies
View Related
Mar 17, 2014
how to calculate the child's height in float value fixing value where if you choose male the accurate value, but if you choose female the value will be accurate too.
int heightMother, heightFather;
int heightMaleChild, heightFemaleChild;
String gender;
[code]....
View Replies
View Related
Dec 12, 2014
I'm unable to get the output for "AverageDriver" to print correctly(5 scores in descending order and average). The output displays the scores the same way they are entered and the average is always zero. The output for the Average program works fine and with the correct output.
import java.util.Scanner;
//This class keeps 5 scores entered by the user
public class Average
{
private int[] data;
private double mean;
public Average()
[Code] ....
View Replies
View Related
May 7, 2014
I cannot get the data file to print out correctly when I go to the display case. It only prints one name three times.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.Scanner;
import java.io.*;
import java.text.*;
import java.math.*;
import java.util.StringTokenizer;
[Code] ....
Output when I choose Case 1:
Minia
Minia Jane
Minia Jane
View Replies
View Related
Nov 6, 2014
My console SQL Query app is not giving me the desired output.I am sure its might have something to do with my SQL Query.Its missing the description of the stock items and it prints a new line for each user as per number of stocks an repeats the user details and then prints the stock listed by that user(only want the user listed once with all its stocks). I will add my files and current output and desired out put below.
MakeDB.java:
import java.sql.*;
import java.io.*;
public class MakeDB
{
public static void main(String[]args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:StockTracker";
[Code] .....
View Replies
View Related
Nov 9, 2014
I'm working on a homeowork assignment here URL... and I can't get the graph to work correctly. I'm not really sure how to print the string of names, gender and rank at the point on the graph. Also I'm just wondering if I set up the scanner to find names right.
import java.util.*;
import java.io.*;
import java.awt.*;
public class Names {
public static final int year = 1880;
public static final int decades = 14;
public static final int decadeWidth = 70;
[code]....
View Replies
View Related
Jan 21, 2015
I have two programs that talk to each other over a TCP connection. When I write the data "STX+1234" where STX is the Ascii character STX or Ctrl B and I expect the written String length to be 6 which it is. On the other side of the socket I create the ServerSocket and use the client socket's InputStream to create a BufferedReader. When I receive the string it now has 12 characters where each original character has been replaced by NUL and the character. How do I read the string as it is originally specified without the conversion? And is the problem on the reader side or the writer side?
View Replies
View Related