This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm trying to take numbers (integers) from a website and put them into an array for further manipulation. Here is what I have so far
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class Scores {
public static void main(String[] args) throws IOException{
URL url = new URL("http://cs.armstrong.edu/liang/data/Scores.txt");
Scanner input = new Scanner(url.openStream());
int []score = new int [100];
int i = 0;
while (input.hasNextInt()) {
score[i++] = input.nextInt();
System.out.println(score);
}
}
}
The problem is all of the numbers in the array come back as [I#6bc7c054, any help would be appreciated.
System.out.println(score[i]);
you were printing the array itself
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
import java.util.Scanner;
import static java.lang.System.out;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Safeties;
out.print("Safeties:");
Safeties = input.nextInt();
out.print("Home Team:");
String HomeTeam = input.nextLine();
out.print("Touchdowns:");
double Touchdown2 = input.nextDouble();
}
}
The output is:
Safties:
Home Team:Touchdowns:
But it's supposed to be
Safties:
Home Team:
Touchdowns:
I'm assuming it's because I have an int above a string but I don't know how to fix this problem. I need them in that order. I'm completely lost. Can anyone help?
Add input.nextLine(); below out.print("Home Team:");
This question already has answers here:
NoSuchElementException with Java.Util.Scanner
(10 answers)
Closed 6 years ago.
I am trying to build a very basic program in java to print all the unique characters from the string but I am getting runtime error.
Input - amanda
output -amnd
import java.util.*;
class uniquechars {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("Enter a string:");
String str = inp.nextLine(); // input from user
String res="";
for (int i=0;i<str.length();i++){
int count=0;
for(int j=0;j<res.length();j++){
if(str.charAt(i)==res.charAt(j)){
count++;
}
}
if(count==0){
res = res+str.charAt(i);
}
}
System.out.println("Output string with only unique characters:"+res);
}
}
Error
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at uniquechars.main(Main.java:6)
If you are using any online tool to test your code, be sure to provide input to the program.
My guess is you are forgetting to give the input to the program while running it on an online tool.
It works on codechef.com/ide, you just have to select your programming language from the dropdown list. as shown here.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 7 years ago.
problem: i am trying to take string input from files and then store it in an arraylist then trying to search specific words and removing them and the resultant output should go to another text file... this code works fine if there are only 2 elements in a list . but if i put more it showing java.lang.IndexOutOfBoundsException index:12 size 12 . i am not that good in java so pls help and sorry for any silly mistakes ...
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class replace{
public static void main(String[] args) throws Exception{
String filen = "C:/Users/Damini/Desktop/hi.txt";
FileReader file = new FileReader("C:/Users/Damini/Desktop/rules.txt");
BufferedReader b = new BufferedReader(file);
PrintWriter pw = new PrintWriter(filen);
List<String> temps = new ArrayList<String>(10);
String line = b.readLine();
while(line!= null)
{
temps.add(line);
line = b.readLine();
}
int size = temps.size()-1;
for(int i=0;i<=size;i++)
{
if(temps.get(i).equals("hello"))
{
temps.remove(i);
}
}
pw.println(temps);
pw.close();
System.out.println("output:"+ temps);
b.close();
}
}
Try to write your arrayList temps without capacity 10 like this:
List<String> temps = new ArrayList<String>();
the capacity is how many elements the list can potentially accommodate without reallocating its internal structures.
In java arrays are 0 based. It means that an array of size 12 has index between 0 and 11.
Modify your code with
for (int i = 0; i <= size; i++)
because size is 12 and last valid index is 11.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I want to make a program that copies strings into a vector until the hold string is set to a certain value, at which point the program should print out the elements of the array.
I'm not sure how the in.nextline() function works, so it could be that I'm not erasing previous entries from hold, or is it that my if(hold=="0") check is simply not valid java?
import java.util.*;
import java.util.Scanner;
public class startingPoint
{
public static void main(String roark[])
{
String hold;
boolean finished=false;
Scanner in = new Scanner(System.in);
Vector<String> vec = new Vector<String>();
while(finished==false){
System.out.print("Enter the string you'd like to save, or enter 0 to print out saved strings\n");
hold=in.nextLine();
if(hold=="0"){
for(int looper=0;looper<vec.size();looper++){
System.out.print(vec.get(looper));
System.out.print("\n");
}
finished=true;
}else{
vec.add(hold);
}
}
}
}
You need to use equals method:
if("0".equals(hold){
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
I'm pretty new to Java and this website, and I'm sorry if this question has already been asked but if it isn't to hard, an answer would be extremely helpful.
Psuedocode
import java.util.Random;
import java.util.Scanner;
...
private Random rand = new Random();
private Scanner scan = new Scanner();
...
public static int randomInt() {
return rand.nextInt(scan.nextInt());
}