Counting Input Array - For Loop Statement

Mar 13, 2014

I have to take user input and then count how many times each number that the user input and print each one out. For some reason, I can't even get the for loop statement to print and it's pretty much the same as my other program except for the loop which is a little different.

//User inputs numbers between 1 and 100, program counts how many of each integer is and ends with a 0
 
import java.util.Scanner;
 public class occurrence {
 public static void main(String[] args) {
 //scanner/values
Scanner input = new Scanner(System.in);
int number = 0;
int c = 0; //array count
 
[Code] ....

View Replies


ADVERTISEMENT

Use While Loop With Input And If Statement For Output

Apr 26, 2015

Here is my code so far. I am trying to get the WHILE LOOP to work so the user inputs a number, the if statement prints the output and then it returns to ask for another number and goes again and again looping :

import java.util.Scanner;
public class ifwhileloop {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
double nmbr;

[Code] ....

View Replies View Related

Creating A Loop For Counting Integers

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

Counting Number Of Words From User Input

Jan 2, 2015

I tried the following code, OK:

Java Code:

String temp = "hi this sf hello is new what is this";
String[] cmd = temp.split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

However, when i get the input from user , i didnt get the expected result:

Java Code:

System.out.println("Enter the input string to count the words: ");
String[] cmd = new Scanner(System.in).next().trim().split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

Now Result for above code:

Enter the string to count the words:

hi this is new

words are: 1

View Replies View Related

Counting Duplicates In Array

Jan 23, 2013

It's supposed to count all of the duplicates in an array and print out how many occurrences of the value starting at whatever index, or if there are no duplicates state that. Basically:

No duplicates with value 1 beyond Index 0

There are 3 more occurrences of value 2 starting at index 1

There are 2 more occurrences of value 2 starting at index 2....

This is what I've got so far:

Java Code:

public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};
for(int i = 0; i<arr.length; i++){
int count = 0;
for(int j = i+1; j<arr.length; j++){
if((arr[j] == arr[i]) && (i!=j)){
count++;
System.out.print("There are " + count + " more occurrences of ");
System.out.println(arr[i] + " starting at index " + i);
}
}
}
} mh_sh_highlight_all('java');

View Replies View Related

GUI Log - IF Statement / For Loop

Nov 29, 2014

I've done a basic login GUI for a application, a user enters their name and address and clicks register, a four digit pin is then randomly generated, the pin and name and address are all added to an object of the MemberDetails class , this obj is then added to an array list.

returning customers can then enter a previously generated pin and either get a welcome back message or an invalid pin message.

my issue if i register two new users and get their generated pins, that on the returning members screen it will only show the welcome message for the second new customer i created and not recognizing the first customers pin. heres my code:

taking in new customer details, generating pin , adding obj to arraylist

int randomPIN = (int) (Math.random() * 9000) + 1000;
members = new MemberDetails();
members.name = nameNew.getText();
members.address = addressNew.getText();
members.pin = randomPIN;

[Code] ....

that lest method is a bit of a mess i know, i was going to store the log in attempts in a array but thats probly not a good tactic, and that inner for loop is probly unnecessary ?

View Replies View Related

Counting How Many Times A Value Appears In Array

Nov 23, 2014

I have this code but I can't seem to get it to work. It keeps saying that "count" cat be found and that it cannot return a value whose type is void.

Java Code: public class Cuantos {
static int getPosition(double listOfValues[], double targetValue ) {
int i,count,
position = -1;
for (i=0; i < listOfValues.length; i++) {
if (listOfValues[i] == targetValue)

[code]....

View Replies View Related

Counting In Multidimensional Array In Java?

Dec 19, 2014

I have to write a program that will read a picture and then print out the number of blocks inside it.I have to read the picture as a binary matrix of the size r c (number of rows times number of columns).The blocks are groups of one or more adjacent elements with the value 1.

- Blocks are built exclusively of elements with value 1
-Each element with value 1 is a part of some block
-Adjacent elements with value 1 belong to the same molecule.

We only take into account the horizontal and vertical adjacency but not diagonal.

INPUT:

In the first line of the input we have the integers r and c, separated with one space. Then we have the r lines, where each contains s 0's and 1's.The numbers inside the individual lines are NOT separated by spaces.The OUTPUT only print the number of blocks in the picture.

Example:

INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100

OUTPUT:
6

THIS IS WHAT I CAME UP SO FAR:

import java.util.Scanner; 
class Blocks{ 
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
 
[code]...

View Replies View Related

Changing For Loop To While Statement?

Aug 23, 2014

I am having trouble doing a simple exercise changing a for loop to a while statement, it works fine in the for loop but in the while statement it just prints out 10(it is supposed to count to 0 and print liftoff).

public void run() {
int i = 10;
while (i>=1) {
println(+i+"...");
i--;
}
println("liftoff..."); 
 }

View Replies View Related

Looping Through While Loop With Switch Statement?

Sep 23, 2014

I am having a problem with looping a while loop that contains a switch statement. Here is my code:

package hw4jenningsd;
import javax.swing.*;
public class HW4JenningsD {

[Code].....

View Replies View Related

Can Insert While Loop In Switch Statement?

Dec 8, 2014

I am trying to add a while loop into my switch statement. If you run the program, it will ask to enter the class grade (9,10,11, or 12). If you insert 5, it will say to try again. But, if you enter a wrong number twice, it will continue on to the next part of the program, which is asking how many students are in the class. Therefore, I believe a while loop would work, but it is not working at all. The program still runs, just doesn't fix the error. The program is below:

import java.util.Scanner;
public class stephProject {
public static void main(String[] args) {
//call method
welcomeMessage(); //method 1 of 3

[Code] ....

View Replies View Related

If Statement Won't Work Inside A While Loop

Dec 12, 2014

I made this calculator in C++ and it worked wonderful so I decided to make it in java. When I run the program it gives me no errors but my if statements inside my while loop don't work

import java.util.Scanner;
public class ohmlaw {
public static void main(String args[]) {
float current;
float resistance;
float voltage;
String calchoice = new String();
Scanner cc = new Scanner(System.in);

[code]....

View Replies View Related

Switch Statement Inside While Loop

May 5, 2015

cannot break from while loop. Whenever I am trying to exit from startCustomerManagement-> backEnd() -> mainScreen()..It gets stuck between mainScreen and backEnd screen. However I can exit from backEnd()->startCustomerManagement() screen

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication19;
import java.io.BufferedReader;

[code]...

View Replies View Related

Program Involving For Loop And If Statement Not Working

Oct 23, 2014

Alright so I'm trying to write a code that implements a for loop and if statements that displays any number from 100-200 if the number is divisible by either 5 or 6 in rows of ten numbers each row. If it is not divisible by that number then it should go back to the beginning of the loop until it reaches 200. My main problem is that it doesn't display anything. I don't get any errors or anything but every time I run the program it just displays nothing. Sample output is at the bottom of the code.
 
public class Exercise5_11 {
public static void main(String[] args) {
int count = 0;
int i = 100; 
//for (the numbers from 100 to 200)
for (i = 100; i>100 && i<200; i++){
 
[Code] ....

/*Output
100 102 105 108 110 114 115 125 126 130
132 135 138 140 144 145 155 156 160 162
165 168 170 174 175 185 186 190 192 195
198 200
*/

It is still wip of course so I was trying to just get it to display int i but it doesn't do anything and I'm not really sure why.

View Replies View Related

Program Ask For Five Names Using Loop Statement And Print All

Jan 24, 2014

write a program that would ask for the five names using loop statement and print all names .

View Replies View Related

Counter Controlled Loop With A Switch Statement

Sep 28, 2014

I'm trying to create a switch statement inside of a counter controlled (while) loop that asks for an input of...

"How many characters would you like to convert?"

Then you type in a number > 0, and then it should convert letters into the "1337" equivalent.

This is an example on what it should do.

How many characters would you like to convert? 5
Enter character #1 to convert: !
!-
Enter character #2 to convert: $
$-
Enter character #3 to convert: #
#-
Enter character #4 to convert: *
*-
Enter character #5 to convert: ,
, -

Whenever i try to run the program, i only get the- How many characters would you like to convert- i input 5 but then nothing else prints..

My codes..

System.out.print ( "How many characters would you like to convert?: " );

int convertCounter = 1;
char ch;
ch = input.next().charAt(0);
while (convertCounter > 0)
switch ( ch ){

[Code] ....

View Replies View Related

Counting Repeated Instances - Parallel Array Is Not Working Correctly

Sep 6, 2014

So in my parallel array i read from a textfile of strings and if i enter the string into the string array and if strings are repeated i store it in a parallel array that counts repeated instances. I'm supposed to get 27 15 21 23 20 but instead i get 106 0 0 0 0....

Scanner sc=new Scanner(new File("Cat.txt"));
String category=sc.nextLine();
int total=sc.nextInt();
int[]totcat=new int[total];
String[]names=new String[total];
while(sc.hasNextLine()){

[Code] .....

View Replies View Related

Java Statement Getting Executed Multiple Times Outside For / While Loop

Aug 14, 2014

I am trying to execute the following method: This method takes a delivery date as one of the arguments and a list of calendar holidays as another argument. If the delivery date is present in the holiday list, I will add 1 day more to the delivery date and check again if the new delivery date is part of holiday list.

Issue is that the last 3 statements before 'return' are getting executed multiple times though there is no for or while loop. Why they are getting invoked multiple times.

@SuppressWarnings("rawtypes")
private String fetchNextWorkingDay(String sDeliveryDate, Element eleCalendarDayExceptions, SimpleDateFormat sdf, Format formatter) throws Exception {
System.out.println("");
System.out.println("Inside fetchNextWorkingDay method");
System.out.println("Del Date to compare is "+sDeliveryDate);
Boolean isDateSet = true;

[Code] .....

View Replies View Related

Read Picture And Print Out Number Of Blocks - Counting In Multidimensional Array

Dec 19, 2014

I have to write a program that will read a picture and then print out the number of blocks inside it.

I have to read the picture as a binary matrix of the size r - c (number of rows times number of columns). The blocks are groups of one or more adjacent elements with the value 1.

- Blocks are built exclusively of elements with value 1
- Each element with value 1 is a part of some block
- Adjacent elements with value 1 belong to the same molecule.

We only take into account the horizontal and vertical adjacency but not diagonal.

INPUT:

In the first line of the input we have the integers r and c, separated with one space.
Then we have the r lines, where each contains s 0's and 1's.
The numbers inside the individual lines are NOT separated by spaces.

The OUTPUT only print the number of blocks in the picture.

Example:

INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100

OUTPUT:
6

THIS IS WHAT I CAME UP SO FAR:

import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char ch[][];
int rowNum=sc.nextInt();
int columnNum=sc.nextInt();

[Code] ....

View Replies View Related

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

Jan 27, 2015

I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;

now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.

View Replies View Related

If Statement - Checking / Comparing Two Strings From User Input

Dec 7, 2014

I am trying to do is get a user to input their name into the system. Once the user has inputted the name into the system, the system should check to see if the name entered matches "alice". If this is true then the system should print "welcome to the system alice". If it is not alice it should not do anything.

Here is the code I have so far... The fault/ error seems to be with the sInput string in the IF statement.

import java.io.*;
public class Hellohuman
{
public static void main (String[] args) {
try {
InputStreamReader isr = new InputStreamReader(System.in);

[Code] ....

View Replies View Related

Simple If Statement Taking A String Input Doesn't Work

Jun 6, 2014

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
if (input == "next") {
System.out.print("good Job!");
}
else {
System.out.println("Why ?");
}

//If I type in next it prints out "Why ?" instead of "Good Job!" - Why though ?

View Replies View Related

Write A Statement That Reads User Input Integer Into Defined Variable

Jan 20, 2015

so i'm following a java tutorial from the book and it has a few challenge questions. and i'm stucked on one. i think i just don't understand what is it that its asking me. heres the question, Write a statement that reads a user's input integer into the defined variable, and a second statement that prints the integer. assuming scanner is given, and i checked my heading code is ok.
 
Scanner scnr = new Scanner(System.in);
int userNum = 0;
System.out.println("What is the product of 8 time 2");
userNum = scnr.nextInt();

[code]....

View Replies View Related

Switch Statement That Decides What Range To Print Based On Letter Grade Input

Nov 1, 2014

Alright so I wrote a switch statement that decides what range to print based on the letter grade input.

import java.util.Scanner;
public class SwitchPractice
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);

[code]...

It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.

View Replies View Related

User Input Random Numbers Then Program Sort Them In Different Ways - Missing Return Statement

Apr 7, 2014

I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class SwingSorts extends JFrame implements ActionListener
{
JRadioButton bubble;
JRadioButton selection;

[Code] .....

View Replies View Related

2D Array Addition - Not A Statement Error

Dec 19, 2014

I am new to java programming. I want to write a java program for 2D array addition. My code is:

Java Code:

class Matrixadd {
public static void main(String...s) {
int x[][]={
{1,2,3},
{4,5,6},
{7,8,9},

[Code] .....

I am getting a 'Not a statement' error at line 36.

View Replies View Related







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