Array Prints Out Zeros?

Nov 16, 2014

Ok, so let's say I am having a user input scores and at the end I want to print out the results so I do something like:

int[] Scores = new int[1000];
//code to ask user for input and store into array //

The user only inputs lets say 5 scores out of possible 1000. I then try to print it out by doing something like this:

for(int counter = 0; counter < Scores.length; ++counter) {
System.out.println(Scores[counter])
}

How would I go about printing out only the index's that were input, because right now it prints out the scores and then 995 0's after.

View Replies


ADVERTISEMENT

Populate Array With Leading Zeros

Nov 18, 2014

why I need to populate an array with leading zeros. It sounds like it wants me to populate the entire array for one number (string). When I use next(), it separates the numbers so why do I need to go the leading zeros route?

This assignment will give you practice with external input files and arrays. You are going to write a program that adds together large integers. The built-in type int has a maximum value of 2,147,483,647. Anything larger will cause what is known as overflow. Java also has a type called long that has a larger range, but even values of type long can be at most 9,223,372,036,854,775,807.The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 50, so we will be able to store integers up to 50 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the front of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we're likely to add them like this:

38423
27

To simulate this right-shifting of values, we will store each value as a sequence of exactly 50 digits, but we'll allow the number to have leading 0's. For example, the problem above is converted into:

0000000000000000000038423
0000000000000000000000027

Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these.The data for your program will be stored in a file called sum.txt. Each line of the input file will have a different addition problem for you to solve. Each line will have one or more integers to be added together. Take a look at the input file at the end of this write-up and the output you are supposed to produce. Notice that you produce a line of output for each input line showing the addition problem you are solving and its answer. Your output should also indicate at the end how many lines of input were processed. You must exactly reproduce this output.

You should use the techniques described in chapter 6 to open a file, to read it line by line, and to process the contents of each line. In reading these numbers, you wont be able to read them as ints or longs because many of them are too large to be stored in an int or long. So youll have to read them as String values using calls on the method next(). Your first task, then, will be to convert a String of digits into an array of 50 digits. As described above, youll want to shift the number to the right and include leading 0s in front.

The String method charAt and the method Character.getNumericValue will be useful for solving this part of the problem.You are to add up each line of numbers, which means that youll have to write some code that allows you to add together two of these numbers or to add one of them to another. This is something you learned in Elementary School to add starting from the right, keeping track of whether there is a digit to carry from one column to the next. Your challenge here is to take a process that you are familiar with and to write code that performs the corresponding task.

Your program also must write out these numbers. In doing so, it should not print any leading 0s. Even though it is convenient to store the number internally with leading 0s, a person reading your output would rather see these numbers without any leading 0s.You can assume that the input file has numbers that have 50 or fewer digits and that the answer is always 50 digits or fewer. Notice, however, that you have to deal with the possibility that an individual number might be 0 or the answer might be 0. There will be no negative integers in the input file.You should solve this problem using arrays that are exactly 50 digits long. Certain bugs can be solved by stretching the array to something like 51 digits, but it shouldnt be necessary to do that and you would lose style points if your arrays require more than 50 digits.The choice of 50 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits.

Consider the input file as an example of the kind of problems your program must solve. We might use a more complex input file for actual grading. The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using BigInteger or BigDecimal. You must solve it using arrays of digits.Your program should be stored in a file called Sum.java.

Input file sum.txt
82384
204 435
22 31 12
999 483
28350 28345 39823 95689 234856 3482 55328 934803
7849323789 22398496 8940 32489 859320
729348690234239 542890432323 534322343298
3948692348692348693486235 5834938349234856234863423
999999999999999999999999 432432 58903 34
82934 49802390432 8554389 4789432789 0 48372934287
0
0 0 0
7482343 0 4879023 0 8943242
3333333333 4723 3333333333 6642 3333333333

Output that should be produced
82384 = 82384
204 + 435 = 639
22 + 31 + 12 = 65
999 + 483 = 1482
28350 + 28345 + 39823 + 95689 + 234856 + 3482 + 55328 + 934803 = 1420676
7849323789 + 22398496 + 8940 + 32489 + 859320 = 7872623034
729348690234239 + 542890432323 + 534322343298 = 730425903009860

[code]....

View Replies View Related

