Storing Months In Array And String Datatype

Oct 30, 2014

public class MonthsName
{
static String monthsName[];
public static void main(String[] args) {
String[] months = new String[13];
months[0] = null ;

[Code] .....

I keep getting 1 error that tells me this.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MonthsName.main(MonthsName.java:22)
Java Result: 1

Just before that netbeans couldn't find the main class. I was wondering if it netbeans or my code that's wrong.

View Replies


ADVERTISEMENT

Storing A String In A 2D Array?

Sep 24, 2014

One of the requirements of my programming assignment is that a user enters a command along with two numbers. For example, if I entered the following command:

I 5 6

The program create an image like this (it wouldn't be outputted though):

00000
00000
00000
00000
00000
00000

It creates a 5 x 6 "image". This is where my troubles begin. The program should also accepts other commands, such as:

L 3 2 F

which would produce (also not outputted):

00000
00F00
00000
00000
00000
00000

Here is my method for creating an "image" with an M * N array

for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
System.out.print("0");
} System.out.println();
}

The code works, but I need to store the image in an array so it can be changed by other methods (I can't create the image manually every time). I tried doing something like this but it doesn't work:

public static String[][] createImage(int row, int column) {
String[][] image = new String[row][column];
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
image[j][i] = "0";
} System.out.println();
} return image;
}

This method outputs as many blank lines as the columns I entered and the memory location of image.

So my question is: how would I store "0" in a 2D array so that it can be accessed and changed by other methods?Or, am I using a 2D array incorrectly and will the image have to be created manually every time? If so, how would I output image if it is created in a separate method?

View Replies View Related

Split String And Storing It In Array Of Strings

May 10, 2014

String str = "#11* 1# 2*12# 3"

required o/p in array as

#11

* 1

# 2

*12

# 3

i.e splitting the strings for every 3rd string and storing it in an array of strings ...

View Replies View Related

Program To Store Total Rainfall For Each Of 12 Months Into Array Of Doubles

Oct 26, 2014

"Create a project called RainFall and a class nameD RainFall. Write a program that stores the total rainfall for each of 12 months into an array of doubles. The program should display total rainfall for the year, the average monthly rainfall, the month with the most rain and the month with the least rain. When outputting the month with the most and least rain, output the name of the month. This is what I have so far.

Scanner keyboard = new Scanner(System.in);
double averageMonthly;
double mostRain;
double leastRain;
int months;
double monthlyRain = 0;
double totalRain = 0;

[Code] ....

View Replies View Related

Storing A String Into Object

Dec 3, 2014

I have the following method that I need to implement:

{
// YOUR CODE HERE
File file = new File(filename);
int counter = 0;
String tempArtist, tempName;
Album tempAlbum;
Track tempTrack;

[code]....

Ok so I updated my code from the initial post since I made some progress on my own. I guess now I'm just stuck on how to scan in the file of strings and stock it into the type Track. (I've tried using both the initial linked list for this and a temporary variable with no luck).

View Replies View Related

Storing And Scanning String In Java?

Jun 10, 2014

I am building an app, where I have to store the data eg: "hello ! how are you" in java as a string and then use scanner to get input and check if the entered word is present in the line stored. If it is stored then the entire sentence must be displayed. eg : if the stored string is "hello how are you"

if the entered string is "how", then the entire sentence "hello how are you" should be displayed.

View Replies View Related

Warning While Using Boolean Datatype In Program Execution Success

Oct 26, 2014

I am getting notification about "The value of the local variable result not used" in my program when i am using boolean data type.But code execution was successful. Results are also displayed accurately.I am using Eclipse, there i i have noticed this warning.

