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 {
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.
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.
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.
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 );
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.
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
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. */
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
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); }
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.
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: ");
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?
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.*;
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();
I'm trying to print sentences from a list which read in sentences from a text file. I'm able to get the program to print out the sentences in a numbered format corresponding to each sentence, but the first sentence in the list is always repeated, same as the next sentence. How can I print out the contents of a list in a numbered format (1. _ 2. _ 3._) without repeating sentences?
Right, so I got this method that creates and sorts 2 lists. What I want to do is merge these lists into a third list, have it sorted, and then print the contents of the list. The problem is, I'm tired and I don't remember how I can print it.
import java.util.*; public class MergeTwoSortedListWilson { public void CHANGEME() { // To do ArrayList<String> aList = new ArrayList<String>(); aList.add("Banana");
[Code] ...
MergeTwoSortedListWilson.java:35: error: cannot find symbol System.out.println(aList); ^ symbol: variable aList location: class MergeTwoSortedListWilson 1 error
I have a for cycle in which I have a line for printing in the output file. But the file remains empty.
I inserted ShowMessageDialog in the same cycle in order to see what are the results of the calculations that are supposed to appear in the output file, and I see that the results are fine.
I am having some trouble with this program. I am getting only one result to print when it should show all the solutions. Also the 1 solution I am getting is only printing 7 queens not 8.
import java.util.Stack; public class Queen1 { boolean conflict, complete = false; public int solve(int n) { //create a stack //each element stores the position of the queen on a different row Stack<Integer> s = new Stack<Integer> ();
We had to use these methods with the given parameters and code them correctly. We cannot use any built in java methods for sets.
import java.util.Scanner; public class setPractice { public static Scanner kbd; public static final int MAXSIZE = 20; public static void main(String[] args) { kbd = new Scanner(System.in); int[] setA = new int[MAXSIZE]; int[] setB = new int[MAXSIZE]; int[] intersect = new int[MAXSIZE];
[Code] ....
This is the input given to the program:
How many numbers will be in the 1st set: 3 Enter list of integers for 1st set: 12 3 2
The ascending order for 1st is: 2 3 12
How many numbers will be in the 2nd set: 4
Enter list of integers for 2nd set: 2 3 6 1
The ascending order for the 2nd set is: 1 2 3 6
The intersection of the two sets is: 0 0 0 //Program not correctly print intersection of two sets
The difference of A-B is: //Program is not printing the difference either...