I'm new to Java. I would really appreciate it if you could help me figure this out.
I'm trying to make a program to read user inputs (integers) and store them into an array and print them out afterwards.
I use a variable called currentSize to keep track of how many variables were inserted.
Since I dont know how many inputs there are going to be, every time the element numbers equals the array length, I use Arrays.copyOf method to double the size of the existing array.
I use a while loop with in.hasNextInt() with the goal to exit the while loop once the user enters something else such as a letter rather than an integer.
My problem is it keeps throwing InputMismatchException although the idea is for it to exit the while loop once a non-integer value is entered.
As I was trying to pinpoint where it went wrong I added 2 print statements to make sure the number of elements is counting correctly and that Array length is increasing its size.
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
I have tried another approach and got it to work the way I wanted without the for loop so I suspected the while loop is the issue.
import java.util.Scanner;
import java.util.Arrays;
public class ArrayPrinter{
public static int DEFAULT_LENGTH = 2;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//keep track of how many element we insert
int currentSize = 0;
int[] numList = new int[DEFAULT_LENGTH];
System.out.println("Please insert value to store in array: ");
while(in.hasNextInt()){
for(int i = 0; i < numList.length; i++){
numList[i] = in.nextInt();
currentSize++;
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
if(currentSize == numList.length){
numList = Arrays.copyOf(numList, currentSize * 2);
}
}
}
for(int number : numList){
System.out.print(number + " ");
}
}
}
It may be just something very easy but I have looked through all the other posts on Stack but to no avail.
Thank you so much!
There is a problem with your algorithm. The line containing: while(in.hasNextInt()) will run only one time,
before the first input. After that your second loop for(int i = 0; i < numList.length; i++) will run indefinitely or until you type an invalid integer.
In order to understand the problem you need to look exactelly at the line where the exception happens: numList[i] = in.nextInt();. The method in.nextInt is not able to handle an invalid input.
You need only the "for" loop and you need to use hasNextInt inside it.
for (int i = 0; i < numList.length; i++) {
if (in.hasNextInt()) {
numList[i] = in.nextInt();
currentSize++;
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
if (currentSize == numList.length) {
numList = Arrays.copyOf(numList, currentSize * 2);
}
}
}
for (int number : numList) {
System.out.print(number + " ");
}
I understand that you are playing around with loops and arrays in order to learn it. However, to implement this logic
in a simpler way you should use a List. A List (i.e.: ArrayList) can handle automatically a variable number of items and your final code would be much simpler.
Related
write a program in java that accept n number of elements in an array then display only duplicate elements
for example if we enter 12352342678898
it will show 238
i have tryed this but this is very long
import java.util.*;
class JavaApplication1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size: ");
int s=sc.nextInt();
int a[]=new int[s];
int t[]=new int[s];
int p=0;
System.out.println("Enter numbers :-");
for(int i=0;i<s;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<s;i++)
{
boolean flag=false;
for(int j=i+1;j<s;j++)
{
if(a[i]==a[j])
{
flag=true;
break;
}
}
if(flag==true)
{
t[p]=a[i];p++;
}
}
System.out.print("duplicate elements are:- ");
for(int i=0;i<p;i++)
{
boolean flag=false;
for(int j=i+1;j<s;j++)
{
if(t[i]==t[j])
{
flag=true;
break;
}
}
if(flag==false)
{
System.out.print(t[i]+" ");
}
}
}
}
I'll tell you the keypoints of the solution I'd implement.
Do not force the user to establish the size of the array beforehand: just say press "q" to exit.
The idea of using the index of the array as the numbers is not a bad one as long as you are the only programmer. If not I'd use a dictionnary with "element":"number of repetitions". You can then add letters to your program
Finally, you need to make it more modular. First create a function that takes all the numbers and return an array. Then create a function that filters that array and returns the repeated elements. Finally, create a function that writes that array. In your main you should only call those 3 functions.
You can create a Set and as you go through the numbers the first time, you can add them to the set. It will not allow you to add duplicate values because sets do not allow duplicates. This will result in you only having to loop through the numbers once and then at the end, display the numbers contained in the set. I hope that helps
One possibility is to sort the array a, e.g., using java.util.Arrays.sort(a);. Then just iterate through a and check for repeating numbers. That part could look like this:
Arrays.sort(a);
Integer prev = null;
Integer printed = null;
for (int val : a) {
if (prev != null && val == prev && (printed == null || val != printed)) {
System.out.print(val + " ");
printed = val;
}
prev = val;
}
This is quite an ugly program and solution (my code). While this works from algorithmic point of view, I do not recommend this as a good programming practice. For instance, using null checks... It is also advisable to move the detection of duplicate numbers into separate method (or better, class), without printing out. This is mixing computation with user interface, bad practice.
I am trying to take certain number as an input and create an array of same length. I know the array is indexed beginning from 0 to n-1, for the array of length n.
I cannot quite figure out where I made the mistake.
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("Enter the number of test cases:");
int test = read.nextInt();
String[] name = new String[test];
if (test > 0 && test <= 20) {
System.out.println("Please give " + test + " names:");
for (int i = 0; i < name.length; i++) {
name[i] = read.nextLine();
}
}
}
}
Expectation: for example if the input is 9, an array of size 9
result: if the input is 9, the array creates is of 8
The problem is not with arrays but with read.nextLine(). You can use read.next().
Just use .next() instead of .nextLine(). It only parses the next element of the command line. With this you could also type all names in one line, because the Scanner separates by whitespace characters instead of lines.
See the Java documentation for further information about the two methods. https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
The problem is that after the read.nextInt() your position is after the int just before the line end so the first read.nextLine() in the loop will read an empty line.
You can either make a read.nextLine() before the loop or instead of nextInt you can read with nextLine() and cast to int with parseInt() in case you are sure you hit an enter after the int.
E.g. instead of
int test = read.nextInt();
do
int test = Integer.parseInt(read.nextLine());
If you don't want to require enter, then you can change
name[i] = read.nextLine();
to
name[i] = read.next();
I am attempting to solve this problem but I'm not sure why my solution doesn't work. My attempts at debugging tell me that the solution is attempting to access indices outside of the bounds of some of the data structures, but this does not make sense to me as it seems like my for-loop test would would.
There are probably many other issues with this solution besides this.
I'm also 90% sure that there's a more efficient way to do this. Could you help me figure out what it is I've done wrong here?
If there is a more efficient solution, what would it be? I'm struggling to deal with keeping track of the same number of spaces in the same order in an efficient way.
If any more information is necessary, please let me know and I will update.
public static void printReversed(String line){
Scanner console = new Scanner(line);
ArrayList<String> list = new ArrayList<String>(); // keeps track of words in line
int spaceOccur = 0; // keeps track of the number of times there are spaces
while (console.hasNext()){
list.add(console.next());
spaceOccur++;
}
int[] spaces = new int[spaceOccur]; // keeps track of number of spaces for each occurrence of spaces
int count = 0; // for spaces[] traversal
// searches through original input to get number of spaces
for (int i = 0; i < line.length() - 1; i++){
if (line.charAt(i) == ' '){
int j = i;
int num = 0;
// traversal through spaces to count how many
while (line.charAt(j) == (' ')){ // first error here
num++;
j++;
}
i = j; // updates for loop counter to point past spaces
spaces[count] = num; // saves number of spaces
count++;
}
}
// printing reversed input
for (int k = 0; k < list.size(); k++){
// prints reversed chars
for (int m = list.get(k).length(); m > 0; m++){
System.out.print(list.get(k).charAt(m));
}
// prints spaces
for (int n = 0; n < spaces[k]; n++){
System.out.print(" ");
}
}
}
I'd say that you're on right tracks, but some places need some closer inspection. The first loop seems to have some problems: The j++ is probably the one that goes beyond boundaries of the array - at least if you have spaces at the end of your string. And the whole loop itself seems to ignore the last character of the line.
Are you sure you even need this first loop? If I have understood correctly, the ScannerĀ“s next() will give you strings between the spaces; in the case of two consecutive spaces I think it should return you an empty string. In this case you could just loop the list the way you do in the end of your function, and print a space character when you encounter an empty string in your list. Otherwise just print the word backwards, just like you already do (except that it should be m-- instead of m++ in the last for loop).
But if the Scanner won't give you the empty strings when there are two or more consecutive space characters, I bet the string's split() method should work.
I'm doing a last assessment for the semester and interestingly enough, the code I have written seems to be devoid of errors other than a few simple ones I have just ironed out. However, I am stuck with one last error I cannot get my head around.
The program I am doing is a random numbers generator, using a while loop to generate the numbers and store them in the array, however, a second while loop has to be used to check to see if that number is already in the array, if that number is already in the array, this number has to be discarded and another value has to be obtained to put into the same index. After this the array is printed as a grid of 5x 10. However, upon using a continue command towards the end of the first loop, it comes up with the error:
Random50.java:52: error: continue outside of loop
continue;
^
As much as it seems obvious, I have no idea how to alter my code to make the program run, I was using the the continue command to return to the start of the first loop without incrementing a counter variable, so another value could be stored in the same index again.
import java.util.Random;
import java.util.Arrays;
public class Random50
{
public static void main(String[] args)
{
// Declare and initalise array
int[] random50 = new int[5];
// Declare and initalise counter variable
int i = 0;
// Declare and initalise repeater variable
int r = 0;
// Generator while loop
while (i < random50.length)
{
// Generate random number
int n = (int) (Math.random() * 999) + 1;
// Initalise variables for second while loop
int searchValue = i;
int position = 0;
boolean found = false;
// Duplicate while loop
while (position < random50.length && !found)
{
if (random50[position] == searchValue)
{
found = true;
}
else
{
position++;
}
}
// Return to first loop, determine if duplicate to return to the start of the loop early
if (found);
{
continue;
}
// Store value into array
random50[i] = n;
// Print value and add to counter variable
System.out.print(random50[i] + " ");
r++;
// reset counter variable to maintain grid
if (r == 5)
{
System.out.println("");
r = 0;
}
i++;
}
}
}
So, how can I get the continue to work or in other words, return to the start of the first loop mid loop?
The problem is your while() loop terminating instantly due to an obsolete ; probably placed by accident:
while (i < random50.length);
So your whole loop body will execute exactly once, no matter the condition (which will most likely be optimized out).
Once this is fixed, your use of continue; should work as expected.
Edit:
Same problem further down below:
if (found);
Due to this line you'll always execute the continue; following within these brackets, so the code below becomes unreachable.
I am a beginner and I know I'm making a rookie mistake. I have the following code in my actionePerformed part of my JApplet. the if statement within the for loop is supposed to search for the matching name in array s[] (t6 is a JtextArea) and assign the value of int m to int n, so i can retireve all the information of s[i]. problem is that my n is always 0 not matter what!!! What am I doing worng?
if (e.getSource() == b7) {
for(int m=0; m>i ; m++){
if(t6.getText().equals(s[m].getName())){
n=m;
}
}
String text1 = "";
text1 += s[n].getName().toString() + ", average=" + s[n].getAvgMark()
+ ", " + s[n].getProgramName().toString() + ", "
+ s[n].getDegree()+ ", " + s[n].getUni1() +"-"+ s[n].getStatus0()
+", "+ s[n].getUni2()+"-"+ s[n].getStatus1() + ", "
+ s[n].getUni3()+"-"+ s[n].getStatus2()+"\n";
ta2.setText(text1);
}
I bet you problem is that you you start int n = 0.
Look at your loop
for(int m=0; m>i ; m++){
Th only way this will not be an infinite loop is if i is 0 or less. Which I'll assume it is do to you not getting an infinite loop. And since you're not getting an ArrayIndexOutOfBoundsException I'll assume that i is 0 and not a negative. Therefore
for(int m=0; m>i ; m++){
states that continue the loop if m is greater than 0, which it never is.
So your array index will only be 0 once. It never loops.
Even if you i++, m will NEVER be greater than i because they start evenly and would be incremented evenly.
Just a hunch
Maybe you want this
for(int m = 0; m < s.length ; m++){
// iterates [size of the s array] times
Your code would be easier to debug if you used more descriptive variable names, and you didn't chain and concatenate so much in one statement. You can be a little more verbose; it isn't a competition to write the shortest or most obscure code!
I also suggest you initialize the variable, and prepare for failure.
int matchingIndex = 0; // initialize that variable!
int upperBound = myList.legnth;
for(int m=0; m > upperBound; m++){
String sourceText = searchSource.getText();
String thisName = myList[m].getName();
if(sourceText.equals(thisName)){
matchingIndex = m;
}
}
if (matchingIndex < 1) {
outputElement.setText('No match'); // or some other way to display the error
return;
}
... you see there, I renamed the obscure n to matchingIndex, the secretive t6 to searchSource, the dubious i to upperBound (and I made sure it is what it should be, the length of myList), questionable ta2 becomes outputElement, and the shy s to myList. I also unchained the calls and matches, and now you can read that code. And, you can output different parts to debug it. Think sourceText SHOULD be matching thisName? Output the values and see what the discrepancy is!
Finally, I added some logic to deal with the distinct possibility that no match is found.