How To Trim Leading Zeros In Array Of Digits

Feb 4, 2015

I have an array made that represents digits and I am trying to make a method so that if there are zeros in front of the first significant digit I want to trim them, I understand you can't re size arrays so I have created a new array, but my code doesn't seem to run correctly? Here is my code I can't figure out what is wrong I've tried everything: (I put stars around the error**)

package music;
import java.util.Random;
public class Music {
private int length; // length of the array
private int numOfDigits; // number of actual digits in the array
int[] musicArray;

[Code] .......

View Replies View Related

Prints Every Minimum In Array

Jan 20, 2015

The second message dialog result is always 0 ... i want to find the minimum grade...

import javax.swing.*;
import java.util.Arrays;
public class Parrarrapapa{
public static void main (String[]args){
String length = JOptionPane.showInputDialog("Number of students");

[code]....

View Replies View Related

Program That Prints Repeated Integer In Array

Apr 8, 2015

So I'm trying to write a program that prints out the "most-repeated integer" in an Array.

For example: if an array contains {1,2,2,3} It would print out 2 as the result.This is what I got so far and according to my knowledge I think I'm correct but for some reason it doesn't work.. Please give me some inputs.

public class MostInt{
public MostInt (){
int[] array = {0};
for(int i = 0;i>array.length;i++){
if(i==i++){
System.out.println(i);

[Code]...

View Replies View Related

Create Circular Array Which Prints Out 8 Numbers That Increase By One And Don't Exceed 9

Jan 31, 2015

I'm trying to create a circular array which prints out 8 numbers that increase by one and don't exceed 9. If they do, the remaining numbers are printed from 0 on-wards. I have code below which does this job, but it doesn't really use an array to loop back.

Java Code:

package Practice;

public class Practice
{
public static void main(String[] test)
{
number(7);
number(9);

[code]...

View Replies View Related

Create Instance Of Array Of Several Integers And Prints Data Based On Methods

Apr 15, 2014

I have to make two classes. The first one crates an instance of an array of several integers and prints data (average, greatest, lowest, et cetera) based on the second class, which contains the methods. I'm having a problem with the syntax for the first class required to use the methods.

Here's a shortened version of what I have right now just based on processing the number of integers in the array (because if I can get just one method properly connected, I could figure out everything else).

Driver

import java.util.Arrays;
public class ArrayMethodsDriver
{
//Creates the ArrayMethods object
public static void main(String[] args)
{
int[] a = {7,8,8,3,4,9,8,7};

[Code] ....

When I try to compile this, I currently get the "class expected" error on the count part.

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

View Replies View Related

Code Only Displays Four Zeros

Jun 20, 2014

why this code only displays four zeros ?????

public class VargjetUshtrimi2 {
public static void main (String a []) {
int r[] = new int[11];
for (int i = 1 ;i < 10; i++)
{System.out.println( r[i] );}
}

View Replies View Related

Cannot Make Zeros Output

Apr 3, 2015

I am writing a code that requires a user to input a number, then output the individual digits and then add the sum of the digits. I have the entire program written, but I cannot figure out how to make zeros output as individual digits. If I input 400, it only shows 4 and not 4 0 0. Here is the code:

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

[Code].....

View Replies View Related

How To Change Zeros To Integers In Do-While Statement

Mar 31, 2014

How do i change the outcome to below? at the moment it is all 0? How I can clear some clouds in this area.

Competitor 1: 11111
===============================================

Competitor 2: 22222
===============================================

Competitor 3: 33333
===============================================

Competitor 4: 44444
===============================================

Competitor 5: 55555
===============================================

Here's the code:

case 2: System.out.println("Menu item 2");
for(int i=1; i<MAX_COMPS;i++){
System.out.print("Competitor " + i + ": ");
for (int j=1;j<MAX_SCORES;j++)
System.out.print(scores[i][j] + " ");
System.out.println("");
}

View Replies View Related

How To Separate Numbers In A File And Put Leading Zeros

Nov 15, 2014

I am writing a program that adds together large integers. I have to store each integer in an array of digits, with one digit per array element. Array length is 50 so integer is 50 digits long. I have to store numbers in right-shifting format with leading zeros. For example,

0000000000000000000038423
0000000000000000000000027

Sum.txt contains numbers to be added. There could be one or more numbers per line.each line must be read as string with next() since it's assumed to be a very long number. String of digits needs to be converted into an array of 50 digits. Method CharAt and Character.getNumericValue will be useful. All numbers in each line are to be added. There are no negative numbers and individual number might be 0 or answer might be 0. Answer is always 50 digits or fewer.

BigDecimal or BigInteger are not allowed.

I'm lost where it says to put number with leading zeros in a 50 room array. How do I add numbers after formatting numbers with leading zeros?

View Replies View Related

Decimal Point - Remove Leading Zeros

Jan 23, 2014

If I have 0123.45 string and trying to get 12345 as result. I am trying regex like below.

String s="0123.45";
s = s.replaceAll("^0*([0-9]+).*", "$1");

It gave result 123 as leading zeros removed that is one of the things that I want. How do I achieve 12345?

View Replies View Related

Postal Code - Trimming Leading Zeros

Jun 10, 2014

I got situation where i have postal code as 0009 in database and the use is entering 0009 but somehow in my java code it only reading 9 from the xml file

This is how i define getter and setter

When i debug the code i get

passed postalcode 9

Java Code:

public String Postalcode="";
public void setPostalcode(String Postalcode) {
this.Postalcode = Postalcode;
}
public String getPostalcode() {
return Postalcode;
} mh_sh_highlight_all('java');

What if 0009 is passed i what it to remain 0009

View Replies View Related

Place Leading Zeros Onto Numbers Without Having To Convert Them To Strings

Jan 21, 2014

Any way to place leading zeros onto numbers without having to convert them to strings? Is such a thing possible?

View Replies View Related

Postal Code In Database - Leading Zeros Been Trim

Jun 10, 2014

I got situation where i have postal code as 0009 in database and the use is entering 0009 but somehow in my java code it only reading 9 from the xml file
 
This is how i define getter and setter :
 
When I debug the code i get this :

passed postalcode 9
 
if a user entered 0009 I what it to remain 0009
 
Java Code:

public String Postalcode="";  
public void setPostalcode(String Postalcode) { this.Postalcode = Postalcode; } 
public String getPostalcode() { return Postalcode; }

View Replies View Related

PrintWriter Only Prints Last Line?

Dec 6, 2014

I'm tyring to print the same output in console to a text file, but I can only get the last line of the console output in the text file, not sure what is wrong with my code:

while (in.hasNextLine()) {
PrintWriter writer = new PrintWriter("output5.txt");
tempS = in.nextLine().toLowerCase();
System.out.println(wp.bestPages(tempS));

[code]....

What's causing only the last time to be printed in text file? Are there better ways to print console outputs into a text file than PrintWriter?

View Replies View Related

Program Prints 2 Values Instead Of One

Feb 4, 2015

When I run this code, it is supposed to get one value from turnTimer(); and return it, just as a test. This works when I enter a valid pit. For example. If I were to input "A" when it's player one's turn, it will return 1, like it should. However, if I were to type "H" when it's player one's turn, it returns "Not a valid pit!"(like it should) but then it also returns 12. It shouldn't know that H is 12 because it's in a separate method. I'm confused as to why it's printing both values.

import java.util.*;
public class Mancala {
static Scanner input = new Scanner(System.in);
public static int pit;
public static void main(String[]args) {
Mancala mancala = new Mancala();
int[] board = {0,3,3,3,3,3,3,0,3,3,3,3,3,3};

[Code] .....

View Replies View Related

Deleting From ArrayList / Goes Through Both If And Else And Prints

Dec 8, 2014

I've a ArrayList with dogs on, and I've a function that should allow me to delete a dog from the register and if the dog was found it prints "The dog was deleted" and if the dog doesn't exist in the arraylist it should print "the dog couldn't be found".

Everything works perfect until I shall delete a dog that is not first on the list. Then the program shows first "dog was not found" and on the row after "the dog was deleted" if the dog was second on the list. If it was third I will get 2 messages that the dog wasn't found and then that the dog was deleted. I've no clue why it prints both else and if!

public static void taBortHund(){
//Har tar vi bort hund fran listan
//System.out.println(hundlista);
System.out.print("Vilken hund vill du ta bort? ");
String hunden = tangentbord.nextLine();
for (int taBort = 0; taBort<hundlista.size(); taBort++){

[code]....

View Replies View Related

Program Is To Accept Number And Display New Number After Removing All Zeros

Dec 29, 2014

ex
Sample input:2340980
Sample Output:23498

this is the program i have tried but is dosent work

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

[Code]....

View Replies View Related

How To Make A Table That Prints Out Sin / Cos / Tan For Angles

Sep 30, 2014

I am stuck and having a problem trying to find what i believe is a simple fix but can't wrap my head around it. For class I needed to make a table that prints out Sin,Cos,Tan for angles in steps of 5 to 180. The problem is in my output with the first couple lines of the table. for angle 5.0 the Sin(.0872) Cos(.9962) and Tan (.0875) are being displayed on the angle line 10.0

public class TrigTable {
public static void main(String[] args) {
double angle = 0;
double sin =0;
double cos = 0;
double tan = 0;
 
System.out.println("Angle Sin Cos Tan"); // Table header for the values

[Code] ....

Here is the first couple lines of output:

Angle Sin Cos Tan
----- --- --- ---
0.0 0.0 1.0 0.0
5.0 0.0 1.0 0.0
10.0 0.0872 0.9962 0.0875
15.0 0.1736 0.9848 0.1763
20.0 0.2588 0.9659 0.2679

View Replies View Related

How To Use Logger And Prints Time And Date

Feb 5, 2015

I need to add Logger function in the catch block that prints time, date and description of the what had happened.

public class InputUtil {
public static final String[] SPECIAL_CHARACTERS = { "!", "#","%", "^", "=",
"+", ";", "[", "]", "|", "<", ">", "?", "~", "`", "-", "" };
public static String deleteSpecialCharacters(String inputString) {
String outputString = inputString;

[Code] ....

View Replies View Related

Program Prints 2 Values When It Should Only Print 1

Feb 4, 2015

When I run this code, it is supposed to get one value from turnTimer(); and return it, just as a test. This works when I enter a valid pit. For example. If I were to input "A" when it's player one's turn, it will return 1, like it should. However, if I were to type "H" when it's player one's turn, it returns "Not a valid pit!"(like it should) but then it also returns 12. It shouldn't know that H is 12 because it's in a separate method. I'm confused as to why it's printing both values.

import java.util.*;
public class Mancala
{
static Scanner input = new Scanner(System.in);
public static int pit;
public static void main(String[]args)
{
Mancala mancala = new Mancala();
int[] board = {0,3,3,3,3,3,3,0,3,3,3,3,3,3};

[code]....

View Replies View Related

Create Program That Prints Time Between 00:00 (0:00 Am) And 23:45 (11:45 Pm)

Feb 8, 2014

I was told to create a program that prints the time between 00:00 (0:00 a.m.) and 23:45 (11:45 p.m.) in the 24-hour clock and 12-hour clock format like this:

24-hour Clock 12-hour Clock
-----------------------------
00:00 0:00 a.m.
00:15 0:15 a.m.
00:30 0:30 a.m.
00:45 0:45 a.m.
01:00 1:00 a.m.
01:15 1:15 a.m.
01:30 1:30 a.m.
01:45 1:45 a.m.
02:00 2:00 a.m.

ect...

but cant seem to create the program and my program doesnt seem to run.

View Replies View Related

Write A Method That Prints Characters

May 7, 2014

Write a method that prints characters using the following: public static void printChars(char ch1, char ch2, int numberPerLine). This method prints the characters between ch1 and ch2 with specific numbers per line. Characters are separated by exactly one space.Test your method with the following main method:

public static void main(String[] args)
{
printChars(‘A’,’z’,10);
printChars(‘0’,’9’,5);
}

View Replies View Related

System Prints Final Statistics Many Times

May 25, 2014

I made a heads or tails game but I'm getting a bug when the user says he doesn't want to play anymore. The statistics are printed as many times as the games played. If you want to test the code, write" cap" " pajura" and when the program says "Vrei sa mai joci?" that means "do you wanna play another one?" and you can answer with "da"(yes) or "nu"(nu) Here is the code:

import java.util.Scanner;
public class cap_sau_pajura{
private static int user;
private static int pc;
private static String converted;
static int usermove;
private static int castiguri = 0;
private static int pierderi = 0;

[code]....

View Replies View Related







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