* program to practice about comparison operators */
public class compoperators {
public static void main(String[] args) {
boolean result;
}

[code]....

View Replies View Related

Getting User Input From TextArea And Storing It As String Value

Jan 20, 2015

I am having trouble with getting a user input once they enter it in to a JTextArea and then storing it in to a string value. See, I wanted the user to enter an answer in to the JTextArea, and then once I store it in to a string value, I would be able to use it later. I wanted to after storing the variable to print it on to a JLabel, but nothing is showing up... Heres my code.

static String usera[]= new String [10];
for (int i=0; i< usera.length; i++) {
//These are the JTextArea names
usera[0]= Question1.getText();
usera[1]= Question2.getText();

[Code].....

I already initialized the JLabels earlier in my code, and I did not feel the need to post all my code here. This is where I am trying to get the string variable and then printing it out.

View Replies View Related

HashMap - Turning String Into Integer And Storing Accordingly

Oct 2, 2014

When learning HashMaps in C++ I had to create the whole algorithm. In the code I created I could simply place a string into the method and it would store the names for me by turning the string into a integer and storing is accordingly. If there was a collision it would grow linearly at that location.

//play with Hash Tables
void getNames(String names) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put(names,22);
}

How can I do this in Java. I read about them and look at examples and they all for the most part look like this.

View Replies View Related

Storing Biginteger In Array?

Apr 1, 2014

how can i store biginteger in an array?

View Replies View Related

Storing Set Of Numbers In Array?

Oct 9, 2014

I trying to write a piece of code which takes a set of numbers or even data type and stores in an array (or something more suitable).

For example:

Date N1 N2 N3 N4 N5 Day
10/11/2012 10 09 32 100 15 Thursday
11/12/2013 01 0 0 23 50 51 Tuesday

I'd like to be able to sort them so that if I want to search for one of the entries, I can then create a function which allows me to sort them by date or even return all the numbers by a given date or day, etc....

However, I'd like to be able to set it up so that each set is "linked" meaning that again that I can search by date and it returns everything at that date.

I wanted to use an array but I don't know:

-How to do it?

-Whether this is a suitable approach?

View Replies View Related

Storing Input From User As String Variable And Then Converting It To Double?

Jul 8, 2014

I am trying to make a program that calculates the change due in dollars and cents. The user inputs both the amount due and the amount tendered. My program only works with whole numbers?

View Replies View Related

Splitting Values From A File With String Tokenizer And Storing It In Another Class

Aug 26, 2014

I'm new to java. I have a Product class with getters and setters.

E.g. setProdType & getProdType

I want to store the values from a file into that

StringTokenizer token = new StringTokenizer(line,"**");
while(token.hasMoreElements()) {
int p.setProdType = Integer.parseInt(token.nextElement().toString());
}

View Replies View Related

3 By 3 Array And Storing It In 2 Dimensional Array

Apr 17, 2014

I am having trouble fixing and figuring out how to change my code. My out put is very off.

"
Enter a 3-by-3 matrix row by row:
1 1 1
1 1 1
1 1 1
Sum of the major diagonal is 6.0
Sum of the values of the column are: 10.0
Sum of the values of the column are: 20.0
Sum of the values of the column are: 30.0 "

Code is below:

public static void main(String[] args) {
double[][] m = new double[3][3];
m = createArray();
}
private static double[][] createArray() {

[Code] ....

View Replies View Related

Storing Multiple User Inputs In Array

Feb 28, 2015

I need to generate a league table in Java based on results provided in the shape of user input. I have the 6 teams in an array. I need to ask the user how many wins, draws, losses each team has had.

for (String element : teams) {
System.out.println(
"Please enter the number of wins, draws and losses for " + element);
int[] wins = new int [6];
int[] draws = new int [6];
int[] losses = new int [6];
}

How do I pull 3 separate values from the user and then store each value in a separate element of each array? so if one team wins 3, draws 2 and loses 1 - I need to put 3 into the wins array, 2 into the draws array and 1 into the losses array? I then need to total all this in a table.

View Replies View Related

Storing Data In Array And Printing To Screen

Jun 2, 2014

My assignment was to create a class and then write a program that uses the methods of the class I created. All the output is correct except for the Question # Missed. It prints [I@1dd0fe7. The wrong question numbers have to be stored into an array and that array printed. I've tried using a for loop to print, rearranging the code, setting the code to how I did in the other methods. I either get this [I@1dd0fe7 -or- a bunch of 0's. I'm not sure if I am calling the method incorrectly, or just do not have the correct set-up to print the array, or if I am just not grabbing the ones that are incorrect.

public class DriverExam {
// Fields
private boolean result;
private int testTotal;
private int numWrong;
private int [] numMissed;
private char [] answerKey;

[Code] ....

View Replies View Related

Reading Records From TXT File And Storing It Into Array

Apr 15, 2014

I am reading records from a txt file and storing it into an array

import java.util.*;
import java.io.*;
 public class PatientExercise {
//patients exercise time
public static void main (String[]args) throws IOException{
Scanner in = new Scanner(new FileReader("values.txt"));
double [] patientTimeRecords = new double [300];
int noExerciseCount=0, numPatients =0;
double highest=0, lowest=0, avg=0, totalTime=0;
 
[Code] ....

However an error msg keeps popping up:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at pastpapers.PatientExercise.main(PatientExercise.ja va:44)

line 44 is:patientTimeRecords[i]= in.nextDouble();

View Replies View Related

Storing Multiple Phone Numbers Into Single Array

Jul 9, 2014

This particular program is supposed to prompt the user to input either an uppercase or lowercase Y to process a phone number, they are then prompted to enter the character representation of a phone number (like CALL HOME would be 225-5466), this repeats until the user enters something other than the letter Y. All of the words entered are to be stored into a single array. The program is then to convert these words in the array to actual phone numbers.

import java.util.*;
public class Program1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String begin;
int phoneNumber = number.convertNum();
System.out.println("Please enter an uppercase or lowercase Y when you are ready to enter a telephone number: ");

[code]....

I realize that the second method doesn't have anything to return yet, I just wanted to put that in there while I was doing things that I actually know how to do ha. The convertNum() method is supposed to be the method with which the array of characters is converted to phone numbers.

would think it'd be easier to store the inputs from the user as individual letters rather than words for the sake of converting to phone numbers.

Also, we are only supposed to recognize the first 7 letters of each word that is entered, like the CALL HOME example, there are 8 letters but it's still a seven digit phone number, so I'm not sure how that would work.Also, when printing the phone numbers after they've been converted, we're supposed to print the hyphen after the third number, I have no clue how that would be done from an array.

View Replies View Related

Binary Tree - Storing Each Word As String And Int Frequency For Each Time Word Occurs

Apr 27, 2015

I have built a binary tree, from a file. In each node, I am storing each word as a string, and an int frequency for each time the word occurs. For the assignment, I need to find how many words occur only once in the file. I wrote the program, but for some reason I am getting a number different from what my professor is expecting.

As far as I know, this file has loaded into the tree correctly, because all of my other answers in the assignment are correct. What am I doing wrong?

public void findUnique() {
System.out.println("There are " + findUniqueWords(root, 0) + " unique words.");
}
private int findUniqueWords(Node subTree, int uniqueCount) {
// Base Case: At the end of the branch
if(subTree == null){
return uniqueCount;

[Code] ....

View Replies View Related

How To Calculate Total Age In Months And Store As Variable

Feb 9, 2015

I was wondering how can I calculate total age in months and store it as variable? this is what I have so far:

*
* You will ask the user to provide their name (as a single word, assume first name here)
* You will ask the user their age in full years (no fraction or months)
* You will ask the user how many full months since their last birthday (again, whole numbers here).
*
* You will use this information to calculate the user's age in months and use that information to print out a greeting.
*/
import java.util.Scanner;

[code].....

View Replies View Related

A Program That Stores Total Rainfall For Each Of 12 Months

Apr 29, 2015

Write a program that stores the total rainfall for each of the 12 months into an array of doubles. The program should have methods that return the following:

The total rainfall for the year
The average monthly rainfall
The month with the most rain
The month with the least rain

And the code:

double rain[] = new double [12];
double sum;
double average;
double most;
double least;

[code]....

I'm having a problem with figuring out how to show which month is the least and most.

View Replies View Related

Iterating Through Months While Keeping In Mind Different Length Of Month

May 20, 2015

I have to create a method to find the current month with the input being a number of milliseconds using System.currentTimeMillis(). Now I was able to solve the problem with the following extremely cumbersome method:

public static int monthLeap(long ms) {
int result = 0;
int jan = 31, mar = 31, may = 31, jul = 31, aug = 31, oct = 31, dec = 31;
int apr = 30, jun = 30, sep = 30, nov = 30;
int feb = 28;
int febLeap = 29;

[Code] ....

I first go through a loop that goes through all the years and subtracting the number of days of a normal year or a leap year in milliseconds from the total milliseconds. At the end I should have a rest value of milliseconds that represents the amount of milliseconds that have passed already this year. Out of this number I then find the month we are currently in.

My problem is that the above method is way to large and I get a checkstyle warning: "NPath Complexity is 12,288 (max allowed is 80)".

I know that this can somehow be solved with a for loop iterating through the time and counting up months until there is no more month left. My problem though is the different lengths of the months. If each month was the same I could just subtract the amount of days in each month from the rest value.

(Since I already got it down to <= 12 months, I shouldnt bother with the extra days to find the month. Yet if I want to find the month when the date is the first or last day of the month it is important to be very precise) ....

View Replies View Related

Program That Calculates How Much Employees Earn Over A Period Of Time In Months

Oct 5, 2014

i am very new to java programming what i am doing wrong here. Write a program that calculates how much an employee would earn over a period of time (in months), if, every month, the employee’ pay-per-hour rate is one dollar more than the month before (so if his starting perhour rate is 7.25, next month it will be 8.25, next month 9.25, and so on). The employee is going to work 20 hours per week, 4 weeks per month.

The program should input from the user and validate both number of months (which should be a positive integer larger than 0) and the pay-per-hour rate (which should be a floating-point number larger than $7.25 – the minimum federal wage pay-per-hour rate). For each one of these values; if the value is wrong, the program should repetitively ask for that value until the value entered is correct. The program should confirm/output the correct value.

/*
This program calculates how much as employee would earn if every month the employee pay per hour would increase by a dollar
*/
package nick.employee;
import java.util.Scanner;
public class nickemployee {

[Code]...

what i am doing wrong

View Replies View Related

Converting Days Into Weeks / Months / Years - Invalid Method Declaration

May 29, 2014

import java.util.Scanner;
public class days
{ public EnumTest ()
{ this.day = weeks/months/years}
final int daysInMonth = 30; //constants
final int monthsInYear = 12;
final int daysInWeek = 7;
public void convert()

[Code] ....

after compiling it shows invalid method declaration ; return type required.

View Replies View Related

Accepting String And String Array In Just ONE Method?

Mar 18, 2014

Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.

public class test {
public static void main(String[] args) {
Printhelloworld();
String[] verbs = {"go", "do", "some", "homework"};
printArrays(verbs);

[Code] .....

View Replies View Related

How To Compare String To Each Element Of String Array

Mar 28, 2014

How do I compare a String to each element of a string array?

For example:

int headscount = 0;
if (coins[i].equals("heads")){
headscount++;
System.out.println("b" + headscount);
}

This doesn't give me the right value because the IDE says that equals() is an incompatible type. I also tried changing the "heads" to an variable, but the results remains the same.

I would prefer using an Array!

View Replies View Related







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